hist distribution using ggplot2

2019-09-19 11:07发布

问题:

I plot a vector distribution with qplot that I don't know its distribution in advance, it's calculated in a function. I just know that the x values are between 0 and 1.

I use the below command line and get the attached histogram.

As the distribution is jammed, how can I make it more spread so that the distribution becomes clear? which parameters to use or other functions that produce histogram?

Moreover how to color so that the bins becomes more distiguishable?

keeping the color legend may or may not be necessary.

qplot(my.vec,binwidth = .2)+ 
  geom_histogram(binwidth = .2, aes(fill = ..count..), colour='black', fill='skyblue')

Another example:

回答1:

To "unjam" the plot, use xlim:

qplot(x) + geom_histogram(binwidth = 0.2, aes(fill = ..count..), colour='black', fill='skyblue') + xlim(0,1)

I'm not sure why you want to use variation in color here when column height is already giving you that information. That's the whole point of a histogram, and then you don't have to worry about confusing color-blind people. So why not just:

qplot(x) + geom_histogram(binwidth = 0.2, colour='black', fill='skyblue') + xlim(0,1)

Here's the toy data I used to test it:

x <- c(0.41, 0.42, 0.47, 0.47, 0.49, 0.50, 0.51, 0.55, 0.56, 0.57, 0.59, 0.61, 0.62, 0.65, 0.68, 0.69, 0.70, 0.75, 0.78, 0.79)

And here's a .png of the plot I got: