我正在用ggplot2
,geom_line
和geom_point
在geom_smooth
中用趋势线绘制线图。我的代码与此类似。
year <- as.character(2011:2020)
x <- c(12, 13, 12.5, 14, 15, 17, 16, 18, 18, 19)
y <- c(25, 28, 30, 27, 30, 31, 30, 31, 33, 33)
dat <- data.frame(year, x, y) %>%
pivot_longer(cols = c(x,y), names_to = "item", values_to = "price")
ggplot(dat, aes(x=year, y=price, group=item, col=item))+
geom_line()+
geom_point(aes(shape=item))+
geom_smooth(method = "loess", se = FALSE, size=0.8, linetype="dotdash")+
labs(x = "Year",
y = "Price")
我想在图例中分别包含geom_smooth
趋势线,以便图例中有四个项目-x,y,x趋势线和y趋势线。我应该如何进行?