ggplot2;如何在分类变量中绘制两个不同组的平均线

时间:2023-01-20 12:11:48

I have a following set of data;

我有一系列的数据;

    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

I made the following plot by using ggplot2;

我用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)

ggplot2;如何在分类变量中绘制两个不同组的平均线

V1 contains two categorial variable, P1 and P2 and within them, there are two subcategories, G1 and G2. What I like to do is to generate the plot with mean lines of G1 and G2 but the code above gives me the mean of categorical variables, P1 and P2.

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

I'd really appreciate any help on this.

我非常感谢在这方面的任何帮助。

Thanks.

谢谢。

1 个解决方案

#1


2  

Move color=V1 to aes() of ggplot() to have different colors for crossbars and aldo add postion_dodge() to stat_summary().

将颜色=V1移动到ggplot()的aes(),使交叉条具有不同的颜色,aldo向stat_summary()添加postion_dodge()。

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)

ggplot2;如何在分类变量中绘制两个不同组的平均线

#1


2  

Move color=V1 to aes() of ggplot() to have different colors for crossbars and aldo add postion_dodge() to stat_summary().

将颜色=V1移动到ggplot()的aes(),使交叉条具有不同的颜色,aldo向stat_summary()添加postion_dodge()。

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)

ggplot2;如何在分类变量中绘制两个不同组的平均线