【发布时间】:2021-07-02 17:39:32
【问题描述】:
我在一个散点图中有两个不同的数据集,图例中的 prediction2020_bounds 需要为蓝色,图例中的 median2020 需要为红色。 这是我的代码:
ggplot() +
# Points without corona
geom_line(data=prediction2020_bounds, aes(x = Date, y = lwr), colour = "blue", linetype = "solid") +
geom_line(data=prediction2020_bounds, aes(x = Date, y = upr), colour = "blue", linetype = "solid") +
geom_ribbon(data=prediction2020_bounds, aes(x = Date, ymin = lwr, ymax = upr), alpha=.4) +
labs(x = "Date", y = "Median Daily Price", title = "Daily Median Price", colour = "Legend Title\n") +
scale_x_date(date_breaks="1 month", date_labels="%Y %m") +
scale_colour_manual(labels = c("prediction2020_bounds", "median2020"), values = c(prediction2020_bounds, median2020)) +
theme_bw() +
geom_point(data=prediction2020_bounds, aes(x=Date, y=Price),fill="blue",
colour="darkblue", size=1) +
#Points with corona 2020 year
geom_point(data=median2020, aes(x=Date, y=Price), fill="red",
colour="red", size=1)
【问题讨论】:
-
这是因为您没有在图层的
aes()部分指定颜色。如果你使用aes(..., colour = "my legend item"),它会将颜色映射到一个比例,从而生成一个图例。
标签: r ggplot2 legend scatter-plot