0

I'm trying to create a map of USA with points of earnings by state.

for example I have these data:

    state   capital_name     lat        long      women    earning            
1   Alabama   Montgomery   32.36154  -86.27912     621       832    
2    Alaska     Juneau     58.30194  -134.41974    797      1008 
3    Arizona   Phoenix     33.44846  -112.07384    669       827  
4    Arkansas Little Rock  34.73601  -92.33112     610       703  

For now this is my code:

 mapPoints <- ggmap(map) + 
              geom_point(aes(x = lat, y = long, size = sqrt(women_median_weekly_earnings)),   
                         data = join2, alpha = .5 ,color="darkred") +
              scale_size(range=c(3,20))

 map <- get_map(location = 'usa', zoom = 4)

 mapPoints 

This what I get: enter image description here

As you can see, the points are not on the map. What is wrong with the code?

How can I fix it?

Thank you very much

jazzurro
  • 23,179
  • 35
  • 66
  • 76
Gilad Uzan
  • 35
  • 2

1 Answers1

0

You made a mistake in aesthetics; y is latitude (north-south), x longitude (west-east), not the opposite.

Here, your code is looking, for example, for Alabama, for 32.36154 on the x axis, and -86.27912 on the y axis. There's not within the limits of the map supplied, so this point won't be visible.

You should get things done by reversing lat and long in your aes.

Also, zoom = 3 seems to be the right zoom to get all the points within the map


library(ggmap)

map <- map<-get_map(location='united states', zoom=3, maptype = "terrain",
                source='google',color='color')
mapPoints <- ggmap(map) + 
                         geom_point(aes(y = lat, x = long, size = sqrt(earning)), 
             data = df, alpha = .5 ,color="darkred") +
                                                      scale_size(range=c(3,20))

mapPoints

enter image description here

Data:

df <- read.table(text='state    capital_name    lat long    women   earning
                       Alabama  Montgomery  32.36154    -86.27912   621 832
                       Alaska   Juneau  58.30194    -134.41974  797 1008
                       Arizona  Phoenix 33.44846    -112.07384  669 827
                       Arkansas LittleRock  34.73601    -92.33112   610 703',  
                       header = T, quote = '"') 
M--
  • 25,431
  • 8
  • 61
  • 93
Colin FAY
  • 4,849
  • 1
  • 12
  • 29
  • No problem, please do ! – Colin FAY Jul 21 '17 at 14:45
  • Try to make your answers well-formatted, clean, precise, complete but not unnecessarily long. You can read my comment on the edits if you wish. This way you'd get more reputation as your answer appears to be more professional on the first glance. Cheers. – M-- Jul 21 '17 at 14:48
  • thanks Colin. I did what you say about the long and lat. It's hepped. but now I have large circles on the map and that a great progress. How do I differentiate between men and women? – Gilad Uzan Jul 21 '17 at 14:50
  • @GiladUzan to assign color to a group var, read https://stackoverflow.com/questions/6919025/how-to-assign-colors-to-categorical-variables-in-ggplot2-that-have-stable-mappin. Also, as it solves your question, can you please accept the answer I've given? Tbanks, Colin. – Colin FAY Jul 23 '17 at 06:46