Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting breaks in ggplot2 histogram

Tags:

r

ggplot2

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?

like image 509
Usobi Avatar asked Jan 09 '14 21:01

Usobi


People also ask

How to set Axis breaks in ggplot2?

How to Set Axis Breaks in ggplot2 (With Examples) You can use the following syntax to set the axis breaks for the y-axis and x-axis in ggplot2: #set breaks on y-axis scale_y_continuous (limits = c (0, 100), breaks = c (0, 50, 100)) #set breaks on y-axis scale_x_continuous (limits = c (0, 10), breaks = c (0, 2, 4, 6, 8, 10))

How do I set the number of bins in ggplot2 histograms?

By default, ggplot2 will automatically pick a certain number of bins to use in the histogram. However, we can use the following syntax to specify that we want the histogram to use 10 bins: Notice that the histogram now has exactly 10 bins. Or we could use the following syntax to specify that we want the histogram to use 5 bins:

How do you find the number of breaks in a histogram?

Histogram breaks in R. breaks argument. Plug in selection. The hist function uses the Sturges method by default to determine the number of breaks on the histogram. This selection is very important because too many bins will increase the variability and few bins will group the data too much.

How to create histograms with the ggplot2 package in R?

You have two options to create your histograms with the ggplot2 package. On the one hand, you can use the qplot () function, which looks very much like the hist () function:


1 Answers

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))
like image 111
Jaap Avatar answered Oct 18 '22 18:10

Jaap