我正在尝试将legent放在我的ggplot上。我正在努力使线型反映在legent中。我有这个代码:
legend_1 <- "a"
legend_2 <- "b"
colours <- t(c('green', 'red'))
colnames(colours) <-c(legend_1, legend_2)
df <- data.frame(a=1:3,b=c(5,6,-1),c=c(3,5,4))
ggplot(df, aes(x=a)) + #byg plot
geom_line(aes(y=b, color = legend_1), size=1) + #tilføj linje
geom_line(aes(y=c, color = legend_2), size=1,linetype="dashed")+ #tilføj ny linje
theme_gray() + #tilføj tema
labs(title = "title", y = 'ppm', x= 'Tid (s)', color = '') + #tilføj titel og akselabels
theme(plot.title = element_text(hjust = 0.5)) + #centrerer titel
scale_color_manual(values = colours)
我能做什么?
基本上它与添加颜色图例相同。在linetype
aes上映射而不是将其设置为仅一行的参数。通过 添加所需的线型scale_linetype_manual
,确保为两个图例使用相同的标题(我只是通过NULL
insidee删除标题labs
):
library(ggplot2)
legend_1 <- "a"
legend_2 <- "b"
colours <- t(c('green', 'red'))
colnames(colours) <-c(legend_1, legend_2)
lty <- c(a = "solid", b = "dashed")
df <- data.frame(a=1:3,b=c(5,6,-1),c=c(3,5,4))
ggplot(df, aes(x=a)) + #byg plot
geom_line(aes(y=b, color = legend_1, linetype = legend_1), size=1) + #tilføj linje
geom_line(aes(y=c, color = legend_2, linetype = legend_2), size=1)+ #tilføj ny linje
theme_gray() + #tilføj tema
labs(title = "title", y = 'ppm', x= 'Tid (s)', color = NULL, linetype = NULL) + #tilføj titel og akselabels
theme(plot.title = element_text(hjust = 0.5)) + #centrerer titel
scale_color_manual(values = colours) +
scale_linetype_manual(values = lty)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句