2

In the dataframe below, I would expect the y axis values for density be 0.6 and 0.4, yet they are 1.0. I feel there is obviously something extremely basic that I am missing about the way I am using ..density.. but am brain freezing. How would I obtain the desired behavior using ..density.. Any help would be appreciated.

df <- data.frame(a = c("yes","no","yes","yes","no"))
m <- ggplot(df, aes(x = a))
m + geom_histogram(aes(y = ..density..))

enter image description here

Thanks, --JT

JimmyT
  • 1,099
  • 4
  • 10
  • 15
  • 3
    add inside aes `group=1`. At the moment, `yes` and `no` belong to different groups. – Arun May 04 '13 at 11:41
  • Thanks again Arun. I would like to mark this as solved but I think you need to put your response as an answer? – JimmyT May 04 '13 at 12:47
  • you can write the answer yourself Jimmy and mark it as answered. – Arun May 04 '13 at 12:48

1 Answers1

3

As per @Arun's comment:

At the moment, yes and no belong to different groups. To make them part of the same group set a grouping aesthetic:

m <- ggplot(df, aes(x = a , group = 1))   # 'group = 1' sets the group of all x to 1
m + geom_histogram(aes(y = ..density..)) 
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
JimmyT
  • 1,099
  • 4
  • 10
  • 15