例如,我有两个数据集,第一组包含计算点,第二组包含网格坐标。我想使用 ggplot 绘制它们,并且我希望图例如下所示:
数据
df1<- data.frame(lon=c(21:70), lat=c(64:113), tem=c(12:61)) # computation points data
df2<- data.frame(grd.lon=seq(21,70,3.5),grd.lat=seq(12,61, 3.5)) # grid points data
library(ggplot2)
ggplot()+geom_point(data=df1, aes(x=lon,y=lat), color="black", shape=20, size=3)+
geom_point(data=df2, aes(x=grd.lon, y=grd.lat), colour="red", shape=3)
Run Code Online (Sandbox Code Playgroud)
我见过类似的问题,但没有一个真正帮助我我也尝试通过添加 scale_color_manual 和 scale_shape_manaul 手动绘制图例,但仍然没有用。请任何帮助
将您的 df 绑定在一起,如下所示:
df3 <- list("computation point" = df1, "grid points" = df2) %>%
bind_rows(.id = "df")
Run Code Online (Sandbox Code Playgroud)
比将变量映射到美学。ggplot2 然后会自动添加一个图例,可以使用 scale_..._manual 进行调整:
ggplot(df3, aes(shape = df, color = df)) +
geom_point(aes(x=lon,y=lat), size=3)+
geom_point(aes(x=grd.lon, y=grd.lat)) +
scale_shape_manual(values = c(20, 3)) +
scale_color_manual(values = c("black", "red")) +
labs(shape = NULL, color = NULL)
Run Code Online (Sandbox Code Playgroud)