Geom_jitter闪避位置

时间:2019-03-01 17:19:04

标签: r ggplot2 plot

我正在尝试在顶部绘制geom_jitter的ggplot条形图,以便更好地可视化我的度量变化。 代码如下:

ggplot(data) +
   geom_bar(stat="summary", fun.y="mean", aes(x=cohort, y=., fill=time), position="dodge") +
   geom_jitter(aes(x=cohort, y=., colour=time), size=0.5)

geom_barcolourposition="dodge"组合在一起以便并排显示小节,但是geom_jitter无法识别position="dodge"参数,因此这些点均匀分布在两列colour的整个宽度上,如下所示(显然,颜色不是很好,我将代码简化到了关键位):​​

enter image description here

如何使geom_jittergeom_bar的列对齐?

如果您想尝试重现此内容,则数据的设置基本上是这样的:

data <- read.table(header=TRUE, text='
subj cohort time .
   1      EL1 t1 0.433
   2      EL1 t1 0.545
   3      EL1 t1 0.698
   4      EL1 t1 0.224
   5      EL1 t1 0.463
   6      HL1 t1 0.211
   7      HL1 t1 0.702
   8      HL1 t1 0.310
   9      HL1 t1 0.822
   10     EL1 t1 0.0
   11     EL1 t1 0.544
   12     EL1 t1 0.234
   13     EL1 t1 0.492
   14     HL1 t2 0.234
   15     HL1 t2 0.755
   16     HL1 t2 0.321
   17     HL1 t2 0.600
   18     EL1 t2 0.0
   19     EL1 t2 0.522
   20     EL1 t2 0.624
   21     EL1 t2 0.239
   22     EL1 t2 0.474
   23     HL1 t2 0.293
   24     HL1 t2 0.599
   25     HL1 t2 0.310
   26     HL1 t2 0.411
')

1 个答案:

答案 0 :(得分:1)

您可以使用geom_jitter而不是geom_point(position="jitter")(这只是geom_point(position=position_jitterdodge())的简写),这样可以同时产生抖动和闪避

ggplot(data) +
  geom_bar(stat="summary", fun.y="mean", aes(x=cohort, y=., fill=time), position="dodge") +
  geom_point(aes(x=cohort, y=., colour=time), size=0.5, position=position_jitterdodge())

给出请求的输出: enter image description here