Reputation: 1129
I'd like to include a legend for a plot. However, nothing shows up. I've looked at other similar questions but none seem to solve my case.
Here's that section of the code.
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"))
Here's how the output looks with this code (and without the legend as you can see):
Upvotes: 1
Views: 91
Reputation: 39613
This can work but not tested as no data was shared. You need to move the color statements inside aes()
:
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')
Output:
Upvotes: 2