Search code examples
rgoogle-mapsgeom-bar

making a map of usa in r with earnings for men and women


i want to make a google map with geom_point. I have two colomns for earnings for men and women and I want to show them together on the same map.

 state   capital_name     lat        long      women earnings    men 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

as you can see, in the same numbers, i now have woman earnings and man earnings. I want them to be shown in the map together.

I tried to pot a plus (+) to the code for pot it together with on resolt. this in my code:

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

#the men:
+ ggmap(map) + geom_point(aes(x = long, y = lat, size = 
sqrt(men_median_weekly_earnings)), data = join2, alpha = .5 
,color="blue")+scale_size(range=c(3,20))

I have an eror problem as follow:

         Error in add_ggplot(e1, e2, e2name) : 
         argument "e2" is missing, with no default

Each of them works separately but not together.

the women:enter image description here

the men: enter image description here

also, maybe geom_point is not so good? I cant see the us states. there is maybe somethig else even better to shot this data on the map? columns\bars maybe?

thanks.


Solution

  • Something like this:

    Data prep

    df <- read.table(text = "state   capital_name     lat        long      women_earnings    men_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 Little_Rock  34.73601  -92.33112        610              703", header = T)
    
    
    ## I'm going to reshape the data into long-form so it's easier to work with 
    library(reshape2)
    
    df <- melt(df, id.vars = c("state", "capital_name", "lat", "long"), value.name = "earnings")
    

    ggmap

    ggmap(map) + 
        geom_point(aes(x = long, y = lat, size = sqrt(earnings), colour = variable), 
                             data = df, alpha = .5) +
        scale_colour_manual(values  = c("red", "darkblue")) +
        scale_size(range=c(3,20), guide = 'none')
    

    enter image description here

    If you want an interactive map there are a few options, in particular leaflet and googleway. There are differences in how they both 'accept' the data, but it's not too much manipulation.

    Leaflet

    library(leaflet)
    
    colourPalette <- colorFactor(c("red", "darkblue"), unique(df$variable))
    
    df %>%
        leaflet() %>%
        addTiles() %>%
        addCircles(radius = ~(earnings * 500), stroke = FALSE, 
                             fillColor = ~colourPalette(variable)) %>%
        addLegend(pal = colourPalette, values = unique(df$variable))
    

    enter image description here

    Googleway

    library(googleway)
    
    
    mapKey <- 'your_api_key'
    
    ## the colours and radius are required on the data itself
    df_colours <- data.frame(variable = unique(df$variable), 
                                                     colour = c("red", "darkblue"))
    
    df <- merge(df, df_colours, by = "variable")
    df$radius <- df$earnings * 500
    
    google_map(key = mapKey) %>%
        add_circles(data = df, fill_colour = "colour", radius = "radius")
    

    enter image description here