多个跟踪在plot_geo中不起作用

时间:2016-11-08 12:04:37

标签: r plotly

我正在尝试使用R plotly包版本4.5.2创建一个scattergeo贴图。地理范围是新南威尔士州(澳大利亚),我需要在这个州内包括区域边界,所以我不能使用现有的一个地理范围。

我尝试的方法是使用add_paths()绘制区域边界(应用于带有边界信息的数据框),然后使用add_markers(inherit = FALSE)覆盖散点图数据(到不同的数据框)。然而,最终结果不会为标记呈现许多所需的视觉属性(例如,标记颜色是灰色的[根据边界线],而不是绿色和蓝色,并且尺寸属性也被忽略)。看第一个屏幕截图。

但是当我从绘图管道中删除add_paths()时,标记会完美呈现。请参见第二个屏幕截图。

有关如何使这项工作的任何建议?具有简化数据帧的可重现代码如下。会议信息也在下面。

plotly screenshot - with grey boundaries by markers rendered incorrectly

plotly screenshot - markers rendered correctly but no boundaries

library(plotly)

# data for "scatter bubbles"
dat <- data.frame(
  AVIATION_ID = c("AYAM", "BBAX", "BDDX", "BEGX", "BELL", "CBMR"),
  LATITUDE = c(-29.4333, -37.0016, -35.4253, -36.6722, -34.3691, -35.9371),
  LONGITUDE = c( 153.3633, 149.2336, 149.7835, 149.8191, 150.9291, 148.3779),
  RMSE = c(1.303055, 2.114968, 2.459223, 2.841459, 1.238029, 2.125925),
  RMSE_group = c("[1,2)", "[2,3)", "[2,3)", "[2,3)", "[1,2)", "[2,3)"),
  tooltip_label = c("AYAM<br>RMSE: 1.3",  "BBAX<br>RMSE: 2.11", "BDDX<br>RMSE: 2.46",
                    "BEGX<br>RMSE: 2.84", "BELL<br>RMSE: 1.24", "CBMR<br>RMSE: 2.13")
)

# district boundaries
boundaries <- data.frame(
  district = c("NSW_01", "NSW_01", "NSW_01", "NSW_01", "NSW_01", "NSW_02", "NSW_02", "NSW_02", "NSW_02"),
  lat = c(-29, -32, -32.5, -29, -29, -37, -35, -34, -37),
  lon = c(153.5, 153, 150.5, 150, 153.5, 149, 148, 151, 149)
)

# geo layout
lat_range <- c(-38, -27.5)
lon_range <- c(140, 155)
g1 <- list(
  showcoastlines = FALSE,
  lonaxis = list(
    showgrid = TRUE,
    gridwidth = 0.5,
    range = lon_range,
    dtick = 5
  ),
  lataxis = list(
    showgrid = TRUE,
    gridwidth = 0.5,
    range = lat_range,
    dtick = 5
  )
)

# scatter marker attributes
rmse_markers <- list(
  line = list(color = "black", opacity = .7, width = 1.75),
  opacity = .8,
  sizemode = "diameter"
)

# plot
plot_geo() %>%
  add_paths(data = boundaries, x = ~lon, y = ~lat, color = ~district,
            colors = c("grey", "grey"),
            line = list(width = 2), 
            showlegend = FALSE) %>%
  add_markers(data = dat, x = ~LONGITUDE, y = ~LATITUDE, color = ~RMSE_group,
              colors = c("[0,1)" = "yellow", "[1,2)" = "green", "[2,3)" = "blue", "[3,infty)" = "red"),
              size = ~RMSE, sizes= 8*c(min(dat$RMSE),max(dat$RMSE)), marker = rmse_markers,
              text = ~tooltip_label, hoverinfo = "text",
              inherit = FALSE) %>%
  layout(geo = g1)

注意:在一个较旧版本的情节中,我能够让它工作,但我在v4.5.2中试过没有任何运气。

会话信息:

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

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

other attached packages:
[1] plotly_4.5.2  ggplot2_2.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.6       tidyr_0.6.0       viridisLite_0.1.3 digest_0.6.10     dplyr_0.5.0       assertthat_0.1    grid_3.3.1       
 [8] plyr_1.8.4        R6_2.1.2          jsonlite_1.0      gtable_0.2.0      DBI_0.4-1         magrittr_1.5      scales_0.4.0     
[15] httr_1.2.1        lazyeval_0.2.0    tools_3.3.1       htmlwidgets_0.7   purrr_0.2.2       munsell_0.4.3     yaml_2.1.13      
[22] base64enc_0.1-3   colorspace_1.2-6  htmltools_0.3.5   tibble_1.1

1 个答案:

答案 0 :(得分:0)

通过在启动对象时指定比例来解决问题。

我在github上发布了这个问题的简化示例,cpsievert回答了这个问题:

https://github.com/ropensci/plotly/issues/790