使用 scale_..._manual 将自定义图例添加到具有两个 geom_point 层的 ggplot
Add a custom legend to a ggplot with two geom_point layers using scale_..._manual
我有两个数据集,第一个包含计算点,第二个包含网格坐标。我想使用 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)
我见过类似的问题,但 none 确实帮助了我
我也尝试通过添加 scale_color_manual 和 scale_shape_manaul 手动绘制图例,但仍然没有用。
请帮忙
像这样将你的 df 绑定在一起:
df3 <- list("computation point" = df1, "grid points" = df2) %>%
bind_rows(.id = "df")
比将变量映射到美学。然后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)
我有两个数据集,第一个包含计算点,第二个包含网格坐标。我想使用 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)
我见过类似的问题,但 none 确实帮助了我 我也尝试通过添加 scale_color_manual 和 scale_shape_manaul 手动绘制图例,但仍然没有用。 请帮忙
像这样将你的 df 绑定在一起:
df3 <- list("computation point" = df1, "grid points" = df2) %>%
bind_rows(.id = "df")
比将变量映射到美学。然后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)