簡體   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