0

Im sure this question has been asked before but I couldn't find it in the search. I have two datasets that I'm projecting to create a map using ggplot2. I'm having problems getting the legend to appear as I'd like.

Example code:

#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())

This results in the following map:

enter image description here

The problem is that its missing the legend information in green, which make sense bc I haven't mapped anything to the aesthetics. But I'm running into problems when I do:

Here I'm adding color="green" into aes(lon, lat) for df2 and scale_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

My problem with this is that the legend includes the word "colour" in it and "green". Ideally, I'd like the word "colour" to be replaced with the word "places" and for the word "green" to be completely erased. I tried the following change:

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

enter image description here

But that simply replaced the word "green" for "places". I want the word "colour" to be replaced by "places" and I don't want any label following the circle in the legend. So then I tried the following.

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

That gets me a bit of the way there. As you can see, "colour" has been replaced by "places", however, "green" is still there.

Trying scale_color_manual(values = "") gives me the following error:

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

Also, note that my two point datasets contain different types of data. For one dataset relying on magnitude of a variable to determine circle size, in the other I'm simply mapping locations independent of any other variable that determines size. Any help is much appreciated.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62

1 Answers1

2

Something like this (based off answer here) should work:

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

In order to force aesthetic mapping for the Places, I placed it inside aes() & specified the mapping in scale_color_manual().

Z.Lin
  • 28,055
  • 6
  • 54
  • 94