繁体   English   中英

ggplot2 中带有空格的图例标签

[英]Legend labels with spaces in ggplot2

我正在使用 ggplot2 绘制一个非常简单的 dataframe 但我正在努力让它的图例像我想要的那样显示。 这是 dataframe:

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,
                       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)

您需要做的就是指定labels并键入您希望在图例中出现的任何内容。 例如:

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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