geom_text - 使按颜色分组的绘图的文本变黑
Posted
技术标签:
【中文标题】geom_text - 使按颜色分组的绘图的文本变黑【英文标题】:geom_text - make text black for plot grouped by color 【发布时间】:2021-11-25 13:11:12 【问题描述】:我有一个按颜色分组的图表,每个点上方的文本中都有值。但是,我想要黑色的文字,因为它很难阅读。
我能否在不丢失定位的情况下将文本颜色从 geom_text() 更改为黑色?
将color = "black"
添加到 geom_text() 会弄乱文本的位置,但我不知道为什么...
我的数据:
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"))
我使用的代码:
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()
【问题讨论】:
【参考方案1】:使用点和误差线的颜色会自动将它们分成组。如果您手动分配颜色,则需要在 geom_text()
中指定组,即:
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()
【讨论】:
【参考方案2】:这是一种方法:不是最好的方法,但它似乎有效!
此方法使用fill
美学并将颜色美学单独分配给geom_text
美学:
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")
【讨论】:
谢谢,但由于解释的原因,我接受了上述作为答案,而且行数更少。 MIff 答案绝对是更好更正确的答案!:-)以上是关于geom_text - 使按颜色分组的绘图的文本变黑的主要内容,如果未能解决你的问题,请参考以下文章
当数据中不存在分组变量的所有级别时,绘图之间的颜色比例和图例一致
是否可以用ggplot2在R中以科学计数形式显示绘图geom_text数据标签?
如何在 R 中的 ggplot2 中更改 geom_text 中的字体颜色?