1

This should be too damn easy, but how do I get a title onto this ggplot?

ggplot(df, aes(x = month, y = value)) + 
    geom_area(aes(color = variable, fill = variable), 
              alpha = 0.5, position = position_dodge(0.8)) +
    scale_color_manual(values = c("#00AFBB", "#E7B800")) +
    scale_fill_manual(values = c("#00AFBB", "#E7B800"))
markus
  • 25,843
  • 5
  • 39
  • 58

1 Answers1

2

Definitely this has been asked before... but at least it's easy to answer. You have two simple options:

plot + labs(title="Your Title Here")

or:

plot + ggtitle("Your Title Here")

Note that the first way using labs() allows you to access many of the relevant pieces of text in your plot, summarizing below in the code piece:

plot + labs(
  title="Plot Title",
  subtitle="Plot Subtitle",
  caption="Plot caption",
  x="X axis title",
  y="Y axis title",
  color="Legend Title for color", fill="Legend Title for fill",...)
chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • Thanks for getting a novice one step further! Any general explanation for why the plot title instructions might not be included inline with the ggplot instructions? – Doug Mounce Sep 22 '20 at 15:59
  • It all has to do with the philosophy of `ggplot2`, which is built around what is called the [Grammar of Graphics](https://towardsdatascience.com/a-comprehensive-guide-to-the-grammar-of-graphics-for-effective-visualization-of-multi-dimensional-1f92b4ed4149). Basically, it's a layered approach to building data visualizations, starting with your data as the base layer and building from that your aesthetics, the summary statistics, the shapes/things to draw ("geoms"), the coordinate system, and finally, the actual plot. This is why you don't have it all baked into one function. – chemdork123 Sep 23 '20 at 18:48
  • Ah, got it, thanks again. I do use a plot + command to add a stat_smooth line, but ggplot apparently doesn't like to keep layering p + instructions? I get one or the other, but not both. – Doug Mounce Sep 23 '20 at 21:12
  • Hm... not sure what you mean. You should be able to add as many functions as you like using `+` in between them all. So `plot + geom_smooth() + ggtitle() + theme() +.... + ...`. Maybe I'm not understanding what you mean here. – chemdork123 Sep 24 '20 at 02:28
  • yup, that worked, I have to take some aspirin before assimilating the correct use of "+" and ")", but your code returns the two functions I wanted to include - thanks chemdork! – Doug Mounce Sep 24 '20 at 21:12