分组箱线图与ggplot2中的点线图结合

时间:2018-07-20 14:12:56

标签: r ggplot2

我是stackoverflow的新手,因此,如果可以改进提出问题的方式(在这种情况下,如果您让我知道我很高兴),请原谅。

我正在ggplot2中使用以下代码来生成与点图结合的分组框图(不幸的是,我还不能发布图像(没有信誉)):

ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
       geom_boxplot(position=position_dodge(1), notch=T,
                    outlier.colour = NA, width = .7, alpha=0.2) +
       geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
                    position=position_dodge(1), dotsize=0.5, alpha=1) +
       stat_summary(fun.y=mean, geom="point", shape=23, size=5,
                    position=position_dodge(0.3)) +
       stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1, 
                    position=position_dodge(0.3), size=1.2)+
       scale_y_continuous(limits=c(0,100)) +
       scale_fill_discrete(name="Group")

我的问题是:

  • 如何为不同的元素使用不同的颜色?我试图在geom_boxplot()和geom_dotplot中添加颜色/填充命令,但这不起作用:例如,如果我在geom_dotplot()中添加fill =“ green”,则所有点都变为绿色,并在框线图之间居中。我该如何重写代码以获取
  • 为所有箱形图填充白色
  • 组1点的蓝色填充和黑色线
  • 第2组点的绿色填充和黑色线
  • 所有普通钻石的黑色填充

    1. 如何将“强度”类别(即三对箱形图)彼此拉开?

    2. 如何在框线图旁边显示点线图,并在框线图中显示均值+ CI?

    3. 即使我将y轴定义为从0到100,为什么我的协调系统仍然从<0到> 100?

谢谢!

编辑180722

谢谢您的评论。数据头是:

   id intensity AQ_sum group acc   percacc
1  54        40     11  COMP   5  20.83333
2  54        60     11  COMP  18  75.00000
3  54        80     11  COMP  24 100.00000
4  55        40     12  COMP   9  37.50000
5  55        60     12  COMP  22  91.66667
6  55        80     12  COMP  24 100.00000
7  58        40     10  COMP   8  33.33333
8  58        60     10  COMP  22  91.66667
9  58        80     10  COMP  23  95.83333
10 59        40      6  COMP  19  79.16667
11 59        60      6  COMP  24 100.00000
12 59        80      6  COMP  24 100.00000
13 60        40      9  COMP  10  41.66667
14 60        60      9  COMP  23  95.83333
15 60        80      9  COMP  22  91.66667
16 61        40     13  COMP   4  16.66667
17 61        60     13  COMP  19  79.16667
18 61        80     13  COMP  24 100.00000
19 62        40     12  COMP  16  66.66667
20 62        60     12  COMP  23  95.83333

我更新的代码是

ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
  geom_boxplot(position=position_dodge(0.8), notch=T,
               outlier.colour = NA, width = .4, alpha=0.3) +
  geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
               position=position_dodge(0.8), dotsize=0.4, alpha=1)+
  scale_fill_manual(values=c("#999999", "#E69F00"), name="Group") +
  stat_summary(fun.y=mean, geom="point", shape=23, size=3,
               position=position_dodge(0.2)) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1, 
               position=position_dodge(0.2), size=0.5)+
  scale_y_continuous(limits=c(0,103), expand = c(0, 0), 
                     breaks=c(0,20,40,60,80,100), name="Percentage accuracy") +
  scale_x_discrete(expand = c(0, 0.6), name="Degree of intensity (in percent)") +
  labs(title="Accuracy by intensity and group\n") +
  theme_light()+
  theme(plot.title = element_text(face='bold', size=12, hjust = 0.5),
        axis.title.x = element_text(size=10,hjust=0.5),
        axis.title.y = element_text(size=10,vjust=1),
        axis.text.x = element_text(size=10,color='black'),
        axis.text.y = element_text(size=10, color='black'),
        panel.grid.major.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"), 
        panel.grid.minor.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"),
        panel.grid.major.x = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
  ggsave("plotintensity.png", width = 10, height = 5) 

See image of the graph

0 个答案:

没有答案