【问题标题】:ggplot2; how to draw group mean lines of two different groups in a categorical variableggplot2;如何在分类变量中绘制两个不同组的组平均线
【发布时间】:2016-03-17 02:30:28
【问题描述】:

我有以下一组数据;

    V1 V2  V3
 1: G1 P1 82.7
 2: G2 P1 64.6
 3: G2 P1 81.2
 4: G2 P1 95.3
 5: G1 P1 80.0
 6: G2 P1   NA
 7: G2 P1 65.0
 8: G1 P1 83.8
 9: G1 P1 88.0
10: G1 P1 66.9
11: G1 P1 56.8
12: G1 P2 65.1
13: G2 P2 57.7
14: G2 P2 60.4
15: G2 P2 18.6
16: G1 P2 41.2
17: G2 P2 47.0
18: G2 P2 37.1
19: G1 P2 18.8
20: G1 P2 47.9
21: G1 P2 40.0
22: G1 P2 54.3

我使用 ggplot2 制作了以下情节;

ggplot(a,aes(x=V2,y=V3))+
    geom_jitter(aes(group=V1,color=V1,na.rm=T), position =position_jitterdodge())+
    stat_summary(fun.y="mean",geom="crossbar", 
             mapping=aes(ymin=..y.., ymax=..y..), width=1)

V1 包含两个分类变量,P1 和 P2,其中有两个子类别,G1 和 G2。我喜欢做的是用 G1 和 G2 的平均线生成图,但上面的代码给了我分类变量 P1 和 P2 的平均值。

非常感谢您对此提供的任何帮助。

谢谢。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    color=V1ggplot() 移动到aes() 以使横杆具有不同的颜色,并添加postion_dodge()stat_summary()

    ggplot(a,aes(x=V2,y=V3,color=V1))+
      geom_jitter(aes(group=V1,na.rm=T), position =position_jitterdodge())+
      stat_summary(fun.y="mean",geom="crossbar", 
                   mapping=aes(ymin=..y.., ymax=..y..), width=1,
                   position=position_dodge(),show.legend = FALSE)
    

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多