Search code examples
rggplot2aesthetics

Keep black border of symbols when drawing points colored and shaped based on variables on a map (geom_point)


My question is somewhat related to this thread but I'd like to actually have the black border for each symbol since I have instances where symbols overlap and I think having the border would help. However, I have color and shape based on a variable inside the aesthetics in geom_point so the solution of using shape=21 and color=NA does not quite solve my issue here: I want filled symbols with the color based on a variable and shaped based on another variable. Can this be done? Or should I be approaching this in a different way?

df <- data.frame("lon"= c(-95.350278, -95.257593, -104.987625, -104.998113),
             "lat"= c(29.752778, 29.733726, 39.751184, 39.704005),
             "type"= c("aa", "bb", "aa", "bb"),
             "mean"= c(5.369, 3.476, 7.857, 2.356))

states<-map_data("state")

mymap<- ggplot(data = states) +
geom_polygon(aes(x = long, y = lat, group = group), fill=NA, color = "black") +
coord_fixed(1.3) +
guides(fill=FALSE)+
geom_point(data=df, aes(x=lon, y=lat, color=mean, shape=type),size=5, alpha=0.8)+
scale_colour_gradient(low = "royalblue4", high = "red")+
theme_bw()+
theme(legend.position = "right",
axis.text = element_blank(),axis.line = element_blank(),
axis.ticks = element_blank(),panel.border = element_blank(),
panel.grid = element_blank(),axis.title = element_blank(),
plot.title = element_text(hjust=0.5, size=14, face="bold"))

print(mymap)

My map :

screen

Thanks for everyone's time and suggestions.


Solution

  • You need to use "fill" for colouring the shapes and then set "color" as black to get a boarder around them.

    mymap <- ggplot(data = states) +
      geom_polygon(aes(x = long, y = lat, group = group), fill = NA, color = "black") +
      coord_fixed(1.3) +
      geom_point(data = df, aes(x = lon, y = lat, fill = mean, shape = type), color = "black", size=5, stroke = 2) +
      scale_fill_gradient(low = "royalblue4", high = "red") +
      scale_shape_manual(values = c(24,21)) +
      theme_bw() +
      theme(legend.position = "right",
            axis.text = element_blank(), axis.line = element_blank(),
            axis.ticks = element_blank(), panel.border = element_blank(),
            panel.grid = element_blank(), axis.title = element_blank(),
            plot.title = element_text(hjust = 0.5, size = 14, face = "bold"))
    

    enter image description here