简体   繁体   中英

Legend labels with spaces in ggplot2

I am plotting a very simple dataframe using ggplot2 but am struggling to get the legend for it to display like I want it to. Here's the dataframe:

summaryData <- data.frame (year  = c(1999, 2002, 2005, 2008), totalEmissions = c(603, 565, 570, 358))

The big picture is that I am plotting the observed data and a linear regression line and would like the labels in the legend to read "Observed data" and "Linear regression." After some searching, I figured out how to get the point and line to display correctly in the legend--I found this webpage to be particularly helpful, although any comments on this would be welcome--but I still can't figure out how to have labels that include spaces. My code is below.

# Construct the plot
p <- summaryData %>%
    ggplot(mapping = aes(x = year,
                         y = totalEmissions)) +
    geom_point(mapping = aes(alpha = "Observed"),
               shape = 19,
               size = 3,
               show.legend = TRUE) +
    geom_smooth(mapping = aes(alpha = "Regression"),
                color = "blue",
                method = "lm",
                se = FALSE,                         # No confidence interval
                show.legend = TRUE) +               
    theme_classic() +
    theme(legend.position = c(0.175, 0.5),
          plot.title = element_text(hjust = 0.5)) + # Horizontally center title
    scale_alpha_manual(name = NULL,
                       values = c(1, 1),
                       breaks = c("Observed", "Regression"),
                       guide = guide_legend(override.aes = list(linetype = c(0, 1),
                                            shape = c(19, NA),
                                            color = c("black", "blue")))) +
    scale_x_continuous(expand = c(0, 0),            # No space between data and axis
                       limits = c(1998, 2009),
                       breaks = seq(from = 1999, to = 2007, by = 2)) +
    scale_y_continuous(expand = c(0, 0),            # No space between data and axis
                       limits = c(300, 725), 
                       breaks = seq(from = 400, to = 700, by = 100)) +
    labs(title = "Coal PM2.5 vs. Year (US)",
         subtitle = "PM2.5 pollution produced by coal decreased from 1999 to 2008",
         x = "Year",
         y = expression(paste("PM2.5 (tons × 10"^"-3",")")))

print(p)

All you need to do is to specify labels and type whatever you like to appear in the legend. For example:

library(ggplot2)[![enter image description here][1]][1]
summaryData <- data.frame (year  = c(1999, 2002, 2005, 2008), totalEmissions = c(603, 565, 570, 358))
# Construct the plot
p <- summaryData %>%
        ggplot(mapping = aes(x = year,
                             y = totalEmissions)) +
        geom_point(mapping = aes(alpha = "Observed"),
                   shape = 19,
                   size = 3,
                   show.legend = TRUE) +
        geom_smooth(mapping = aes(alpha = "Regression"),
                    color = "blue",
                    method = "lm",
                    se = FALSE,                         # No confidence interval
                    show.legend = TRUE) +               
        theme_classic() +
        theme(legend.position = c(0.175, 0.5),
              plot.title = element_text(hjust = 0.5)) + # Horizontally center title
        scale_alpha_manual(name = NULL,
                           labels = c("Observed data", "Linear regression"),
                           values = c(1, 1),
                           breaks = c("Observed", "Regression"),
                           guide = guide_legend(override.aes = list(linetype = c(0, 1),
                                                                    shape = c(19, NA),
                                                                    color = c("black", "blue")))) +
        scale_x_continuous(expand = c(0, 0),            # No space between data and axis
                           limits = c(1998, 2009),
                           breaks = seq(from = 1999, to = 2007, by = 2)) +
        scale_y_continuous(expand = c(0, 0),            # No space between data and axis
                           limits = c(300, 725), 
                           breaks = seq(from = 400, to = 700, by = 100)) +
        labs(title = "Coal PM2.5 vs. Year (US)",
             subtitle = "PM2.5 pollution produced by coal decreased from 1999 to 2008",
             x = "Year",
             y = expression(paste("PM2.5 (tons × 10"^"-3",")")))

p

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM