HIstogram overlay ggplot

Hi.
I have some continuous variables and I have to draw some histograms.
Does anyone know how to plot 2 overlay histogram in the same graph?

For example, these are my commands for the two histograms

ggplot(data=DATA, aes(x=variable1)) +
geom_histogram(aes(y = stat(count) / sum(count)) , bins = 60,
col="red",
fill="navy",
alpha = .5) +
scale_y_continuous(name = "Percentage", labels = scales::percent) +
scale_x_continuous (name = "" , limits = c(0,150) , breaks = seq(0, 150 , 30) ) +
labs(title= " ") +
theme_bw(base_size = 20) +
theme(plot.title = element_text(hjust = 0.5))

ggplot(data=DATA, aes(x=variable2)) +
geom_histogram(aes(y = stat(count) / sum(count)) , bins = 60,
col="red",
fill="navy",
alpha = .5) +
scale_y_continuous(name = "Percentage", labels = scales::percent) +
scale_x_continuous (name = "" , limits = c(0,150) , breaks = seq(0, 150 , 30) ) +
labs(title= " ") +
theme_bw(base_size = 20) +
theme(plot.title = element_text(hjust = 0.5))

Is there anyway to generate one single chart with histogram of variable 1 and variable 2 overlaid?

Thanks

Reshape your data frame into a long format and map the indexing variable to a suitable aesthetic like fill for example.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.