setwd("~/Google Drive/FRI/")
#install.packages('googlesheets')
library(googlesheets)
## Warning: package 'googlesheets' was built under R version 3.2.5
# sheets = gs_ls()
# sheets[1,]
Read in WorldMap data
world.map <- gs_title("Aug Data")
## Sheet successfully identified: "Aug Data"
Read the data
df <- gs_read(world.map)
## Accessing worksheet titled 'WorldMap'.
## No encoding supplied: defaulting to UTF-8.
Plotly requires the country code for a given country for Choropleth Maps. Let’s create a new var country code first
# install.packages("countrycode")
library(countrycode)
df$country.code <- countrycode(df$Country, "country.name", "wb")
Finally plot the Choropleth Map
library(plotly)
df$hover <- with(df, paste(Country,'<br>', "New Sessions (%)", `New Sessions`, '<br>', "New Users", `New Users`, '<br>', "Bounce Rate (%)", `Bounce Rate`, '<br>', "Avg. Session Duration (sec)", `Avg. Session Duration`))
# give state boundaries a white border
l <- list(color = toRGB("grey"), width = 0.5)
# specify some map projection/options
g <- list(
showframe = FALSE,
showcoastlines = FALSE,
projection = list(type = 'Mercator')
)
plot_ly(df, z = Sessions, text = hover, locations = country.code, type = 'choropleth',
color = Sessions, colors = 'Blues', marker = list(line = l),
colorbar = list(title = 'Sessions')) # %>%
#layout(title = '# of Sessions Aug 1 - Aug 30<br>(Hover for breakdown)', geo = g)