2014-09-03 62 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