r - ggplot2中带空格的图例标签
问题描述
我正在使用 ggplot2 绘制一个非常简单的数据框,但我正在努力让它的图例像我想要的那样显示。这是数据框:
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
推荐阅读
- java - spring-cloud-stream 的 DLQ 的分区 Kafka 主题
- python-3.x - Pandas 重新采样/分组按星期几和年份
- azure - 我们可以在 azure 托管的 Web api 中拥有的类对象的内存限制?
- python - 如何使用 .grid 方法将滚动条添加到 tkinter 文本小部件
- python - 为什么即使字符串匹配也不起作用
- c# - Windows 上的 Kestrel 没有响应
- google-apps-script - 选择一个范围,点击谷歌表格中的组合值
- c# - 了解 Visual Studio Web 应用程序中的项目类型
- xamarin.forms - 如何使用 Xamarin.Forms 自定义渲染器在 ButtonOnClick() 中执行平台 (Android) 特定方法。
- javascript - 如何从这个 json 数据中创建两个数组?