Stefan
Stefan

Reputation: 221

ploting DENSITY histograms with ggplot

how can I simultaneously plot as two histograms the density of values in these two vectors:

interactors1 = c(-6.4, -3.7, -7.7, -4.6, -2.0, -5.5, -3.3, -5.8, -5.0, -4.5,
                  3.2, -0.1, -3.0, -9.2, -3.1, -8.5, -5.4, -9.1, -7.7,  2.2,
                  1.7,  3.4, -8.6, -0.5, -8.1)

and

noninteractors1 = c(-1, 0.1, 2.7, 0.4, 4.3)

Before you ask, yes I did check out this post

I want to use ggplot and not hist, because the plots look much better. When I melt the data into a data frame and plot counts everything is fine - I get this

interactors=data.frame(interactors1,noninteractors1)

ggplot(melt(interactors), aes(value, fill = variable)) 
      + geom_histogram(position = "dodge")

However, I don't need counts, I need densities.

When I do

ggplot(melt(interactors), aes(value, fill = variable)) 
   + geom_histogram(aes(y=..density..),position = "dodge")

I get a corny result . That can't be right because the sum of the densities*bins exceeds 1. What am I doing wrong? Any help would be appreciated.

P.S. I tried posting the plots, but it's not letting me...

Upvotes: 2

Views: 3740

Answers (3)

Rory T
Rory T

Reputation: 21

If you don't want to set binwidth = 1, you can multiply the y value by the binwidth. For example:

m + geom_histogram(binwidth = 0.5, aes(y = (..density..)*0.5))

This allows you to vary the binwidth and create frequency histograms with the proper scale.

Upvotes: 2

Stefan
Stefan

Reputation: 221

Thanks to Jaap and Philippe for their replies, much appreciated. I found what I was looking for on Google, but will post it here as well - the more info there is, the better.

In order for the sum of the densities to equal 1, binwidth=1 must be specified (thanks Jaap). When the bins are not equal to 1, the sum of the densities is not equal to 1. Rather, the sum of the products of density*bin width equals 1. When binwidth=1, the height of each rectangle (the density) is equal to the probability of your variable having a value=x. When binwidth!=1, the probability of that is equal to the area of the rectangle, i.e. density*width of the rectangle/the size of the bin/.

Cheers everyone. :)

Upvotes: -2

Philippe
Philippe

Reputation: 204

Try this

data=(melt(interactors))
ggplot(data, aes(x=value, fill=variable)) + geom_histogram(aes(y=..density..), binwidth = 1)

Upvotes: 1

Related Questions