在带有position_dodge的条形图上叠加点(和误差条)

问题描述 投票:0回答:1

我一直在尝试寻找针对我的特定问题的答案,但是我没有成功,所以我刚刚做了一个MWE来发布在这里。

我尝试了答案here,但没有成功。

我想做的任务似乎很容易,但我无法弄清楚,结果使我有一些基本问题...

我只想使用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()

但是我得到了:

example

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

此外,我想用geom_errorbar添加错误栏,但我没有设法得到有效的示例...

此外,实际值指向条形图,这让我想知道这里实际绘制的是什么...是否正确地为每个条形图取值,以及为什么绘制最大值(或看起来如此)?默认值。

我认为应该以中位数(或均值)正确绘制此图,并在方框图中添加诸如晶须之类的误差线(最小值和最大值)。

任何想法如何...

  • ...“ rep”值点是否以正确的顺序显示?
  • ...将条形图显示的值从最大值更改为中位数?
  • ...添加带有最大值和最小值的误差线?
r ggplot2 bar-chart point errorbar
1个回答
2
投票

我对您的绘图代码进行了一些重组,以简化操作。秘诀是使用正确的分组(否则可以从fillcolor推断出。此外,由于要躲避多个级别,因此必须使用dodge2

[如果不确定条形/柱形图中的“绘制位置”,添加选项color="black"总是很有帮助,因为您使用了dodgedodge2

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)

给出:enter image description here

关于误差线

由于只有三个重复,所以我会显示原始数据点,也可能会显示小提琴图。为了完整起见,我还添加了一个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")

给予

enter image description here

评论后更新

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

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")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.