Search code examples
rggplot2usmap

Error when using usmap package with geom_point


I'm trying to plot the locations of various stations across the U.S. with color of the point corresponding to a certain category. Some of these locations are outside of the continental U.S. (territories), but I'd like to at least include Alaska and Hawaii on the map. My code had previously worked months ago, but I went back to change colors and upon rerunning it, I get an error. I'm getting the same error when running other code where I use usmaps with ggplot in this way too.

Here's the data I'm using:

  stano Latitude  Longitude                                     category
1   358 14.54408  120.99139   Burnout increasing but slower than average
2   402 44.28015  -69.70422   Burnout increasing but slower than average
3   405 43.64790  -72.34237 Burnout increasing faster than 5.8% per year
4   436 46.61764 -112.10185   Burnout increasing but slower than average
5   437 46.90609  -96.77504                    Burnout decreasing yearly
6   438 43.53124  -96.75762   Burnout increasing but slower than average

This is what I ran:


## Create the map ---------------------

map <- data %>%  

  filter(!is.na(stano), stano != '358') %>%  
  
  dplyr::select(
    lon = Longitude, 
    lat = Latitude, 
    stano, category
  ) %>% 
  
  na.omit() %>% 
  
  usmap_transform()   


plot_usmap() + 
  geom_point(data = map, 
             aes(y = y, x = x, color = category), 
             size = 2) +
  
  scale_color_manual(
    breaks = c("Burnout decreasing yearly", "Burnout increasing but slower than average", 
               "Burnout increasing faster than average", "Burnout increasing faster than 5.8% per year"), 
   values = c("forestgreen", "blue", "lightpink", "darkred")) + 
  
  labs(title = 'Mental Health Burnout Change From 2019 to 2022', color = 'Change in burnout percentage') + 
  
  theme(legend.position = 'top') +
  
  guides(color = guide_legend(nrow = 2))

This is the error:


Error in `geom_point()`:
! Problem while computing aesthetics.
i Error occurred in the 3rd layer.
Caused by error:
! object 'x' not found

This is the original plot I was getting before this error occurred.

burnout map

I have made sure my packages are up to date. I've tried using lat and lon in place of x and y in the aes mapping. I've tried not using usmap_transform() and just plotting the coordinates as they are. I'd like to still use this package if possible.


Solution

  • After transforming your dataframe with usmap_transform(), the positions of the points are no longer available as x- or y-coordinates (there is alspo no longer a lat-or lon column in that dataframe, those positions are now stored as sfc_POINT in the geometry column. That means geom_point can no longer access them, so you need an geom that understands that data, and that's geom_sf():

    library(usmap)
    
    df <- tribble(
      ~id, ~stano, ~Latitude,  ~Longitude,                                     ~category,
      1,   358, 14.54408,  120.99139,   "Burnout increasing but slower than average",
      2,   402, 44.28015,  -69.70422,   "Burnout increasing but slower than average",
      3,   405, 43.64790,  -72.34237, "Burnout increasing faster than 5.8% per year",
      4,   436, 46.61764, -112.10185,   "Burnout increasing but slower than average",
      5,   437, 46.90609,  -96.77504,                    "Burnout decreasing yearly",
      6,   438, 43.53124,  -96.75762,   "Burnout increasing but slower than average",
    ) %>%  
      filter(!is.na(stano), stano != '358') %>%  
      dplyr::select(
        lon = Longitude, 
        lat = Latitude, 
        stano, category
      ) %>% 
      na.omit() %>% 
      usmap_transform()
    
    
    usmap::plot_usmap()+
      geom_sf(data = df, aes(color = category))