0

stateCoords columns: State, Abbr, Longitude, Latitude, Region.

There are 5 regions, West, Midwest, Southwest, Southeast, and Northeast, 1 being West, 2 being Midwest, and so on.

# Declaration of Region Colors: West, Midwest, Southwest, Southeast, Northeast
regionColors <- c("#f76f6a","#f59846", "#C7E363", "#f9dc6d", "#58b2c3")

# Import state coords
stateCoords <- read.csv("stateCoords.csv", header=TRUE)

# Generate US Map
# Retrieve map of the U.S. (Alaska and Hawaii excluded)
map.data <- map_data("state")
# Create the map
m =  ggplot(map.data) + geom_map(aes(map_id = region), map = map.data, fill 
= "white", color = "grey20", size = 0.25) +
  expand_limits(x = map.data$long, y = map.data$lat) + 
  theme(axis.line = element_blank(), axis.text = element_blank(), axis.ticks 
= element_blank(),
        axis.title = element_blank(), panel.background = element_blank(), panel.border = element_blank(),
    panel.grid.major = element_blank(), plot.background = element_blank(),
    plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines")) 
# plot the map
plot(m)

x = NULL
y = NULL
col = NULL
for (i in 1:nrow(stateCoords)) {
  r = stateCoords[i,]
   x1 = rep(r$Longitude, r$catTweets1)
   y1 = rep(r$Latitude, r$catTweets1)
   x = c(x, x1)
   y = c(y, y1)

   stateColor = regionColors[r$RegionNum]

   nextCol = rep(stateColor, r$catTweets1)

   col = c(col, nextCol)
}


x = x + rnorm(length(x), sd = .27)
y = y + rnorm(length(y), sd = .27)

points = data.frame(x = x, y = y)


# add the points to the map
m2 = m + geom_point(data = points,  aes(x = x, y = y), size = 3, alpha = 
0.3, color = col)
# plot the map with the points
plot(m2)

Can someone please help me add a legend in the bottom left corner? I want a colored dot and the respective Region. For example,

#f76f6a colored dot   West
#f59846 colored dot   Midwest

https://i.stack.imgur.com/fmCGC.png

dput(stateCoords) https://pastebin.com/0J4ZQ89U The amount of information was really long, so I put it in a pastebin...

I tried looking up resources, but I ended up being more confused than I was before. I did look at the ggmap.pdf file that was available.

  • Could you share your data? You can use the code 'dput(stateCoords)' and post the output. Without being able to use your data to try your code out in our own systems it'll be hard for us to offer solutions. – jesstme Apr 26 '17 at 20:30
  • Here is some guidance on providing a reproducible question. This will make your questions more likely to get answered: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – jesstme Apr 26 '17 at 20:44
  • I added a link to the output of the dput(stateCoords). Thanks for the link. – Kaitlyn Apr 26 '17 at 21:35

0 Answers0