在 R 中的 ggplot2 中创建三组图例
Creating three groups of legends in ggplot2 in R
我在图表中绘制了 5 个元素:
- 3 种犯罪类型(anti-social 行为、盗窃和暴力)的发生率
- 3 个平滑的条件均值(geom_smooth() 中的线性模型)- 即针对每种犯罪类型一个,以研究失业率与每种犯罪类型之间的关系
- 1 条失业率线
所以我尝试创建三个图例标题如下:
犯罪 ("anti-social 行为", "盗窃", "暴力")
平滑条件均值("anti-social 行为"、"盗窃"、"暴力" )
无姓名 ("失业率")
到目前为止,我已经成功添加了犯罪传说。
然而,无论我把add linetype = "
放在哪里,图例中都不会出现光滑的条件手段。此外,如果我尝试在三个 geom_smooth()
之后添加 labs(linetype = "LEGEND TITLE")
,失业率图例标题会更改(如下面的输出所示)。
这是输出(虚线指的是三个 geom_smooth()
,颜色与他们正在计算平滑条件均值的犯罪类型相匹配。
这是我使用的代码;如您所见,我尝试在 aes()
内部和外部添加 linetype =
。无论如何它都不会出现在图例中。
所以问题是,我应该把 linetype = "" 放在哪里,以便显示每个平滑条件均值的名称??
ggplot(mapping=aes(Date)) +
geom_line(aes(y = Crime_occurrences, colour = Crime),
size = 1, data = cr_grouped) + #this is the crime legend and it works a charm
geom_line(lwd = 1, mapping = aes(x = Date, y = rescale(Unemployment.rate, to = out_range,
from = in_range),
linetype = "Unemployment rate"), #this shows up alright
data = ur_grouped) +
labs(linetype = "Legend") + #this shows up only if I delete labs() under the 3 geom_smooth()
geom_smooth(method = 'lm',se=F, aes(x=Date,y = Crime_occurrences, group=1),
linetype = "123456", col = "darkgreen", lty = 6, data=smooth_theft)+ #ex outside aes()
geom_smooth(method = 'lm',se=F, aes(x=Date,y = Crime_occurrences, group=1,
linetype = "ABCD"), #example inside aes()
col = "red", lty = 6, data=smooth_antisocial)+
geom_smooth(method = 'lm',se=F, aes(x=Date,y = Crime_occurrences, group=1),
col = "blue3", lty = 6, data=smooth_violence)+
labs(linetype = "Smooth Conditional Means") + #this renames the labs() created for unemployment
scale_x_date(breaks = seq(as.Date("2019-01-01"), as.Date("2020-10-01"), by="1 month"),
date_labels = '%m %Y') +
scale_y_continuous(sec.axis =
sec_axis(~ rescale(.x, to = in_range, from = out_range),
name = "Unemployment Rate (%)")) +
theme(axis.text.x=element_text(angle =- 90, vjust = 0.5))
感谢您的帮助!
输出:
cr_grouped
structure(list(Date = structure(c(17897, 17897, 17897, 17928,
17928, 17928, 17956, 17956, 17956, 17987, 17987, 17987, 18017,
18017, 18017, 18048, 18048, 18048, 18078, 18078, 18078, 18109,
18109, 18109, 18140, 18140, 18140, 18170, 18170, 18170, 18201,
18201, 18201, 18231, 18231, 18231, 18262, 18262, 18262, 18293,
18293, 18293, 18322, 18322, 18322, 18353, 18353, 18353, 18383,
18383, 18383, 18414, 18414, 18414, 18444, 18444, 18444, 18475,
18475, 18475, 18506, 18506, 18506, 18536, 18536, 18536), class = "Date"),
Crime = c("Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences"
), Crime_occurrences = c(59426L, 90521L, 66296L, 61187L,
85416L, 61955L, 67810L, 93273L, 70843L, 68936L, 87633L, 66300L,
71379L, 89408L, 69906L, 72021L, 86315L, 71564L, 83705L, 86819L,
74969L, 78481L, 83309L, 69450L, 67932L, 81552L, 68740L, 69469L,
90307L, 70696L, 60692L, 87529L, 69011L, 53728L, 82404L, 69231L,
59571L, 88523L, 70824L, 57038L, 83460L, 69155L, 70861L, 71699L,
68956L, 135439L, 44984L, 58431L, 138529L, 47934L, 65947L,
102061L, 53208L, 70050L, 88014L, 62855L, 76773L, 91811L,
65687L, 76171L, 81958L, 67925L, 72268L, 84434L, 69627L, 70698L
)), row.names = c(NA, -66L), groups = structure(list(Date = structure(c(17897,
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170,
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444,
18475, 18506, 18536), class = "Date"), .rows = structure(list(
1:3, 4:6, 7:9, 10:12, 13:15, 16:18, 19:21, 22:24, 25:27,
28:30, 31:33, 34:36, 37:39, 40:42, 43:45, 46:48, 49:51, 52:54,
55:57, 58:60, 61:63, 64:66), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 22L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
ur_grouped
structure(list(Date = structure(c(17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536
), class = "Date"), Unemployment.rate = c(4.85019120720378, 4.87826349489932,
4.72619911892442, 4.82962291533381, 4.78037962696207, 4.75317591530959,
4.53739659597437, 4.58692391687679, 4.57052637882086, 4.56628766844258,
4.60997237871127, 4.58039236078879, 4.73224909680535, 4.73446295980473,
4.62619907452877, 4.44434845370539, 4.53780319184207, 4.46033205030955,
4.57680473216631, 5.01205713295936, 5.42174180050248, 5.75623503556053
)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", "data.frame"
))
OUT_RANGE和IN_RANGE[out_range可以手动调整,我一般是out_range[2]<- X 检查最适合缩放目的的值]
out_range <-范围(cr_grouped$Crime_occurrences)
in_range <- 范围(ur_grouped$Unemployment.rate)
smooth_theft
structure(list(Date = structure(c(17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536
), class = "Date"), Crime = c("Theft", "Theft", "Theft", "Theft",
"Theft", "Theft", "Theft", "Theft", "Theft", "Theft", "Theft",
"Theft", "Theft", "Theft", "Theft", "Theft", "Theft", "Theft",
"Theft", "Theft", "Theft", "Theft"), Crime_occurrences = c(90521L,
85416L, 93273L, 87633L, 89408L, 86315L, 86819L, 83309L, 81552L,
90307L, 87529L, 82404L, 88523L, 83460L, 71699L, 44984L, 47934L,
53208L, 62855L, 65687L, 67925L, 69627L)), row.names = c(NA, -22L
), groups = structure(list(Date = structure(c(17897, 17928, 17956,
17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231,
18262, 18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506,
18536), class = "Date"), .rows = structure(list(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -22L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
smooth_antisocial:
structure(list(Date = structure(c(17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536
), class = "Date"), Crime = c("Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour"), Crime_occurrences = c(59426L,
61187L, 67810L, 68936L, 71379L, 72021L, 83705L, 78481L, 67932L,
69469L, 60692L, 53728L, 59571L, 57038L, 70861L, 135439L, 138529L,
102061L, 88014L, 91811L, 81958L, 84434L)), row.names = c(NA,
-22L), groups = structure(list(Date = structure(c(17897, 17928,
17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201,
18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444, 18475,
18506, 18536), class = "Date"), .rows = structure(list(1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -22L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
尝试下一个代码。首先安装 ggnewscale
包。我删除了最后一段代码,因为您没有共享该平滑选项的数据,但添加它不会成为问题。这里的代码:
library(ggnewscale)
#Plot
ggplot(mapping=aes(Date)) +
geom_line(aes(y = Crime_occurrences, colour = Crime),
size = 1, data = cr_grouped) +
geom_line(lwd = 1, mapping = aes(x = Date,
y = rescale(Unemployment.rate,
to = out_range,
from = in_range),
linetype = "Unemployment rate"),
data = ur_grouped) +
labs(linetype = "Legend") +
new_scale_color()+
geom_smooth(method = 'lm',se=F,
aes(x=Date,y = Crime_occurrences, group=1,
color='smooth_theft'),
lty = 6, data=smooth_theft)+
geom_smooth(method = 'lm',se=F,
aes(x=Date,y = Crime_occurrences, group=1,
color='smooth_antisocial'),
lty = 6, data=smooth_antisocial)+
labs(colour = "Smooth Conditional Means") +
scale_color_manual(values=c('darkgreen','red'))+
scale_x_date(breaks = seq(as.Date("2019-01-01"), as.Date("2020-10-01"), by="1 month"),
date_labels = '%m %Y') +
scale_y_continuous(sec.axis =
sec_axis(~ rescale(.x, to = in_range, from = out_range),
name = "Unemployment Rate (%)")) +
theme(axis.text.x=element_text(angle =- 90, vjust = 0.5))
输出:
我在图表中绘制了 5 个元素:
- 3 种犯罪类型(anti-social 行为、盗窃和暴力)的发生率
- 3 个平滑的条件均值(geom_smooth() 中的线性模型)- 即针对每种犯罪类型一个,以研究失业率与每种犯罪类型之间的关系
- 1 条失业率线
所以我尝试创建三个图例标题如下:
犯罪 ("anti-social 行为", "盗窃", "暴力")
平滑条件均值("anti-social 行为"、"盗窃"、"暴力" )
无姓名 ("失业率")
到目前为止,我已经成功添加了犯罪传说。
然而,无论我把add linetype = "
放在哪里,图例中都不会出现光滑的条件手段。此外,如果我尝试在三个 geom_smooth()
之后添加 labs(linetype = "LEGEND TITLE")
,失业率图例标题会更改(如下面的输出所示)。
这是输出(虚线指的是三个 geom_smooth()
,颜色与他们正在计算平滑条件均值的犯罪类型相匹配。
这是我使用的代码;如您所见,我尝试在 aes()
内部和外部添加 linetype =
。无论如何它都不会出现在图例中。
所以问题是,我应该把 linetype = "" 放在哪里,以便显示每个平滑条件均值的名称??
ggplot(mapping=aes(Date)) +
geom_line(aes(y = Crime_occurrences, colour = Crime),
size = 1, data = cr_grouped) + #this is the crime legend and it works a charm
geom_line(lwd = 1, mapping = aes(x = Date, y = rescale(Unemployment.rate, to = out_range,
from = in_range),
linetype = "Unemployment rate"), #this shows up alright
data = ur_grouped) +
labs(linetype = "Legend") + #this shows up only if I delete labs() under the 3 geom_smooth()
geom_smooth(method = 'lm',se=F, aes(x=Date,y = Crime_occurrences, group=1),
linetype = "123456", col = "darkgreen", lty = 6, data=smooth_theft)+ #ex outside aes()
geom_smooth(method = 'lm',se=F, aes(x=Date,y = Crime_occurrences, group=1,
linetype = "ABCD"), #example inside aes()
col = "red", lty = 6, data=smooth_antisocial)+
geom_smooth(method = 'lm',se=F, aes(x=Date,y = Crime_occurrences, group=1),
col = "blue3", lty = 6, data=smooth_violence)+
labs(linetype = "Smooth Conditional Means") + #this renames the labs() created for unemployment
scale_x_date(breaks = seq(as.Date("2019-01-01"), as.Date("2020-10-01"), by="1 month"),
date_labels = '%m %Y') +
scale_y_continuous(sec.axis =
sec_axis(~ rescale(.x, to = in_range, from = out_range),
name = "Unemployment Rate (%)")) +
theme(axis.text.x=element_text(angle =- 90, vjust = 0.5))
感谢您的帮助!
输出:
cr_grouped
structure(list(Date = structure(c(17897, 17897, 17897, 17928,
17928, 17928, 17956, 17956, 17956, 17987, 17987, 17987, 18017,
18017, 18017, 18048, 18048, 18048, 18078, 18078, 18078, 18109,
18109, 18109, 18140, 18140, 18140, 18170, 18170, 18170, 18201,
18201, 18201, 18231, 18231, 18231, 18262, 18262, 18262, 18293,
18293, 18293, 18322, 18322, 18322, 18353, 18353, 18353, 18383,
18383, 18383, 18414, 18414, 18414, 18444, 18444, 18444, 18475,
18475, 18475, 18506, 18506, 18506, 18536, 18536, 18536), class = "Date"),
Crime = c("Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences",
"Anti-social behaviour", "Theft", "Violence and sexual offences"
), Crime_occurrences = c(59426L, 90521L, 66296L, 61187L,
85416L, 61955L, 67810L, 93273L, 70843L, 68936L, 87633L, 66300L,
71379L, 89408L, 69906L, 72021L, 86315L, 71564L, 83705L, 86819L,
74969L, 78481L, 83309L, 69450L, 67932L, 81552L, 68740L, 69469L,
90307L, 70696L, 60692L, 87529L, 69011L, 53728L, 82404L, 69231L,
59571L, 88523L, 70824L, 57038L, 83460L, 69155L, 70861L, 71699L,
68956L, 135439L, 44984L, 58431L, 138529L, 47934L, 65947L,
102061L, 53208L, 70050L, 88014L, 62855L, 76773L, 91811L,
65687L, 76171L, 81958L, 67925L, 72268L, 84434L, 69627L, 70698L
)), row.names = c(NA, -66L), groups = structure(list(Date = structure(c(17897,
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170,
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444,
18475, 18506, 18536), class = "Date"), .rows = structure(list(
1:3, 4:6, 7:9, 10:12, 13:15, 16:18, 19:21, 22:24, 25:27,
28:30, 31:33, 34:36, 37:39, 40:42, 43:45, 46:48, 49:51, 52:54,
55:57, 58:60, 61:63, 64:66), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 22L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
ur_grouped
structure(list(Date = structure(c(17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536
), class = "Date"), Unemployment.rate = c(4.85019120720378, 4.87826349489932,
4.72619911892442, 4.82962291533381, 4.78037962696207, 4.75317591530959,
4.53739659597437, 4.58692391687679, 4.57052637882086, 4.56628766844258,
4.60997237871127, 4.58039236078879, 4.73224909680535, 4.73446295980473,
4.62619907452877, 4.44434845370539, 4.53780319184207, 4.46033205030955,
4.57680473216631, 5.01205713295936, 5.42174180050248, 5.75623503556053
)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", "data.frame"
))
OUT_RANGE和IN_RANGE[out_range可以手动调整,我一般是out_range[2]<- X 检查最适合缩放目的的值]
out_range <-范围(cr_grouped$Crime_occurrences) in_range <- 范围(ur_grouped$Unemployment.rate)
smooth_theft
structure(list(Date = structure(c(17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536
), class = "Date"), Crime = c("Theft", "Theft", "Theft", "Theft",
"Theft", "Theft", "Theft", "Theft", "Theft", "Theft", "Theft",
"Theft", "Theft", "Theft", "Theft", "Theft", "Theft", "Theft",
"Theft", "Theft", "Theft", "Theft"), Crime_occurrences = c(90521L,
85416L, 93273L, 87633L, 89408L, 86315L, 86819L, 83309L, 81552L,
90307L, 87529L, 82404L, 88523L, 83460L, 71699L, 44984L, 47934L,
53208L, 62855L, 65687L, 67925L, 69627L)), row.names = c(NA, -22L
), groups = structure(list(Date = structure(c(17897, 17928, 17956,
17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231,
18262, 18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506,
18536), class = "Date"), .rows = structure(list(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
18L, 19L, 20L, 21L, 22L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -22L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
smooth_antisocial:
structure(list(Date = structure(c(17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536
), class = "Date"), Crime = c("Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour", "Anti-social behaviour",
"Anti-social behaviour", "Anti-social behaviour"), Crime_occurrences = c(59426L,
61187L, 67810L, 68936L, 71379L, 72021L, 83705L, 78481L, 67932L,
69469L, 60692L, 53728L, 59571L, 57038L, 70861L, 135439L, 138529L,
102061L, 88014L, 91811L, 81958L, 84434L)), row.names = c(NA,
-22L), groups = structure(list(Date = structure(c(17897, 17928,
17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201,
18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444, 18475,
18506, 18536), class = "Date"), .rows = structure(list(1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -22L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
尝试下一个代码。首先安装 ggnewscale
包。我删除了最后一段代码,因为您没有共享该平滑选项的数据,但添加它不会成为问题。这里的代码:
library(ggnewscale)
#Plot
ggplot(mapping=aes(Date)) +
geom_line(aes(y = Crime_occurrences, colour = Crime),
size = 1, data = cr_grouped) +
geom_line(lwd = 1, mapping = aes(x = Date,
y = rescale(Unemployment.rate,
to = out_range,
from = in_range),
linetype = "Unemployment rate"),
data = ur_grouped) +
labs(linetype = "Legend") +
new_scale_color()+
geom_smooth(method = 'lm',se=F,
aes(x=Date,y = Crime_occurrences, group=1,
color='smooth_theft'),
lty = 6, data=smooth_theft)+
geom_smooth(method = 'lm',se=F,
aes(x=Date,y = Crime_occurrences, group=1,
color='smooth_antisocial'),
lty = 6, data=smooth_antisocial)+
labs(colour = "Smooth Conditional Means") +
scale_color_manual(values=c('darkgreen','red'))+
scale_x_date(breaks = seq(as.Date("2019-01-01"), as.Date("2020-10-01"), by="1 month"),
date_labels = '%m %Y') +
scale_y_continuous(sec.axis =
sec_axis(~ rescale(.x, to = in_range, from = out_range),
name = "Unemployment Rate (%)")) +
theme(axis.text.x=element_text(angle =- 90, vjust = 0.5))
输出: