ggplot刻面-删除空的x轴标签

问题描述:

我在文章除了使用数据的特定子集删除一些行之外.

I' using faceting following this article except for using a certain subset of the data removing some rows.

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

这给出了预期的以下绘图.我需要从下面的每个图中删除空标签-例如,首先是sorgho,第二是poaceetriticum,依此类推.

This gives the following plot as expected. I need to remove the empty labels from each of the plots below - for example, sorgho from first, poacee and triticum from second and so on.

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

You need to add the scales argument to facet_wrap(). Try

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