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

时间:2016-01-11 13:14:18

标签: r ggplot2 legend

我想在地图上放点,但我希望图例只显示填充。下面的代码用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

1 个答案:

答案 0 :(得分:1)

使用show.legend = FALSE来取消geom的图例。

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

enter image description here