繁体   English   中英

使用 position_dodge 在条形图上叠加点(和误差线)

[英]Overlay points (and error bars) over bar plot with position_dodge

我一直试图寻找我的特定问题的答案,但我没有成功,所以我刚刚制作了一个 MWE 在这里发布。

我在这里尝试了答案但没有成功。

我想做的任务看起来很简单,但我无法弄清楚,我得到的结果让我有一些基本的问题......

我只想使用ggplot2在条形图上叠加点和误差ggplot2

我有一个长格式数据框,如下所示:

> mydf <- data.frame(cell=paste0("cell", rep(1:3, each=12)),
   scientist=paste0("scientist", rep(rep(rep(1:2, each=3), 2), 3)),
   timepoint=paste0("time", rep(rep(1:2, each=6), 3)),
   rep=paste0("rep", rep(1:3, 12)),
   value=runif(36)*100)

我试图通过以下方式获得我想要的情节:

myPal <- brewer.pal(3, "Set2")[1:2]
myPal2 <- brewer.pal(3, "Set1")
outfile <- "test.pdf"
pdf(file=outfile, height=10, width=10)
print(#or ggsave()
  ggplot(mydf, aes(cell, value, fill=scientist )) +
  geom_bar(stat="identity", position=position_dodge(.9)) +
  geom_point(aes(cell, color=rep), position=position_dodge(.9), size=5) +
  facet_grid(timepoint~., scales="free_x", space="free_x") +
  scale_y_continuous("% of total cells") +
  scale_fill_manual(values=myPal) +
  scale_color_manual(values=myPal2)
)
dev.off()

但我得到了这个:

例子

问题是,每个“科学家”栏应该有 3 个“代表”值,但这些值按“代表”排序(它们应该是 1,2,3,1,2,3,而不是 1,1, 2,2,3,3)。

此外,我想用geom_errorbar添加误差geom_errorbar但我没有设法得到一个有效的例子......

此外,叠加实际值指向条形,这让我想知道这里实际绘制的是什么......如果每个条形的值都正确,以及默认情况下绘制最大值(或看起来如此)的原因。

我认为应该正确绘制的方法是使用中位数(或平均值),在箱线图中添加像胡须一样的误差线(最小值和最大值)。

任何想法如何...

  • ...“代表”值点是否以正确的顺序出现?
  • ...将条形显示的值从最大值更改为中值?
  • ...添加具有最大值和最小值的误差线?

我稍微重组了你的绘图代码,让事情变得更容易。 秘诀是使用正确的分组(否则可以从fillcolor推断出。此外,由于您要在多个级别上躲避, dodge2必须使用dodge2

当您不确定条形图/柱形图中的“绘制在何处”时,添加选项color="black"总是有帮助的,这表明由于您使用的是dodge而不是dodge2 .

p = ggplot(mydf, aes(x=cell, y=value, group=paste(scientist,rep))) +
  geom_col(aes(fill=scientist), position=position_dodge2(.9)) +
  geom_point(aes(cell, color=rep), position=position_dodge2(.9), size=5) +
  facet_grid(timepoint~., scales="free_x", space="free_x") +
  scale_y_continuous("% of total cells") +
  scale_fill_brewer(palette = "Set2")+
  scale_color_brewer(palette = "Set1")

ggsave(filename = outfile, plot=p, height = 10, width = 10)

给出: 在此处输入图片说明

关于误差线

由于只有三个重复,我会显示原始数据点和小提琴图。 为了完整起见,我还添加了一个geom_errorbar

ggplot(mydf, aes(x=cell, y=value,group=paste(cell,scientist))) +
  geom_violin(aes(fill=scientist),position=position_dodge(),color="black") +
  geom_point(aes(cell, color=rep), position=position_dodge(0.9), size=5) +
  geom_errorbar(stat="summary",position=position_dodge())+
  facet_grid(timepoint~., scales="free_x", space="free_x") +
  scale_y_continuous("% of total cells") +
  scale_fill_brewer(palette = "Set2")+
  scale_color_brewer(palette = "Set1")

在此处输入图片说明

评论后更新

正如我在下面的评论中提到的,百分比的叠加会导致不良结果。

ggplot(mydf, aes(x=paste(cell, scientist), y=value)) +
  geom_bar(aes(fill=rep),stat="identity", position=position_stack(),color="black") +
  geom_point(aes(color=rep), position=position_dodge(.9), size=3) +
  facet_grid(timepoint~., scales="free_x", space="free_x") +
  scale_y_continuous("% of total cells") +
  scale_fill_brewer(palette = "Set2")+
  scale_color_brewer(palette = "Set1")

在此处输入图片说明

暂无
暂无

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

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