1

I have a basic problem with the geom_histogram function

With the dataset:

df <- data.frame(value = factor( rep(c("A","B"), c(100,200) )))

I create a histogram with:

ggplot(df, aes(x=value, fill = factor(value))) + geom_histogram()

and the output is a histogram with count 100 for A and 200 for B

If I instead plot the density with:

ggplot(df, aes(x=value, fill = factor(value), ..density..)) + geom_histogram()

the output is a histogram with density 1 for A and 1 for B. I assume the reason is that the density is calculated on A and B separately.

The histogram created with:

ggplot(df, aes(x=value, group = 1, fill = factor(value),..density..)) + geom_histogram()

Is a histogram where A is 0.33 and B is 0.66, but the fill color is black, and I cannot find a way to get the fill colors used in the previous histograms in this version of the plot.

How do I generate the last version of the histogram with fill colors based on factor(value)?

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
user1570707
  • 113
  • 1
  • 6

1 Answers1

4

I solved the problem with:

ggplot(df, aes(x=value, fill = factor(value))) + 
 geom_histogram(aes(y=..count../sum(..count..)))
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
user1570707
  • 113
  • 1
  • 6