ggplot faceting - 删除空的x轴标签

use*_*440 1 r ggplot2

我在本文后面使用了faceting,除了使用某个数据子集删除一些行.

# create a dataset
specie=c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition=rep(c("normal" , "stress" , "Nitrogen") , 4)
value=abs(rnorm(12 , 0 , 15))
data=data.frame(specie,condition,value)

# remove some rows
data=data[c(1:2,5:6,7,9,11:12),]

# Grouped
ggplot(data, aes(fill=condition, y=value, x=specie)) + 
  geom_bar(position="dodge", stat="identity")

# Faceting
ggplot(data, aes(y=value, x=specie, color=specie, fill=specie)) + 
  geom_bar( stat="identity") +    
  facet_wrap(~condition)
Run Code Online (Sandbox Code Playgroud)

这给出了如预期的以下图.我需要从下面的每个图中删除空标签 - 例如,sorgho从第一个图poaceetriticum第二个图等等.

在此输入图像描述

ngh*_*ran 6

您需要将scales参数添加到facet_wrap().尝试

# Faceting
ggplot(data, aes(y=value, x=specie, color=specie, fill=specie)) + 
  geom_bar( stat="identity") +    
  facet_wrap(~condition, scales = "free")
Run Code Online (Sandbox Code Playgroud)