繁体   English   中英

R 中的 Plotly:当连接到具有串扰的等值线时,饼图子图会更改域

[英]Plotly in R: Piechart subplot changing domains when linked to choropleth with crosstalk

有没有办法使用串扰防止饼图子图在 R 中使用 Plotly 更改其域?

这里的想法是在左侧有一个等值线 map,在右侧有一个饼图。 当我在 map 上单击一个国家/地区时,饼图显示来自该国家/地区的数据。 我使用 SharedData object 并且两个子图之间的链接工作正常。

问题是:饼图子图停留在我的 dataframe(在本例中为 AUS)中的第一个位置代码的位置,但是当我单击另一个国家/地区时,饼图移动到 plot 的中心。

也许这是一个错误或尚未实施?

这是我的代码:

library(plotly)
library(crosstalk)

df <- data.frame(Code = rep(c("AUS", "BRA", "CAN", "USA"),each = 4),
             Category = rep(c("A","B","C","D"),4),
             Values = rep(c(10,15,5,20),each=4),
             Perc = c(10, 20, 20, 50,
                      35, 5, 15, 45,
                      5, 75, 5, 15,
                      60, 30, 10, 0))

shared_data <- SharedData$new(df, key = ~Code)

p1 <- shared_data %>%
  plot_geo(z = ~Values,
           zmin=0,
           zmax=20,
           color = ~Values,
           locations = ~Code,
           visible=T)

p2 <- shared_data %>%
  plot_ly(type = "pie",
          visible = T,
          showlegend = F,
          values = ~Perc,
          labels = ~Category,
          domain = list(x = c(0.5, 1),
                        y = c(0,1)),
          hole = 0.8,
          sort = F) %>%
  layout(autosize = T, geo = list(domain = list(x = c(0.5, 1),
                                                y = c(0,1)
                                                ))
         )

sp1 <- subplot(p1, p2) %>%
  hide_legend() %>%
  hide_colorbar() %>%
 layout(xaxis = list(domain=c(0,0.5)), #adding this does not work either
         xaxis2 = list(domain=c(0.5,1)))

我似乎找到了解决问题的解决方法。

  • 我删除了饼图的域。
  • 我必须首先在子图中 plot 饼图(饼图向左)。 (代码后的解释)
  • 我定义了每个子图的宽度(较小的饼图,较大的等值线)。

然后在定义子图的布局时,定义子图网格的行数和列数很重要。 这本身已经解决了问题,因为您可以使用列数。 为了进一步修改饼图的大小,可以使用域列表。

这是代码:

    g <- list(
  showframe = FALSE,
  showcoastlines = TRUE,
  coastlinecolor = toRGB("black"),
  projection = list(type = 'Mercator')
)

p1 <- shared_data %>%
  plot_geo(z = ~Values,
           zmin=0,
           zmax=20,
           color = ~Values,
           locations = ~Code,
           visible=T) %>%
  layout(autosize = T, geo = g)

 

p2 <- shared_data %>%
  plot_ly(type = "pie",
          visible = T,
          showlegend = F,
          values = ~Perc,
          labels = ~Category,
          #domain = list(x = c(0, 0.3),
          #              y = c(0,1)),
          hole = 0,
          sort = F) %>%
  layout(autosize = T, geo = g)  # I have to set up 'geo' here, otherwise I get a warning in the subplot. Probably to match 'geo' from the choropleth?

sp1 <- subplot(p2, p1, # notice I plot the piechart (p2) first
             widths=c(0.25, 0.75)) %>% # here I set up the size of each subplot
  hide_legend() %>%
  hide_colorbar() %>%
 layout(grid = list(rows = 1, 
               columns = 3#, #setting columns as 2 and changing the domain also works 
               #domain = list(x = c(0,1),
               #              y = c(0,1))
               )
   )

sp1

我无法像我想要的那样 plot 右侧的饼图,因为我找不到正确设置域的方法。

在对 layout.grid 的引用( https://plotly.com/r/reference/layout/#layout-grid )中,“roworder”条目是指枚举网格列的方式: “请注意,列总是从左到右。”

我认为,如果可以从右到左枚举它们,我将能够将饼图放在右侧并正确设置其域。

那么可能应该实施“layout.grid.columnorder”吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM