在ggplot2中,我如何添加额外的图例? [英] In ggplot2, how can I add additional legend?
问题描述
我试图使用来自不同数据框的数据在 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(colors = brewer.pal(7,YlOrBr))+
geo m_polygon(data = xx.sub1.df,fill =grey50)+
geom_path(data = xx.sub1.df,color =grey80)+
labs(fill =Mapped value ,title =Title)
到目前为止,一切都按预期工作,我得到一张不错的地图:
然而,我想要改变的是为来自 xx.sub1.df
的数据添加单独的图例 - 因为所有多边形都是只是充满了灰色,我希望它会是一个额外的入口。
我该如何做到这一点?
我不是100%肯定这是你想要的,但我明白这是我如何处理这个问题。如果我们将一些未使用的 geom
与来自 xx.sub1.df
的任何数据进行映射,但在绘图中使其不可见,我们仍然可以得到 geom
的图例。在这里,我使用了 geom_point
,但你可以让它成为其他人。
geom_polygon()+ geom_path(color = color = 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)
#现在我们将geom_point()设置形状添加为NA,但颜色为grey50,因此
#legend将显示正确的颜色
p2 < - p + geom_point(data = xx.sub1.df,aes(size =xx.sub1,shape = NA),color =grey50)
现在我们只需要改变图例上点的大小和形状,并更改图例的名称(谢谢to @DizisElferts谁演示了此早先 )。
p2 + guides(size = guide_legend(Source,override.aes = list(shape = 15,size = 10)))
当然,您可以更改标签的工作方式或其他方式来突出显示您想要显示的内容。
如果这不是你想要的,请告诉我!
I'm trying to build a map in ggplot2
using data from separate data frames.
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:
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.
How can I achieve that?
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.
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")
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).
p2 + guides(size=guide_legend("Source", override.aes=list(shape=15, size = 10)))
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!
这篇关于在ggplot2中,我如何添加额外的图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!