繁体   English   中英

ggplot2:具有多边形和点的自定义图例

[英]ggplot2: custom legend with polygon and point

我想在地图上放置点,但我希望图例仅显示填充。 下面的代码显示了我要使用plot1的图例,但是在plot2图例的填充上覆盖了一个点。 如何摆脱plot2图例中的这一点? 我想要plot2与plot1中的图例。

library(sp)
library(maptools)
library(rgeos)
library(ggplot2)
library(ggmap)


# prepare a map of USA and Canada
data(wrld_simpl)
canada.usa <- wrld_simpl[wrld_simpl$NAME %in% c("Canada", "United States"), ]

# fortify and add original data
can.us  <- fortify(canada.usa, region="NAME")
can.us2 <- merge(can.us, canada.usa@data, by.x="id", by.y="NAME") 

# first plot - legend looks perfect
plot1 <- ggplot(can.us2, aes(x=long, y=lat, group=group, fill=id)) + geom_polygon() + 
         coord_quickmap() + theme_nothing(legend=TRUE)
plot1 

# generate a simple set of two points
two.pts <- data.frame(gCentroid(canada.usa, byid=TRUE)@coords)

# add these two points to the original plot - not the legend I want
plot2 <- plot1
plot2 <- plot2 + geom_point(data=two.pts, aes(x=x, y=y, group=NULL, fill=NULL,  size=20)) + 
         guides(size=FALSE)
plot2

使用show.legend = FALSE可以show.legend = FALSE的图例。

ggplot(mtcars, aes(mpg, hp, col = as.factor(cyl))) + 
  geom_line() +
  geom_point(show.legend = FALSE)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM