Gautam
Gautam

Reputation: 2753

Create buttons to subset and update R plotly map

I'm trying to plot the US electricity generation capacity on a map. My data consists of the coordinates (lat, lon), planned capacity (mw) and the year when the facility is expected to be operational (current_year).

I want the map to have markers for each location (lat, lon) where a facility is planned - the color and size depend on the type of facility (prime_mover) and capcity (mw), respectively. I also want the capacity to be displayed when the mouse hovers over. Lastly, I want to add buttons that subset dt and update the map by current_year.

Here's what I've tried:

DATA:

> dput(dt[1:50,.(lat, lon, mw, current_year)])
structure(list(lat = c(33.5812648426403, 33.5812648426403, 34.0668345432655, 
33.6961126585264, 34.0668345432655, 41.2963261323817, 27.878260812094, 
39.5272127787272, 41.7952019373576, 41.298797861735, 42.6404243603087, 
44.7034406661987, 39.1547560972326, 39.5872026391932, 40.4542882818925, 
40.6479177474976, 35.6200162972977, 34.5709360758464, 35.2237865046451, 
32.7985594613211, 31.5559690263536, 31.5559690263536, 32.4392329624721, 
29.2475475692749, 28.508979373508, 41.5753456115723, 44.3516218012029, 
37.7541649917076, 33.0466017150879, 30.8503398895264, 33.5812648426403, 
41.4192822047642, 40.0061813195546, 42.6477045331682, 41.4192822047642, 
44.1539509513161, 40.0926746931232, 41.4942982991536, 40.7539427621024, 
41.7855738004049, 41.4942982991536, 37.9775902557373, 41.3185318840875, 
41.5753456115723, 40.4008912222726, 40.6867153712681, 33.0070703946627, 
33.0070703946627, 33.0070703946627, 32.2995414733887), lon = c(-111.886676718972, 
-111.886676718972, -118.294102238674, -117.736324001003, -118.294102238674, 
-73.3511804318895, -82.4752747291742, -86.497910563151, -70.2159778806898, 
-70.0584147135417, -70.983841879326, -93.0496198866102, -74.807897679946, 
-75.3653199479387, -74.4234486630088, -73.95051612854, -82.5088296007754, 
-82.6195589701335, -89.9929729260896, -97.2939638410296, -97.1812811957465, 
-97.1812811957465, -97.8751057216099, -95.4604983520508, -96.685354953342, 
-86.4192230224609, -86.1332654086026, -77.4094619750977, -114.984835205078, 
-90.0291446685791, -111.886676718972, -72.9225483485631, -76.7841037114461, 
-89.0048991612026, -72.9225483485631, -94.1275901794434, -80.0676585963515, 
-74.2516840154474, -74.0973572503953, -73.8303874333699, -74.2516840154474, 
-121.942034454346, -104.699122111003, -86.4192230224609, -112.594391305106, 
-76.1847250802176, -111.078885787573, -111.078885787573, -111.078885787573, 
-100.947525024414), mw = c(485.7, 323.8, 462, 462, 230, 375.7, 
784, 414, 330, 15.4, 60, 238, 321, 242, 430, 93, 382.4, 486, 
694, 471, 235.5, 471, 471, 100, 204, 10.8, 253, 374, 19, 400, 
510, 100, 469.8, 465.8, 561, 210, 1025, 470, 132, 1170, 12, 444.6, 
80, 476, 30, 250, 714, 241, 723, 477.8), current_year = c(2019, 
2018, 2020, 2020, 2020, 2019, 2021, 2018, 2019, 2019, 2021, 2018, 
2020, 2019, 2018, 2019, 2019, 2018, 2018, 2022, 2020, 2020, 2020, 
2019, 2019, 2019, 2020, 2018, 2018, 2019, 2025, 2018, 2018, 2020, 
2018, 2019, 2020, 2018, 2018, 2020, 2021, 2020, 2022, 2018, 2019, 
2019, 2024, 2023, 2025, 2021)), .Names = c("lat", "lon", "mw", 
"current_year"), row.names = c(NA, -50L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000000000330788>)

CODE:

g <- list(
  scope = 'usa',
  projection = list(type = 'Mercator'),
  showland = TRUE,
  landcolor = toRGB("gray85"),
  subunitwidth = 1,
  countrywidth = 1,
  subunitcolor = toRGB("white"),
  countrycolor = toRGB("white")
)

p <- plot_geo(dt, locationmode = 'USA-states', sizes = c(1, 250)) %>%
  add_markers(
    x = ~lon, y = ~lat,
    text = ~paste(mw),
    color = ~prime_mover,
    size = ~mw,
    hoverinfo = "text"
  ) %>%
  layout(
    title = 'Upcoming GT installations in the US<br>(Hover for plant name and year)', 
    geo = g
  )

For some reason this does not display properly in RStudio but I'm able to save the plot as an .html object and view in a browser (below output is a screenshot from Chrome).

OUTPUT:

enter image description here

From what I understand, to add buttons for subsetting dt and updating the plot, I need to add something like below (works for changing color in simple line plots) but I'm not sure what syntax etc. to use for my purpose.

updatemenus = list(
          list(
            type = "buttons",

                    buttons = list(

              list(method = "restyle",
                   args = list("line.color", "blue"),
                   label = "Blue"),

              list(method = "restyle",
                   args = list("line.color", "red"),
                   label = "Red"))) )

Upvotes: 0

Views: 1109

Answers (1)

MLavoie
MLavoie

Reputation: 9836

Here is something to help you. I only did for the first 3 years but can be easily expanded. By the way, prime_mover was not provided in your dataset.

plot_geo(locationmode = 'USA-states', sizes = c(1, 250)) %>%
    add_markers(data = subset(dt, current_year %in% c("2018")),
     x = ~lon, y = ~lat,
     text = ~paste(mw),
    # color = ~prime_mover,
     size = ~mw,
     hoverinfo = "text",
     name = "2018",
    showlegend = FALSE # if you don't want a legend
   ) %>%
   add_markers(data = subset(dt, current_year %in% c("2019")),
     x = ~lon, y = ~lat,
     text = ~paste(mw),
    # color = ~prime_mover,
     size = ~mw,
     hoverinfo = "text",
     name = "2019"
   ) %>%
   add_markers(data = subset(dt, current_year %in% c("2020")),
               x = ~lon, y = ~lat,
               text = ~paste(mw),
              # color = ~prime_mover,
               size = ~mw,
               hoverinfo = "text",
               name = "2020"
   ) %>%
   layout(title = 'Upcoming GT installations in the US<br>(Hover for plant name and year)', 
    geo = g,
     updatemenus = list(
       list(
         y = 0.8,
       #  type= 'buttons',
         buttons = list(
           list(
             method = "update",
             args = list(list(visible = c(TRUE, FALSE, FALSE))),
             label = "2018"),
           list(method = "update",
                args = list(list(visible = c(FALSE, TRUE, FALSE))),
                label = "2019"),

           list(method = "update",
                args = list(list(visible = c(FALSE, FALSE, TRUE))),
                label = "2020")))
     )
   )

enter image description here

Upvotes: 1

Related Questions