[英]How to put legends inside a Plot with ggplot2?
I have the next plot我有下一个 plot
ggplot(data=Predict_Fav, aes(x=Fecha, y=TotalCases)) +
ggtitle("Posibles Escenarios de Casos Infectados de COVID-19 en Reino Unido,
Según Tasa de Crecimiento del 31 de Marzo al 6 de Abril del 2020")+
geom_line(aes(y=TotalCases),linetype = "twodash",color="orange")+
geom_point(color="black")+
geom_line(data=Data_UK7,linetype = "twodash",color="black")+
geom_point(data= Data_UK7, color="black")+
geom_line(data=Predict,linetype = "twodash",color="blue")+
geom_point(data= Predict, color="black")+
geom_line(data=Predict_Desfav,linetype = "twodash",color="red")+
geom_point(data= Predict_Desfav, color="black")+
scale_x_date(date_breaks ="2 day")+
xlab("Fecha")+
ylab("Casos Confirmados Totales")
How I can put a legend with the corresponding color code, like this:如何使用相应的颜色代码放置图例,如下所示:
There is no data in the example to completely help with this exact plot, but for the legend positioning.示例中没有数据可以完全帮助这个精确的 plot,但对于图例定位。 This page is helpful.这个页面很有帮助。 What you need to is add:您需要添加的是:
+ theme(legend.position = c(0.8, 0.2))
As described in linked post.如链接帖子中所述。 legend.position can take a vector of x, y coordinates. legend.position 可以采用 x,y 坐标的向量。 Replace 0.8 and 0.2 with what you need.用你需要的替换 0.8 和 0.2。
The colored lines can go into one geom_line call and you specify the color with aes(color = your_grouping_variable_here).彩色线条可以 go 到一个 geom_line 调用中,您可以使用 aes(color = your_grouping_variable_here) 指定颜色。 This will also automatically make the legend.这也会自动生成图例。 Lastly, add custom colors in scale_color_manual:最后,在 scale_color_manual 中添加自定义 colors:
library(tidyverse)
data <- tibble(x = rep(1:10,2),
y = c(1:10, 2:11),
group = c(rep("one", 10), rep("two", 10)))
ggplot(data = data, aes(x = x, y = y))+
geom_line(aes(color = group))+
scale_color_manual(values = c("red", "blue"))+
theme_classic()+
theme(legend.position = c(0.2, .8))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.