6

I know that i can fit a density curve to my histogram in ggplot in the following way.

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

histogram

However, I want my yaxis to be frequency(counts) instead of density, and retain a curve that fits the distribution. How do I do that?

chang02_23
  • 284
  • 1
  • 4
  • 10
  • The density will always be on a different scales than the counts since the density has to integrate to 1 by definition. So you are basically asking to have a plot with two different y scales which isn't possible in ggplot2 (at least not easily -- you can search this site to find examples). Why do you think you need both a histogram and an kernel density estimation? You usually go with one or the other. – MrFlick Jul 24 '15 at 23:04

1 Answers1

5

Depending on your goals, something like this may work by just scaling the density curve using multiplication:

ggplot(df, aes(x=x)) + geom_histogram() + geom_density(aes(y=..density..*10))

or

ggplot(df, aes(x=x)) + geom_histogram() + geom_density(aes(y=..count../10))

Choose other values (instead of 10) if you want to scale things differently.

Edit:

Since you are defining your scaling factor in the global environment, you can define it within aes:

ggplot(df, aes(x=x)) + geom_histogram() + geom_density(aes(n=n, y=..density..*n)) 
# or
ggplot(df, aes(x=x, n=n)) + geom_histogram() + geom_density(aes(y=..density..*n))

or another, less nice way using get:

ggplot(df, aes(x=x)) + 
  geom_histogram() + 
  geom_density(aes(y=..density.. * get("n", pos = .GlobalEnv)))
Community
  • 1
  • 1
Jota
  • 17,281
  • 7
  • 63
  • 93
  • i'm determining a scaling variable n outside the ggplot function. Then i call my ggplot function `ggplot(df, aes(x=x)) + geom_histogram() + geom_density(aes(y=..density..*n))` I get an error Error in eval(expr, envir, enclos) : object 'n' not found . Is there a way to pass in the scaling variable into geom_density? – chang02_23 Jul 26 '15 at 01:33