user13424
user13424

Reputation: 121

geom_histogram and geom_blank: controlling y limits

I want to create a set of histograms that will make use of facet_wrap. The y limits vary a lot, sometimes there is no data for a given combination. So, I thought I could control the y limits using a dummy data frame and geom_blank.

Geom_blank() works as expected when I use geom_point() but not when when I use geom_histogram()

For example, I create these two data frames

test  <- data.frame(grp=rep(c("a", "b"),3), x.value=rep(c(10,20,30), each=2), 
         y.value=rep(1:3, each=2))
dummy <- data.frame(grp=rep(c("a", "b"),3),  x.value=rep(c(10,20,30), each=2),  
         y.value=c(0,10,10,20,20,30))  

If I want to do a scatterplot:

p1 <- ggplot(test, aes(x=x.value, y=y.value)) + geom_point()
p1 <- p1 + facet_wrap(~grp, scales="free")
p1

p2 <- p1 + geom_blank(data=dummy)
p2  

geom_blank() changed the y limits.

But when I try to create a histogram

p3 <- ggplot(test, aes(x=x.value, weight=y.value)) + geom_histogram(bins=6)
p3 <- p3 + facet_wrap(~grp, scales="free")
p3

p4 <- p3 + geom_blank(data=dummy)
p4

geom_blank() has no impact.

How can I control complex y limits on a set of histograms?

Upvotes: 0

Views: 355

Answers (1)

markus
markus

Reputation: 26373

You need to map y.value to y like you did in p1 and p2.

p4 <- p3 + geom_blank(aes(y = y.value), data=dummy)
p4

enter image description here

Upvotes: 2

Related Questions