ggplot箱式图并排

时间:2019-03-22 15:22:46

标签: r ggplot2 boxplot

我想按介质类型并排显示盒图(总计和ELISA)。我在其他聊天中看到需要融合数据。当我这样做时,我得到以下信息

  

错误:stat_boxplot需要以下缺失的美感:y

我的数据:

media    total    ELISA  
surface     1.3     2.7
surface     5.2     6.5
surface     0.9     2.9
surface     0.7     2.1
porewater   376.8   651.0
porewater   288.7   308.0
porewater   32.3    63.3
porewater   69.7    184.0
porewater   116.0   11.4
porewater   31.6    51.5
porewater   38.6    97.5
porewater   63.2    152.0
porewater   118.0   254.0
porewater   1147.5  4174.0
porewater   626.6   798.0
porewater   39.2    81.8
porewater   3.3     7.5
porewater   70.3    231.5
porewater   1.7     3.2
sediment    567.0   1454.1
sediment    24.0    70.7
sediment    13.7    23.9
sediment    16.9    105.7
sediment    39.7    5.9
sediment    8.2     17.6
sediment    27.4    85.0
sediment    14.7    30.8

我的代码:

library(reshape2)
elisa.m <- melt(elisa, media.vars='media', measure.vars=c('total','ELISA'))

library(ggplot2)
p <- ggplot(elisa.m) +
  geom_boxplot(aes(x = media, y=value)) + 
  stat_boxplot(geom = "errorbar", width = 0.5)

我不确定为什么这不起作用。我应该将其命名为值以外的其他东西吗?

2 个答案:

答案 0 :(得分:0)

您的映射aes()应该在ggplot()内,stat_boxplot才能继承它。因此,

  p <- ggplot(elisa.m, aes(x = media, y = value)) +
  geom_boxplot() + 
  stat_boxplot(geom = "errorbar", width = 0.5)

答案 1 :(得分:0)

myplot = ggplot(elisa.m, aes(x=media, y=measurement))+
          geom_boxplot()+
          stat_boxplot(geom ='errorbar', width=0.5) +  
          facet_rep_wrap(~mode, repeat.tick.labels=TRUE)

但是,要使其正常工作,您必须重新组织数据,以便采用以下格式:

media   measurement  mode
surface    1.3       total
  .                  total
  .                  total
surface    2.7       elisa
  .                  elisa
  .                  elisa

希望它会有所帮助:) 安娜