2014-09-03 76 views
0
library(ggplot2) 
set.seed(2) 
a = sort(rep(c("A","B"),6)) 
b = c(rep(1:3,2),rep(4:6,2)) 
cc = rnorm(length(a)) 
d = rep(sort(rep(1:2,3)),2) 
df = data.frame(a,b,cc,d) 
print(df) 
ggplot(df, aes(x = as.factor(b), y = cc, fill = as.factor(d))) + 
    geom_bar(stat = "identity", position = "dodge") + 
    facet_wrap(~a) 

在以下情节: 如何除掉冗余x轴值的每一个的,即“A” &“B”的因素。 我的意思是“A”不需要4:6,而“B”也是1:3。 我需要做些什么调整?ggplot去除冗余x轴值

回答

1

facet_wrapfacet_grid都有一个scales说法,让你定义该x和/或y尺度应该是免费的或固定的。

在你的情况,你想x尺寸可以自由地在两个方面有所不同,因此

ggplot(df, aes(x = as.factor(b), y = cc, fill = as.factor(d))) + 
    geom_bar(stat = "identity", position = "dodge") + 
    facet_wrap(~ a, scales = 'free_x') 
+0

哇它是那么容易......很抱歉打扰应该有更多。愚蠢的我。 – 2014-09-03 10:16:45