首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >geom_text -将按颜色分组的绘图的文本设置为黑色

geom_text -将按颜色分组的绘图的文本设置为黑色
EN

Stack Overflow用户
提问于 2021-10-05 08:42:44
回答 2查看 54关注 0票数 1

我有一个按颜色分组的图形,每个点上方的文本中都有值。但是,我想要黑色的,因为它很难读。

能否帮助我在不丢失位置的情况下将文本的颜色从geom_text()更改为黑色?

向geom_text()添加color = "black"会弄乱文本的位置,但我不确定为什么……

我的数据:

代码语言:javascript
代码运行次数:0
运行
复制
structure(list(type = c("full", "full", "full", "noadiposity", 
"noadiposity", "noadiposity", "nocv", "nocv", "nocv", "nocv2", 
"nocv2", "nocv2", "noenergy", "noenergy", "noenergy", "noenergy2", 
"noenergy2", "noenergy2"), fi.cat = structure(c(1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Non-frail", 
"Pre-frail", "Frail"), class = "factor"), mean = c(0.0566154812663495, 
0.150817937965167, 0.285714285714286, 0.0459153181095795, 0.148380746409361, 
0.292192760942761, 0.0550705669171458, 0.147270820014587, 0.288461538461538, 
0.0530093023576546, 0.145279762712841, 0.292717236467236, 0.0531040684693624, 
0.146793227463497, 0.292499719195777, 0.054311319499867, 0.14824350913368, 
0.283745781777278), sd = c(0.0289882935363143, 0.0342654979144937, 
0.0393662413936823, 0.0298601819635622, 0.0345078387756546, 0.0422635938212309, 
0.0285280200524055, 0.0338893364029561, 0.0430877768970245, 0.0275365612798787, 
0.0358119253511248, 0.0415426999110631, 0.0270394224053038, 0.0374836297491701, 
0.0384867847822804, 0.0280882098015465, 0.0353023978795509, 0.039235018559239
)), row.names = c(NA, -18L), groups = structure(list(type = c("full", 
"noadiposity", "nocv", "nocv2", "noenergy", "noenergy2"), .rows = structure(list(
    1:3, 4:6, 7:9, 10:12, 13:15, 16:18), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

我使用的代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
library(ggplot2)

ggplot(grouped_mean, aes(x = fi.cat, y = mean, color = type)) +
  geom_point(position = position_dodge(0.9), size = 2) + 
  geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd), position = position_dodge(0.9), size = 1, width = 0.2) +
  geom_text(aes(label = round(mean, 2)), vjust = -5.5, position = position_dodge(0.9), size = 3) +
  labs(x = "FI category", y = "Mean FI score", color = "FI type") +
  scale_color_brewer(palette = "Blues") +
  theme_minimal()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-05 09:22:21

使用点和错误条的颜色会自动将它们分成组。如果您手动指定颜色,则需要在geom_text()中指定组,即:

代码语言:javascript
代码运行次数:0
运行
复制
ggplot(grouped_mean, aes(x = fi.cat, y = mean, color = type)) +
  geom_point(position = position_dodge(0.9), size = 2) + 
  geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd), position = position_dodge(0.9), size = 1, width = 0.2) +
  # Add grouping and manual colour to geom_text
  geom_text(aes(label = round(mean, 2), group=type), colour="black", vjust = -5.5, position = position_dodge(0.9), size = 3) +
  labs(x = "FI category", y = "Mean FI score", color = "FI type") +
  scale_color_brewer(palette = "Blues") +
  theme_minimal()

票数 1
EN

Stack Overflow用户

发布于 2021-10-05 09:15:09

这里有一个方法:不是最好的方法,但它似乎是有效的!此方法使用fill美学,并将颜色美学分别分配给geom_text美学:

代码语言:javascript
代码运行次数:0
运行
复制
library(ggplot2)

ggplot(grouped_mean, aes(x = fi.cat, y = mean, fill=type)) +
  geom_point(position = position_dodge(0.9), size = 2) + 
  geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd), position = position_dodge(0.9), size = 1, width = 0.2) +
  geom_text(aes(label = round(mean, 2)), vjust =-11.5, position = position_dodge(0.9), size = 3) +
  labs(x = "FI category", y = "Mean FI score", color = "FI type") +
  geom_point(aes(color=type), position = position_dodge(0.9), size = 2) + 
  geom_errorbar(aes(color=type, ymin = mean-sd, ymax = mean+sd), position = position_dodge(0.9), size = 1, width = 0.2) +
  scale_color_brewer(palette = "Blues") +
  theme_minimal()+
  guides(fill = "none")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69447346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档