Search code examples
rgoogle-mapslegendggmap

Adding a legend to the ggplot map with shape


I have used two layers geom_point, with shape 24 and default. I would like to represent shapes and respective description in the legend. Can any one guide me how to do it.

total_map<-ggmap(Map)+
geom_point(data = crime_poss_drugs,shape=24,size=6,
aes(x = lon, y = lat,colour=factor(drug_cases)))+
geom_point(data = crime_prod_drugs,aes(x = lon, y = lat,colour=factor(drug_cases)))

Thanks in advance.


Solution

  • Basically, you can put shape=... into the call to aes(...).

    library(ggplot2)
    # make up some data
    set.seed(1)
    crime_poss_drugs <- data.frame(x=1:10, y=rnorm(10,1), drug_cases=rep(1:2,each=5))
    crime_prod_drugs <- data.frame(x=1:10, y=rnorm(10,3), drug_cases=rep(1:2,each=5))
    # plot with different shape for each dataset
    ggplot() +
      geom_point(data=crime_poss_drugs, 
                 aes(x, y, color=factor(drug_cases), shape="POSS"))+
      geom_point(data=crime_prod_drugs, 
                 aes(x, y, color=factor(drug_cases), shape="PROD"),size=3)+
      scale_shape_manual("Crime",values=c(POSS=24,PROD=16))+
      scale_color_discrete("Drug Cases")