使用 R 将图例添加到结合 geom_line 和 geom_shape 的 ggplot
Posted
技术标签:
【中文标题】使用 R 将图例添加到结合 geom_line 和 geom_shape 的 ggplot【英文标题】:Adding a legend to a ggplot that combines geom_line and geom_shape using R 【发布时间】:2020-07-26 19:29:42 【问题描述】:我有以下 MWE:
library(scales)
library(tidyverse)
Canada <- c(383.927, 387.088, 390.648, 393.926, 395.947, 393.98, 388.772,
392.391, 391.804, 389.321, 390.583, 390.062, 390.596, 392.19,
392.267, 397.572, 397.807, 394.64, 391.737, 392.659, 392.483,
392.012, 391.842, 394.06, 391.661, 390.621, 392.533, 396.218,
401.802, 397.298, 392.468, 392.056, 394.752, 392.947, 392.606,
391.839, 392.169, 393.29, 399.993, 396.114, 403.1, 398.263, 395.066,
397.16, 399.562, 396.865, 392.898, 396.89, 398.529, 402.269,
-9.999e+09, 398.294, 401.033, 399.328, -9.999e+09, 400.062, 395.829,
397.754, 395.306, 394.87, 398.469, 399.91, 405.053, 404.678,
402.185, 396.605, -9.999e+09, 402.252, 405.295, 401.08, 400.527,
398.38, 400.152, 396.42, 402.497, 406.855, 403.56, -9.999e+09,
-9.999e+09, 405.773, 402.306, 403.146, 403.079, 400.712)
Canada1 <- c(390.704083333333, 393.322083333333, 393.900083333333, 396.780833333333,
398.3274, 401.312181818182, 402.45)
x.axis <- c("Jan. 2010","","","","","July 2010","","","","","",
"Dec. 2010","","","","","","July 2011","","","","","",
"Dec. 2011","","","","","","July 2012","","","","","",
"Dec. 2012","","","","","","July 2013","","","","","",
"Dec. 2013","","","","","","July 2014","","","","","",
"Dec. 2014","","","","","","July 2015","","","","","",
"Dec. 2015","","","","","","July 2016","","","","","",
"Dec. 2016","")
# Edit
d1 <- as.Date(paste0("201001","01"), "%Y%m%d")
d2 <- as.Date(paste0("201612","01"), "%Y%m%d")
date1 <- format(seq(d1,d2,by="month"), "%Y%m%d")
datCan <- data.frame(co2 = Canada, labels = as.character(time), date = date1, group = 1)
d1 <- as.Date(paste0("201006","01"), "%Y%m%d")
d2 <- as.Date(paste0("201606","01"), "%Y%m%d")
date2 <- format(seq(d1,d2,by="year"), "%Y%m%d")
dat2Can <- data.frame(co2 = Canada1, date = date2, group = 2)
Canplot <- ggplot() + geom_line(data = datCan, aes(x = date, y = co2, group = group))
+ geom_line(data = dat2Can, linetype = "dashed", aes(x = date, y = co2, group = group), color = "Red")
+ geom_point(data = dat2Can, size=2, aes(x = date, y = co2, group = group), color = "Red")
+ ylim(380,410) + scale_x_discrete(labels = x.axis)
+ theme_classic(base_size=12) + ylab("yval")
+ xlab("Time") + theme(axis.text.x = element_text(angle = 45, hjust=1))
哪个产生
我想为这个情节添加一个图例,但由于我包含了geom_line
和geom_point
而遇到了麻烦。我相信我需要这样做,因为这些点位于特定位置。关于我如何做到这一点的任何想法?我愿意创建一个独立的图例,它只是一条黑线和红色虚线,中间有一个圆圈。这会很好用,因为无论如何我都在使用 LaTeX 来格式化我的文档。
【问题讨论】:
您可以访问this 您的 cdoe 在我的会话中没有生成相同的图表,您确定“-9.999e+09”这个值吗? @dc37 抱歉,我忘记了一大段代码,请参阅上面的编辑 在你的例子中,你的datCan
中的time
是什么?
【参考方案1】:
要为不同的数据框获取两个不同的图例,您可以使用 ggnewscale
包中的 new_color_scale
函数。
在这里,我使用 lubridate
将日期序列添加到您的单个向量的线和点,然后我从这些创建数据框:
library(lubridate)
date_seq = seq(ymd("2010-01-01"),ymd("2016-12-01"),length.out = length(Canada))
date_seq_point = seq(ymd("2010-06-01"),ymd("2016-06-01"),length.out = length(Canada1))
Can <- data.frame(Canada = Canada,
Xaxis = date_seq,
dataset = "Lines")
Can_point = data.frame(Canada = Canada1,
Xaxis = date_seq_point,
dataset2 ="Points")
现在,您可以使用这些新数据框进行绘图,并在调用 new_color_scale
时为线和点获取适当的图例:
library(ggnewscale)
library(ggplot2)
ggplot()+
geom_line(data = Can, aes(x = Xaxis, y = Canada, color = dataset))+
scale_color_manual(name = "Lines",values = "black")+
new_scale_color()+
geom_line(data = Can_point, aes(x = Xaxis, y = Canada, color = dataset2))+
geom_point(data = Can_point, aes(x = Xaxis, y = Canada, color = dataset2))+
scale_color_manual(name = "Points",values = "red")+
ylim(380,410)+
scale_x_date(date_breaks = "6 months", date_labels = "%b %Y")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
编辑:一个传说中的两个不同传说
根据 OP 的要求,目标是为线和点保留一个图例,但线图例中不显示点。
因此,您可以通过以下方式将两个数据帧绑定在一起:
library(lubridate)
date_seq = seq(ymd("2010-01-01"),ymd("2016-12-01"),length.out = length(Canada))
date_seq_point = seq(ymd("2010-06-01"),ymd("2016-06-01"),length.out = length(Canada1))
Can <- data.frame(Canada = Canada,
Xaxis = date_seq,
dataset = "Lines")
Can_point = data.frame(Canada = Canada1,
Xaxis = date_seq_point,
dataset ="Points")
DF <- rbind(Can, Can_point)
然后,您可以在每个数据集的函数中绘制线和点,但通过将shape
参数添加到aes
来控制点的形状。然后,可以在scale_shape_manual
中设置黑点的形状为NA:
library(ggplot2)
ggplot(DF, aes(x = Xaxis, y = Canada, color = dataset, shape = dataset))+
geom_point()+
geom_line()+
ylim(380,410)+
scale_color_manual(values = c("black","red"))+
scale_shape_manual(values = c(NA,16))
它回答了你的问题吗?
【讨论】:
是的,我得去看看它是否与我的代码兼容。虽然,两个图例条目中都有一个点,第一个应该只是一条黑线,第二个应该是一个带有点的红色虚线 我相应地编辑了我的答案。让我知道它是否适合您。 我不想要两个单独的图例,只需要一个包含您那里的条目的图例!所以只有一个标题为“传奇”,然后点和线完全符合你的要求 请检查我编辑的答案。让我知道它是否是您正在寻找的。span>以上是关于使用 R 将图例添加到结合 geom_line 和 geom_shape 的 ggplot的主要内容,如果未能解决你的问题,请参考以下文章
在 geom_line 图中显示多个分组 geom_hline 的图例
R语言ggplot2可视化:使用geom_line函数将dataframe中数据可视化为时间序列(或折线图)(Time Series Plot From a Data Frame)添加标题副标题