在ggplot2中,如何添加其他图例?

时间:2023-02-09 21:42:50

I'm trying to build a map in ggplot2 using data from separate data frames.

我正在尝试使用来自不同数据帧的数据在ggplot2中构建一个映射。

library(maptools)

xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

xx.sub1 <- subset(xx, xx$FIPSNO < 37010)
xx.sub2 <- subset(xx, xx$FIPSNO > 37010)

xx.sub1@data$id <- rownames(xx.sub1@data)
xx.sub1.points <- fortify(xx.sub1, region="id")
xx.sub1.df = join(xx.sub1.points, xx.sub1@data, by="id")

xx.sub2@data$id <- rownames(xx.sub2@data)
xx.sub2.points <- fortify(xx.sub2, region="id")
xx.sub2.df = join(xx.sub2.points, xx.sub2@data, by="id")

ggplot(xx.sub2.df) + 
  aes(long, lat, fill = (SID79/BIR79)*1000, group = group) + 
  geom_polygon() + geom_path(color="grey80") +
  coord_equal() + 
  scale_fill_gradientn(colours = brewer.pal(7, "YlOrBr")) +
  geom_polygon(data = xx.sub1.df, fill = "grey50") + 
  geom_path(data = xx.sub1.df, color="grey80") +
  labs(fill = "Mapped value", title = "Title")

Up to this point everything works as expected and I get a nice map:

到目前为止,一切都按预期工作,我得到一个漂亮的地图:

在ggplot2中,如何添加其他图例?

What I'd like to change however is to add separate legend for data from xx.sub1.df - since all polygons are just filled with grey I hope it will be one additional entry.

我想改变的是为xx.sub1.df中的数据添加单独的图例 - 因为所有多边形都填充了灰色,我希望它是一个额外的条目。

How can I achieve that?

我怎样才能做到这一点?

1 个解决方案

#1


24  

I'm not 100% sure this is what you want, but here's how I'd approach the problem as I understand it. If we map some unused geom with any data from xx.sub1.df, but make it invisible on the plot, we can still get a legend for that geom. Here I've used geom_point, but you could make it others.

我不是百分百肯定这是你想要的,但这就是我理解它的方法。如果我们使用xx.sub1.df中的任何数据映射一些未使用的geom,但是在图中使它不可见,我们仍然可以获得该geom的图例。在这里我使用了geom_point,但你可以把它做成其他人。

p <- ggplot(xx.sub2.df) + 
  aes(long, lat, fill = (SID79/BIR79)*1000, group = group) + 
  geom_polygon() + geom_path(color="grey80") +
  coord_equal() + 
  scale_fill_gradientn(colours = brewer.pal(7, "YlOrBr")) +
  geom_polygon(data = xx.sub1.df, fill = "grey50") + 
  geom_path(data = xx.sub1.df, color="grey80") +
  labs(fill = "Mapped value", title = "Title")

#Now we add geom_point() setting shape as NA, but the colour as "grey50", so the 
#legend will be displaying the right colour

p2 <- p + geom_point(data = xx.sub1.df, aes(size="xx.sub1", shape = NA), colour = "grey50")

在ggplot2中,如何添加其他图例?

Now we just need to alter the size and shape of the point on the legend, and change the name of the legend (thanks to @DizisElferts who demonstrated this earlier).

现在我们只需要改变图例上点的大小和形状,并更改图例的名称(感谢前面演示过的@DizisElferts)。

p2 + guides(size=guide_legend("Source", override.aes=list(shape=15, size = 10)))

在ggplot2中,如何添加其他图例?

Of course you can change the way the labels work or whatever to highlight what you want to show.

当然,您可以更改标签的工作方式或突出显示您要显示的内容。

If this isn't what you're after, let me know!

如果这不是你想要的,请告诉我!

#1


24  

I'm not 100% sure this is what you want, but here's how I'd approach the problem as I understand it. If we map some unused geom with any data from xx.sub1.df, but make it invisible on the plot, we can still get a legend for that geom. Here I've used geom_point, but you could make it others.

我不是百分百肯定这是你想要的,但这就是我理解它的方法。如果我们使用xx.sub1.df中的任何数据映射一些未使用的geom,但是在图中使它不可见,我们仍然可以获得该geom的图例。在这里我使用了geom_point,但你可以把它做成其他人。

p <- ggplot(xx.sub2.df) + 
  aes(long, lat, fill = (SID79/BIR79)*1000, group = group) + 
  geom_polygon() + geom_path(color="grey80") +
  coord_equal() + 
  scale_fill_gradientn(colours = brewer.pal(7, "YlOrBr")) +
  geom_polygon(data = xx.sub1.df, fill = "grey50") + 
  geom_path(data = xx.sub1.df, color="grey80") +
  labs(fill = "Mapped value", title = "Title")

#Now we add geom_point() setting shape as NA, but the colour as "grey50", so the 
#legend will be displaying the right colour

p2 <- p + geom_point(data = xx.sub1.df, aes(size="xx.sub1", shape = NA), colour = "grey50")

在ggplot2中,如何添加其他图例?

Now we just need to alter the size and shape of the point on the legend, and change the name of the legend (thanks to @DizisElferts who demonstrated this earlier).

现在我们只需要改变图例上点的大小和形状,并更改图例的名称(感谢前面演示过的@DizisElferts)。

p2 + guides(size=guide_legend("Source", override.aes=list(shape=15, size = 10)))

在ggplot2中,如何添加其他图例?

Of course you can change the way the labels work or whatever to highlight what you want to show.

当然,您可以更改标签的工作方式或突出显示您要显示的内容。

If this isn't what you're after, let me know!

如果这不是你想要的,请告诉我!