Question Details

No question body available.

Tags

r ggplot2 histogram density-plot

Answers (2)

August 16, 2025 Score: 2 Rep: 167,856 Quality: Medium Completeness: 100%

Two things:

  • if you replace afterstat(count) in your histogram with afterstat(density), it makes everything easy, and you can remove the aes(y=) from your density plots;
  • I'm changing two things you do that have been deprecated (since ggplot23.4.0): ..density.. to afterstat(density), and size= (for lines) to linewidth=

I've renamed the datasets for clarity here,

df1
August 16, 2025 Score: 1 Rep: 78,127 Quality: Medium Completeness: 80%

Put y = afterstat(density) in the initial call to ggplot and all layers will have the same y axis coordinates.

I have also defined a custom theme, to get theme out of the way, it's secondary to the question.

library(ggplot2)
library(scales)

theme
so_q79737184