我想在剧情中加入一个图例。然而,什么也没有显示出来。我看过其他类似的问题,但似乎没有一个能解决我的问题。
以下是代码的这一部分。
ggplot(lm_model, aes(x=year, y=pred_price)) +
geom_point(color="red") +
geom_line(color="red") +
geom_line(aes(x=year, y=real_price)) +
labs(title="Linear Regression",
x="Year",
y="Gas Price") +
scale_color_manual(labels = c("Predicted", "True Value"))下面是包含此代码的输出(如您所见,没有图例):

发布于 2020-12-14 08:00:47
这可以工作,但没有测试,因为没有数据共享。您需要在aes()中移动color语句
library(ggplot2)
#Data
lm_model <- data.frame(year=2010:2020,
pred_price=runif(11,0,75),
real_price=runif(11,0,75))
#Code
ggplot(lm_model, aes(x=year, y=pred_price)) +
geom_point(aes(color="red")) +
geom_line(aes(color="red")) +
geom_line(aes(x=year, y=real_price,color='black')) +
labs(title="Linear Regression",
x="Year",
y="Gas Price") +
scale_color_manual(labels = c("Predicted", "True Value"),
values=c('black','red'))+
labs(color='Price')输出:

https://stackoverflow.com/questions/65281993
复制相似问题