xxx
xxx

Reputation: 307

how to make the histogram fill option work

I want to see the distribution difference between two groups in my data. My data looks similar to

data <- data.frame(rnorm(500,mean = 3),sample(c(0,1), replace=TRUE, size=500))
colnames(data) <- c('x','y')

And then I draw the histogram by

ggplot(data, aes(x = x, fill = y)) +
   geom_histogram(alpha=0.6, bins = 50, position = 'identity')

But it returns

enter image description here

Can you please help find what the problem is? I just followed the examples from googling. Thank you!

Upvotes: 2

Views: 427

Answers (1)

dww
dww

Reputation: 31452

Assuming you want y as discrete groups, you should convert it to a factor

ggplot(data, aes(x = x, fill = factor(y))) +
  geom_histogram(alpha=0.6, bins = 50, position = 'identity')

enter image description here

Upvotes: 3

Related Questions