Reputation: 307
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
Can you please help find what the problem is? I just followed the examples from googling. Thank you!
Upvotes: 2
Views: 427
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')
Upvotes: 3