创建按钮以子集和更新R绘图

时间:2018-08-20 14:54:24

标签: r button r-plotly

我正在尝试在地图上绘制美国的发电量。我的数据包括坐标(latlon,计划容量(mw)和设施预计可运行的年份(current_year)。

我希望地图在计划设施的每个位置(latlon)都有标记-颜色和大小取决于设施(prime_mover)的类型和容量(mw)。我也希望在鼠标悬停时显示容量。最后,我想添加作为dt子集的按钮,并通过current_year更新地图。

这是我尝试过的:

数据:

> 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>)

代码:

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
  )

由于某种原因,这在RStudio中无法正确显示,但是我可以将绘图另存为.html对象并在浏览器中查看(以下输出是Chrome的屏幕截图)。

输出:

enter image description here

据我了解,要添加用于子集dt和更新绘图的按钮,我需要添加以下内容(用于在简单的线条图中更改颜色),但是我不确定哪种语法等。用于我的目的。

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"))) )

1 个答案:

答案 0 :(得分:1)

这里有什么可以帮助您的。我只做过头三年,但可以轻松扩展。顺便说一下,您的数据集中未提供prime_mover

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