R for
Authoring!!

OOH Session #40…
…Leaflet Maps

Open Office Hours
(August 27, 2024)

Recap of Session #39:

Plotly Interactivity

library(plotly)

data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")

data_2007 <- data[which(data$year == 2007),]
data_2007 <- data_2007[order(data_2007$continent, data_2007$country),]
slope <- 2.666051223553066e-05
data_2007$size <- sqrt(data_2007$pop * slope)
colors <- c('#4AC6B7', '#1972A4', '#965F8A', '#FF7070', '#C61951')

fig <- plot_ly(data_2007, x = ~gdpPercap, y = ~lifeExp, color = ~continent, size = ~size, colors = colors,
        type = 'scatter', mode = 'markers', sizes = c(min(data_2007$size), max(data_2007$size)),
        marker = list(symbol = 'circle', sizemode = 'diameter',
                      line = list(width = 2, color = '#FFFFFF')),
        text = ~paste('Country:', country, '<br>Life Expectancy:', lifeExp, '<br>GDP:', gdpPercap,
                      '<br>Pop.:', pop))
fig <- fig %>% layout(title = 'Life Expectancy v. Per Capita GDP, 2007',
         xaxis = list(title = 'GDP per capita (2000 dollars)',
                      gridcolor = 'rgb(255, 255, 255)',
                      range = c(2.003297660701705, 5.191505530708712),
                      type = 'log',
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwidth = 2),
         yaxis = list(title = 'Life Expectancy (years)',
                      gridcolor = 'rgb(255, 255, 255)',
                      range = c(36.12621671352166, 91.72921793264332),
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwith = 2),
         paper_bgcolor = 'rgb(243, 243, 243)',
         plot_bgcolor = 'rgb(243, 243, 243)')

fig
library(plotly)

us_cities = read.csv("https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv")

fig <- us_cities 
fig <- fig %>%
  plot_ly(
    lat = ~lat,
    lon = ~lon,
    marker = list(color = "fuchsia", size=us_cities$Population/60000),
    type = 'scattermapbox',
    hovertext = us_cities[,"City"]) 
fig <- fig %>%
  layout(
    mapbox = list(
      style = 'open-street-map',
      zoom =2.5,
      center = list(lon = -88, lat = 34))) 

fig

Today:

Leaflet🍃 Interactivity

plotly
leaflet🍃
threejs
dygraphs

Session Info (August 27, 2024) Rendering:

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dygraphs_1.1.1.6     threejs_0.3.3        igraph_1.4.2        
[4] leaflet.extras_2.0.1 leaflet_2.1.2        plotly_4.10.4       
[7] ggplot2_3.5.1        fontawesome_0.5.2   

loaded via a namespace (and not attached):
 [1] zoo_1.8-12        tidyselect_1.2.1  xfun_0.42         purrr_1.0.1      
 [5] lattice_0.20-45   colorspace_2.1-0  vctrs_0.6.5       generics_0.1.3   
 [9] htmltools_0.5.7   viridisLite_0.4.2 emo_0.0.0.9000    yaml_2.3.8       
[13] base64enc_0.1-3   utf8_1.2.4        rlang_1.1.3       pillar_1.9.0     
[17] glue_1.6.2        withr_3.0.0       lifecycle_1.0.4   stringr_1.5.1    
[21] munsell_0.5.1     gtable_0.3.5      htmlwidgets_1.6.4 evaluate_0.23    
[25] knitr_1.45        fastmap_1.1.1     crosstalk_1.2.0   fansi_1.0.6      
[29] xts_0.14.0        scales_1.3.0      jsonlite_1.8.8    farver_2.1.1     
[33] digest_0.6.31     stringi_1.7.12    dplyr_1.1.4       grid_4.2.2       
[37] cli_3.6.0         tools_4.2.2       magrittr_2.0.3    lazyeval_0.2.2   
[41] tibble_3.2.1      crayon_1.5.2      tidyr_1.3.1       pkgconfig_2.0.3  
[45] ellipsis_0.3.2    data.table_1.15.2 timechange_0.3.0  lubridate_1.9.3  
[49] assertthat_0.2.1  rmarkdown_2.26    httr_1.4.7        rstudioapi_0.15.0
[53] R6_2.5.1          compiler_4.2.2