Reputation: 83
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)
In the following plot: How do I get rid of the redundant x-axis values for each of the factors of a i.e. "A" & "B". I mean the 4:6 are not required for "A" and similarly 1:3 for "B". What is the tweak I need to do?
Upvotes: 0
Views: 336
Reputation: 115515
facet_wrap
and facet_grid
both have a scales
argument that let you define which of the x
and/ or y
scales should be free or fixed.
In your case, you want the x
dimensions to be free to be different in both facets, therefore
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')
Upvotes: 1