Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • This wasn't exactly what I was trying to achieve, but it might have to suffice if there is no way to overlay multiple line graphs onto a bar plot using ggplot2. Thank you for your help! Commented Sep 1, 2017 at 3:38
  • When I coerce the age variable to be a factor instead of numeric, I encounter problems when trying to add additional lines using the geom_smooth and geom_line functions. Might you know a work around for this? (I updated my question above.) Commented Sep 2, 2017 at 18:04
  • I think the issue is that you changed age to numeric in the geom_line() part of your code, but not in the ggplot() part. Commented Sep 3, 2017 at 22:30
  • Yes, I imagine that must be a problem. But, I am trying to keep age as a factor in the ggplot() part since I don't want to have large spaces between 30, 40, 50, and 60. But, it appears that age needs to be numeric in the geom_line() part. Is there a way to work around that? i.e. to not have those big spaces between 30, 40, 50, and 60 that show up when age is numeric? Commented Sep 4, 2017 at 15:40
  • In your new code example, the sum of M + F is given by the total height of the stacked bars. I'm not clear why a line is necessary. However: I have edited my original answer with some code which uses stat_summary to overlay the line on the bars, as per your original question. Personally, I don't think lines on top of bars is a good visualisation technique. Commented Sep 4, 2017 at 23:06