Usobi
Usobi

Reputation: 1856

setting breaks in ggplot2 histogram

I need to make several histograms regarding the same vector of values and a density estimation. So the next plot is good.

 values = rnorm(100)
 plot = ggplot(data.frame(val=values), aes(x=val)) + geom_histogram(aes(y = ..density..)) + geom_density()

However, I need to print several plots (not one plot with different panels) with different break points, say:

breaks = list(c(-1,0,1),c(-2,-1.5,0,1.5,2),c(-0.5,0,0.5))

How can I redefine the breaks for the variable plot?

Upvotes: 10

Views: 25480

Answers (1)

Jaap
Jaap

Reputation: 83215

Using your own code, you can do that with:

ggplot(data.frame(val=values), aes(x=val)) + 
  geom_histogram(aes(y = ..density..)) + 
  geom_density() +
  scale_y_continuous(breaks=c(-2,-1.5,0,1.5,2))

Upvotes: 7

Related Questions