繁体   English   中英

将 geom_text 与 ggplot2 中的 geom_boxplot 对齐

[英]align geom_text with geom_boxplot in ggplot2

假设我有以下两个数据集:

behaviorm <- structure(list(sentential_connective = c("IF", "IF", "IF", "IF", 
"IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", 
"IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", 
"IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", "IF", 
"IF", "IF", "IF"), mentioned_object = c("Same", "Same", "Same", 
"Same", "Same", "Same", "Same", "Same", "Same", "Same", "Same", 
"Same", "Same", "Same", "Same", "Same", "Same", "Same", "Same", 
"Same", "Same", "Same", "Same", "Same", "Same", "Same", "Same", 
"Same", "Same", "Same", "Same", "Same", "Same", "Same", "Same", 
"Same", "Same", "Same", "Same", "Same"), agent_mood = c("Sad", 
"Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", 
"Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", 
"Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", 
"Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", "Sad", 
"Sad", "Sad", "Sad"), Chosen_Box = c("SD", "SD", "SD", "SD", 
"SD", "SD", "SD", "SD", "SD", "SD", "CS", "CS", "CS", "CS", "CS", 
"CS", "CS", "CS", "CS", "CS", "SS", "SS", "SS", "SS", "SS", "SS", 
"SS", "SS", "SS", "SS", "DD", "DD", "DD", "DD", "DD", "DD", "DD", 
"DD", "DD", "DD"), participant = c("a01", "a02", "a03", "a04", 
"a05", "a06", "a07", "a08", "a09", "a10", "a01", "a02", "a03", 
"a04", "a05", "a06", "a07", "a08", "a09", "a10", "a01", "a02", 
"a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10", "a01", 
"a02", "a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10"
), Counts = c(12L, 8L, 12L, 6L, 3L, 12L, 9L, 12L, 12L, 11L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 4L, 0L, 4L, 9L, 0L, 2L, 0L, 0L, 0L)), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 73L, 74L, 75L, 76L, 77L, 
78L, 79L, 80L, 81L, 82L, 145L, 146L, 147L, 148L, 149L, 150L, 
151L, 152L, 153L, 154L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 
224L, 225L, 226L), class = "data.frame")


res <- structure(list(sentential_connective = c("IF", "IF", "IF", "IF"
), mentioned_object = c("Same", "Same", "Same", "Same"), agent_mood = c("Sad", 
"Sad", "Sad", "Sad"), Chosen_Box = c("SD", "CS", "SS", "DD"), 
    statistic = c(54, 0, 0, 8), p.value = c(0.00362357852661936, 
    0.999052845531107, 0.999052845531107, 0.937942586194492), 
    Sig = c("*", "", "", ""), Counts = c(12L, 1L, 1L, 9L)), class = "data.frame", row.names = c(1L, 
73L, 145L, 217L))

下面的代码我用来做绘图:


library(ggplot2)
b <- ggplot()
b <- b + aes(x = sentential_connective, y = Counts)
b <- b + geom_boxplot(aes(color = Chosen_Box), data = behaviorm, outlier.color = NA)
b <- b + geom_text(   aes(label = Sig),        data = res, position = position_dodge2(width = 0.9), size = 5)
b

然而,在结果图中,来自res文件的标签与来自behaviorm文件的箱线图没有正确对齐。

我已经尝试了解释中解释的建议,但没有成功。

有什么建议? 谢谢。

因为您的sentential_connective是单因素IF ,而不是使用x = sentential_connective ,您应该使用x = Chosen_Box使箱线图与其组对齐,并重新使用此x来绘制数据集res的标签。

所以,这样的代码应该可以工作

library(ggplot2)
ggplot(behaviorm, aes(x = Chosen_Box, y = Counts))+
  geom_boxplot(aes(color = Chosen_Box), outlier.color = NA) +
  geom_text(data = res, aes(x = Chosen_Box, label = Sig, y = 13), size = 10)+
  ylim(0,14)

在此处输入图片说明


编辑:为多个变量添加 geom_text

根据您的评论,您在sentential_connective中至少定义了两个组,并且您希望能够将其保留为 x 参数。

因此,为此,我首先复制您的数据,以便在两个数据集中为sentential_connective设置两个因子级别:

behavior2 = behaviorm
behavior2$sentential_connective <- "IF2"
behav = rbind(behaviorm, behavior2)

res2 = res
res2$sentential_connective = "IF2"
RES = rbind(res2,res)

对于绘图,您通过使用position_dodge()接近解决方案,除了您还必须将它用于geom_boxplot

ggplot(behav, aes(x = sentential_connective, y = Counts, color = Chosen_Box))+
  geom_boxplot(position=position_dodge(width=0.8), outlier = NA)+
  geom_text(data = RES, aes( x = sentential_connective, y =13, label = Sig, group = Chosen_Box), size = 10, color = "black", position=position_dodge(width=0.8))+
  ylim(0,14)

在此处输入图片说明

这是您要找的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM