Search code examples
rggplot2usmap

Draw map in R with different color markers and legend?


This is what my data looks like: Data

I want to draw a map for state of Hawaii, where each latitude and longitude is a marker of different color based on the category in dummy, and then a legend based on that dummy column value.

The furthest I have gone is using this attempt, where cleanData is the name of my dataframe.

test_data <- data.frame(lon = cleanData$Longitude, lat = cleanData$Latitude)
transformed_data <- usmap_transform(test_data)

plot_usmap(include = c("HI")) + 
  geom_point(data = transformed_data, 
             aes(x = x, y = y), 
             color = "red",
             size = 1)

Solution

  • You could achieve your desired result by adding your dummy column to your dataset and mapping it on the color aes:

    library(usmap)
    library(ggplot2)
    
    test_data <- data.frame(
      lon = c(-157.8822, -157.7993),
      lat = c(21.33517, 21.39912),
      dummy = c("L2", "L2_DC")
    )
    
    transformed_data <- usmap_transform(test_data)
    #> Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
    #> prefer_proj): Discarded datum unknown in Proj4 definition
    
    plot_usmap(include = "HI") +
      geom_point(
        data = transformed_data,
        aes(x = x, y = y, color = dummy),
        size = 3
      )