R中带有子组的箱线图 [英] Boxplot with Subgroups in R

查看:54
本文介绍了R中带有子组的箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 R 中制作

请注意,对 ggplot() 的大部分调用都是为了获得更接近地模仿您的绘图的答案.可以通过以下方式获得足够的箱线图:

ggplot(df1,aes(x=Point,y=value,fill=variable))+geom_boxplot()

I am trying to make a Boxplot with subgroups in R.

My data looks like this:

Sample Point    N.conc  toc
H1  0.2 0.2
H1  0.3 0.3
H1  0.4 0.4
H1  0.3 0.3
H1  0.3 0.3
H1  0.5 0.5
H1  0.5 0.5
H1  0.4 0.4
H1  0.1 0.1
H2  0.4 0.4
H2  0.5 0.5
H2  0.5 0.6
H2  0.4 0.5
H2  0.1 0.1
H2  0.3 0.3
H2  0.4 0.4
H2  0.3 0.3
H2  0.3 0.3
H3  0.4 0.4
H3  0.5 0.8
H3  0.3 0.3
H3  0.2 0.2
H3  0.2 0.2
H3  0.4 0.4
H3  0.3 0.3
H3  0.2 0.2
H3  0.3 0.4

How can I make a boxplot similar to the attached picture?

解决方案

Note that I read in your data using the following:

df <- read.table(text='Point N.conc toc
                       H1  0.2 0.2
                       H1  0.3 0.3
                       H1  0.4 0.4
                       H1  0.3 0.3
                       H1  0.3 0.3
                       H1  0.5 0.5
                       H1  0.5 0.5
                       H1  0.4 0.4
                       H1  0.1 0.1
                       H2  0.4 0.4
                       H2  0.5 0.5
                       H2  0.5 0.6
                       H2  0.4 0.5
                       H2  0.1 0.1
                       H2  0.3 0.3
                       H2  0.4 0.4
                       H2  0.3 0.3
                       H2  0.3 0.3
                       H3  0.4 0.4
                       H3  0.5 0.8
                       H3  0.3 0.3
                       H3  0.2 0.2
                       H3  0.2 0.2
                       H3  0.4 0.4
                       H3  0.3 0.3
                       H3  0.2 0.2
                       H3  0.3 0.4',header=T)

You can try this:

library(ggplot2)
library(reshape2)

df1 <- melt(df,id.vars=1)

ggplot(df1,aes(x=Point,y=value,fill=variable))+
  stat_boxplot(geom="errorbar",
               stat_params = list(width = 0.2),
               position=position_dodge(width=0.3))+
  geom_boxplot(position=position_dodge(width=0.3),width=0.25)+
  ylab("mg/l")+xlab("")+labs(fill="")+theme_classic()+
  theme(axis.title.y = element_text(angle=0))

Note that most of the call to ggplot() was to get the answer to more closely mimic your drawing. A sufficient boxplot could have been achieved with:

ggplot(df1,aes(x=Point,y=value,fill=variable))+geom_boxplot()

这篇关于R中带有子组的箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆