This markdown document includes the code neccesary to generate the plot map from www.adolfoalvarez.cl

Since there is one interesting map with the geolocalization of the groups in the blog of Revolution Analytics, I will propose something different: Using rvest for webscrapping, dplyr for data crunching and leaflet for visualization, I want to create a choropleth map of R users per country, and obtain the top five groups by number of members. You can see similar choropleths maps generated in R here and here

Let’s get the data:

# library(devtools)
# install_github("hadley/rvest")
library(rvest)
library(dplyr)
r_users <- read_html("http://r-users-group.meetup.com/all/")

From the meetup directory I will extract the information of the groups using rvest and SelectorGadget as described in this vignette

group <- r_users %>%
  html_nodes(".omngj_sj21") %>% #The name of the node is identified using "SelectorGadget"
  html_text()

latitude <- r_users %>%
  html_nodes(".latitude") %>%
  html_text()

longitude <- r_users %>%
  html_nodes(".longitude") %>%
  html_text()

country <- r_users %>%
  html_nodes(".country-name") %>%
  html_text()

locality <- r_users %>%
  html_nodes(".locality") %>%
  html_text()

members <- r_users %>%
  html_nodes(".note") %>%
  html_text()

data <- data.frame(group, locality, country, 
                   latitude, longitude, members, 
                   stringsAsFactors=F) %>%
  tbl_df()

data <- data %>%
  mutate(latitude=as.numeric(latitude),
         longitude=as.numeric(longitude),
         members=gsub(" members","",members) %>% as.numeric()           
  )
data %>%
  select(group, country, members)
## Source: local data frame [195 x 3]
## 
##                                                     group country members
## 1                              Statistical Programming DC      us    2664
## 2                  San Diego Data Science & R Users Group      us     628
## 3  Chicago R User Group (Chicago RUG) Data and Statistics      us    1726
## 4             Seattle useR Group (R Programming Language)      us    1269
## 5            New York Open Statistical Programming Meetup      us    5051
## 6            Kasseler useR Group (R Programming Language)      de      73
## 7            Bay Area useR Group (R Programming Language)      us    4137
## 8                         Sydney Users of  R Forum (SURF)      au    1073
## 9                                        LA R users group      us    1431
## 10                                 Canberra R Users Group      au     203
## ..                                                    ...     ...     ...

Now for the choropleth map we use rgdal and leaflet.

library(rgdal)
library(leaflet)

# We download the data for the map from naturalearthdata.com
url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"
folder <- getwd() #set a folder where to download and extract the data
file <- basename(url) 
# download.file(url, file)
# unzip(file, exdir = folder)

#And read it with rgdal library
world <- readOGR(dsn = folder, 
                     layer = "ne_50m_admin_0_countries",
                     encoding = "latin1", #you may need to use a different encoding
                     verbose = FALSE)

#This function will extract the top 5 groups and return them as text to be included in the map
myfun <- function(x,y){
  a <- data.frame(x,y) %>%
    arrange(-y) %>%
    slice(1:5)
  return(paste0(a$x, ", ", a$y, " members", collapse="<br>"))
}

#Now we group by country and apply the function
data <- data %>%
  group_by(country) %>%
  summarise(top5=myfun(group, members),
            members=sum(members, na.rm=T)              
  ) %>%
  ungroup() %>%
  mutate(country=toupper(country))

#We need to reconvert to data.frame to merge it with the SpatialPolygons data frame "world"
data <- data.frame(data) 
world <- merge(world, data,
               by.x = "iso_a2",
               by.y = "country",
               sort = FALSE)

#Colour palette. Check ?colorQuantile and ?RColorBrewer for more options
pal <- colorQuantile("YlOrRd", NULL, n = 20)

#Pop up: The info displayed when click on a country
world_popup <- paste0("<strong>Country: </strong>", 
                        world$name, 
                        "<br><strong>", 
                        "Members", 
                        ": </strong>", 
                        world$members,
                        "<br><strong>", 
                        "Top 5 groups", 
                        ": </strong><br>",
                        world$top5
)

#Tiles coming from stamen.com
tiles <- "http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png"
attribution <- 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Map data by <a href="http://www.naturalearthdata.com/">Natural Earth</a>.'

#And finally the map:
leaflet(data = world) %>%
  addTiles(urlTemplate = tiles,  
           attribution = attribution) %>%
  setView(0, 0, zoom = 2) %>%
  addPolygons(fillColor = ~pal(world$members), 
              fillOpacity = 0.8, 
              color = "#000000", 
              weight = 1, 
              popup = world_popup)