带有2个数据集的{ggplot2映射中的图例标签

时间:2017-09-09 05:25:41

标签: r ggplot2 mapping spatial

我确定之前已经问过这个问题,但我无法在搜索中找到它。我有两个数据集,我使用ggplot2投影来创建地图。我无法让传奇像我一样出现。

示例代码:

#First dataset
lat<-as.numeric(c(42.279594, 37.335480, 42.652580, 42.331429, 32.897480, 40.785091))
lon<-as.numeric(c(-83.732124, -121.893028,-73.756233, -83.045753, -97.040443, -73.968285))
Location<-c("Ann Arbor, MI","San Joke, CA", "Albany, NY", 
     "Detroit, MI", "Dallas, TX","New York, NY")
Incidents<-as.numeric(c(21, 12, 9, 6, 5, 5))
df1<-cbind.data.frame(Location, Incidents, lat, lon)

#Second dataset
Location<-c("New York, NY","Los Angeles, CA", "Queens, NY", 
     "Minneapolis, MN", "Austin, TX","Bronx, NY")
lat<-as.numeric(c(40.71278, 34.05223, 40.72822, 44.97775, 30.26715, 40.84478))
lon<-as.numeric(c(-74.00594, -118.24368,-73.79485, -93.26501, -97.74306, -73.86483))
df2<-cbind.data.frame(Location, lat, lon)

#Map
library(ggplot2)
states <- map_data("state")
states <- states[order(states$order), ]

ggplot() +
geom_polygon(data=states, aes(long, lat, group=group),
    size=0.1, fill="black", color="white", alpha=0.75) +
geom_point(data=df1, aes(lon, lat,size= Incidents), color="red", alpha =0.5) +
geom_point(data=df2, aes(lon, lat), color="green", alpha =0.5) +
theme(axis.line=element_blank(),
     axis.text.x=element_blank(),
     axis.text.y=element_blank(),
     axis.ticks=element_blank(),
     axis.title.x=element_blank(),
     axis.title.y=element_blank(),
     legend.position="bottom",
     panel.background=element_blank(),
     panel.border=element_blank(),
     panel.grid.major=element_blank(),
     panel.grid.minor=element_blank(),
     plot.background=element_blank())

这导致以下地图:

enter image description here

问题在于它错过了绿色的传奇信息,这很有意义bc我还没有将任何东西映射到美学上。但是,当我这样做时,我遇到了问题:

在此我将color="green"添加到aes(lon, lat) df2scale_color_manual(values = "green")

ggplot() +
    geom_polygon(data=states, aes(long, lat, group=group),
        size=0.1, fill="black", color="white", alpha=0.75) +
    geom_point(data= df, aes(lon, lat,size= Incidents), color="red", alpha =0.5) +
    geom_point(data=df2, aes(lon, lat, color="green"), alpha =0.5) +
    theme(axis.line=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y=element_blank(),
         axis.ticks=element_blank(),
         axis.title.x=element_blank(),
         axis.title.y=element_blank(),
         legend.position="bottom",
         panel.background=element_blank(),
         panel.border=element_blank(),
         panel.grid.major=element_blank(),
         panel.grid.minor=element_blank(),
         plot.background=element_blank())

enter image description here

我的问题是图例中包含“&#34; color&#34;在它和&#34;绿色&#34;。理想情况下,我喜欢“#34; color&#34;被替换为&#34; places&#34;而对于&#34;绿色&#34;被完全抹去。我尝试了以下更改:

scale_color_manual(values = "green", label="places")

enter image description here

但这简直取代了&#34; green&#34; for&#34; places&#34;。我想要&#34; color&#34;被&#34;替换为#34;地点&#34;而且我不想在图例中的圆圈后面有任何标签。所以我尝试了以下内容。

places<-c("green","green","green","green","green","green")
df2<-cbind.data.frame(df2, green)
     Location      lat    lon       places
1 Ann Arbor, MI 42.27959  -83.73212 green 
2  San Joke, CA 37.33548 -121.89303 green 
3    Albany, NY 42.65258  -73.75623 green 
4   Detroit, MI 42.33143  -83.04575 green 
5    Dallas, TX 32.89748  -97.04044 green 
6  New York, NY 40.78509  -73.96828 green  

ggplot() +
    geom_polygon(data=states, aes(long, lat, group=group),
        size=0.1, fill="black", color="white", alpha=0.75) +
    geom_point(data= df, aes(lon, lat,size= Incidents), color="red", alpha =0.5) +
    geom_point(data=df2, aes(lon, lat, color=places), alpha =0.5) +
    theme(axis.line=element_blank(),
         axis.text.x=element_blank(),
         axis.text.y=element_blank(),
         axis.ticks=element_blank(),
         axis.title.x=element_blank(),
         axis.title.y=element_blank(),
         legend.position="bottom",
         panel.background=element_blank(),
         panel.border=element_blank(),
         panel.grid.major=element_blank(),
         panel.grid.minor=element_blank(),
         plot.background=element_blank())+ scale_color_manual(values = "green")

enter image description here

这让我有点意思。如你所见,&#34; color&#34;已被&#34;地点&#34;取代,然而,&#34;绿色&#34;还在那里。

尝试scale_color_manual(values = "")会出现以下错误:

Error in grDevices::col2rgb(colour, TRUE) : invalid color name ''

另外,请注意我的两点数据集包含不同类型的数据。对于依赖于变量大小来确定圆周大小的一个数据集,在另一个数据集中,我只是简单地映射位置,而与确定大小的任何其他变量无关。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

这样的事情(基于答案here)应该有效:

ggplot() +
  geom_polygon(data=states, aes(long, lat, group=group),
               size=0.1, fill="black", color="white", alpha=0.75) +
  geom_point(data=df1, aes(lon, lat,size= Incidents), color="red", alpha =0.5) +
  geom_point(data=df2, aes(lon, lat, color="Places"), alpha =0.5) +
  scale_color_manual(name = "Places", values = c("Places" = "green"), labels = c("")) +
  theme(axis.line=element_blank(),
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.position="bottom",
        panel.background=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.background=element_blank())

enter image description here

为了强化地方的美学地图,我把它放在aes()&amp;在scale_color_manual()中指定了映射。