如何在同一列内制作箱线图来表示土柱

How to make box plots within the same column to represent the soil column

我正在尝试使用箱形图展示地下不同深度的土壤类型(土柱)。但是,由于采样间隔不一致,样本之间也存在间隙。

我的问题如下:

  1. 是否可以将箱形图放在同一列中?即 1 个直列中的所有箱形图

  2. 使用ggdraw时是否可以删除x轴标签和刻度?我试图在使用 plot 时将其删除,但当我使用 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'))

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

您可以像这样使用 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())

编辑:我认为你根本不需要 cowplot,如果这是你想要的情节:

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")