2016-04-07 24 views
2

我试图用箱形图在地面不同深度处演示土壤类型(土柱)。但是,由于采样间隔不一致,样本之间也存在间隙。如何在同一列内制作箱形图以表示土壤柱

我的问题如下:

  1. 是否有可能把同一列中的箱线图?即在1个直列中的所有盒子图

  2. 当使用ggdraw时,是否可以删除x轴标签和刻度?我试图在使用情节时将其删除,但当我使用ggdraw时再次出现。

我的代码如下所示:

SampleID <- c("Rep-1", "Rep-2", "Rep-3", "Rep-4") 
From <- c(0,2,4,9) 
To <- c(1,4,8,10) 
Mid <- (From+To)/2 
ImaginaryVal <- c(1,1,1,1) 
Soiltype <- c("organic", "silt","clay", "sand") 
df <- data.frame(SampleID, From, To, Mid, ImaginaryVal, Soiltype) 

plot <- ggplot(df, aes(x=ImaginaryVal, ymin=From, lower=From,fill=Soiltype, 
      middle=`Mid`, upper=To, ymax=To)) + 
      geom_boxplot(colour= "black", stat="identity") +        scale_y_reverse(breaks = seq(0,10,0.5)) + xlab('Soiltype') +     ylab('Depth (m)') + theme(axis.text.x = element_blank(),     axis.ticks.x = element_blank()) 

ggdraw(switch_axis_position(plot + theme_bw(8), axis = 'x')) 

enter image description here

在图像我已经指出了我想要的东西,用红色箭头和线条。

回答

4

您可以使用position = position_dodge()像这样:

plot <- ggplot(df, aes(x=ImaginaryVal, ymin=From, lower=From,fill=Soiltype, middle=Mid, upper=To, ymax=To)) + 
    geom_boxplot(colour= "black", stat="identity", position = position_dodge(width=0)) + 
    scale_y_reverse(breaks = seq(0,10,0.5)) + 
    xlab('Soiltype') + 
    ylab('Depth (m)') + 
    theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) 

enter image description here

编辑:我不认为你需要cowplot可言,如果这是你希望你的阴谋是什么样子:

enter image description here

ggplot(df, aes(x=ImaginaryVal, ymin=From, lower=From,fill=Soiltype, middle=Mid, upper=To, ymax=To)) + 
    geom_boxplot(colour= "black", stat="identity", position = position_dodge(width=0)) + 
    scale_y_reverse(breaks = seq(0,10,0.5)) + 
    xlab('Soiltype') + 
    ylab('Depth (m)') + 
    theme_bw() + 
    theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) + 
    xlab("") + 
    ggtitle("Soiltype") 
+0

我的天啊。我无法用语言来描述我对你的回答感到满意和满意。这完全解决了我的问题。非常感谢@beetroot。 – Rupesh

+0

另外,有没有一种方法可以删除每个盒子中的“中”线? – Rupesh

+1

很高兴提供帮助,但也许你最好使用'geom_rect'而不是'geom_boxplot'?尝试'ggplot(df,aes(xmin = 1,xmax = 3,ymin = From,fill = Soiltype,ymax = To))+ geom_rect(color =“black”)'... – beetroot