I want to plot a histogram with 10 bins evenly spaced from 0 to 1.
I can get that with:
require(ggplot2)
require(scales, quiet=TRUE)
df = data.frame(x = (0.5 + seq(0, 30))/20)
##
## produces what I want: 10 bins evenly spaced from 0 to 1 all with count=2
##
ggplot(df, aes(x=x))+
geom_histogram(breaks=seq(0, 1, length.out=10+1)) +
scale_x_continuous(breaks=pretty_breaks(10), limits=c(0,1)) +
coord_cartesian(xlim = c(0, 1)) +
theme_bw()
(I am aware that scale_x_continuous/limits and coord_cartesian/xlim are similar/overlapping statements)
Is there another simpler method to get what I am looking for?
I have tried as follows, but I don't obtain the bins as I would expect to:
##
## produces n bins, all count=2, from 0.05 to 0.95
##
ggplot(df, aes(x=x))+
geom_histogram(binwidth=1/10) +
scale_x_continuous(breaks=pretty_breaks(10), limits=c(0,1)) +
coord_cartesian(xlim = c(0, 1)) +
theme_bw()
##
## produces n bins, 2 with count=3, the other ones with count=2
## from 0.05 to 0.95
##
ggplot(df, aes(x=x))+
geom_histogram(bins=10) +
scale_x_continuous(breaks=pretty_breaks(10), limits=c(0,1)) +
coord_cartesian(xlim = c(0, 1)) +
theme_bw()
Here is my environment:
platform x86_64-redhat-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 3.0
year 2016
month 05
day 03
svn rev 70573
language R
version.string R version 3.3.0 (2016-05-03)
nickname Supposedly Educational
packageVersion("ggplot2")
[1] ‘2.1.0’
Best regards,
--
Alberto