1
votes

I have plotted a choropleth map of the US and overlaid data for each state. Now I am trying to add dots to certain latitudinal and longitudinal coordinates and thought adding geom_point to my ggplot would do the trick. I think I am having some issue with the input for aes and am having a lot of trouble figuring out how to fix this. Here is the code:

library(ggplot2)
library(fiftystater)
library(colorplaner)
library(RColorBrewer)

# prepare data frame
data("fifty_states") 

df <- data.frame(state=tolower(rownames(USArrests)), USArrests)
names(df)[names(df)=='Murder'] <- 'Fake_Data'
names(df)[names(df)=='Assault'] <- 'Bucket'
df<-df[,-c(4:5)]

df$Fake_Data <- runif(nrow(df), 1,60)

df$Bucket = 0
for (i in 1:nrow(df)){
  if(df[i, 2] <= 10){
    df[i,3] = 1
  } else if(df[i,2] > 10 & df[i,2] <= 20){
    df[i,3] = 2
  } else if(df[i,2] > 20 & df[i,2] <= 30){
    df[i,3]= 3
  } else if(df[i,2] > 30 & df[i,2] <= 40){
    df[i,3] = 4
  } else if(df[i,2] > 40 & df[i,2] <= 50){
    df[i,3] = 5
  }
  else if(df[i,2] > 50)
    df[i,3]=6
}

p <- ggplot(df, aes(map_id = state)) + 
  # map points to the fifty_states shape data
  geom_map(aes(fill = factor(Bucket)), map = fifty_states, color = 'gray') + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_x_discrete(breaks = NULL) + 
  scale_y_discrete(breaks = NULL) +
  labs(x = "", y = "") +
  theme(legend.position = "right", 
        panel.background = element_blank())

labs <- data.frame(long = c(-122.064873,-122.306417), 
                   lat = c(36.951968,47.644855), 
                   names = c('lab', 'Seattle'), stringsAsFactors = FALSE)

p + fifty_states_inset_boxes()+ 
  scale_fill_manual('Fake Data Percentages',
                    values = c('#fef0d9', '#fdd49e', '#fdbb84', '#fc8d59', '#e34a33', 
                               '#b30000'), 
                    labels = c('0-10','10-20','20-30','30-40','40-50','50-60'))+
  geom_point(data = labs, aes(x=long, y=lat),
             color = 'black', size = 5) + 
  geom_point(data = labs, aes(x=long, y=lat),
             color = 'blue', size = 4)

I've tried replacing the x and y values in the geom_point with map_id = state and with a simple x=long, y=lat. But no luck. The error I get is:

Error in FUN(X[[i]], ...) : object 'state' not found

Any help would be greatly appreciated!

1
As a side point, you shouldn't use declare the dataframe within an aesthetic call. I've edited this to show you. i.e. it is data = labs, aes(x=long, y=lat) not data = labs, aes(x=labs$long, y=labs$lat). This can create some problems within the plotsMichael Harper
Interesting, I didn't know that. Do you know an example of what might happen off the top of your head?lostpineapple45
This explains some of the issues: stackoverflow.com/q/32543340/7347699Michael Harper

1 Answers

1
votes

In your global options for the plot, you had set:

ggplot(df, aes(map_id = state))

This tries to pass the dataframe df and aesthetic map_id state to all the arguments, unless overridden in the geometry option. This is what was causing the error.

To fix this, just set the map_id within the geom_map call. This ensures it is only set locally for that one object:

p <- ggplot(df) + 
  # map points to the fifty_states shape data
  geom_map(aes(fill = factor(Bucket), map_id = state), map = fifty_states, color = 'gray') + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_x_discrete(breaks = NULL) + 
  scale_y_discrete(breaks = NULL) +
  labs(x = "", y = "") +
  theme(legend.position = "right", 
        panel.background = element_blank())

labs <- data.frame(long = c(-122.064873,-122.306417), 
                   lat = c(36.951968,47.644855), 
                   names = c('lab', 'Seattle'), stringsAsFactors = FALSE)

p + fifty_states_inset_boxes() + 
  scale_fill_manual('Fake Data Percentages',
                    values = c('#fef0d9', '#fdd49e', '#fdbb84', '#fc8d59', '#e34a33', 
                               '#b30000'), 
                    labels = c('0-10','10-20','20-30','30-40','40-50','50-60'))+
  geom_point(data = labs, aes(x=long, y=lat),
             color = 'black', size = 5) + 
  geom_point(data = labs, aes(x=long, y=lat),
             color = 'blue', size = 4)

enter image description here