【问题标题】:Plotly in R: Piechart subplot changing domains when linked to choropleth with crosstalkR中的情节:当通过串扰链接到等值线时,饼图子图改变域
【发布时间】:2021-04-16 00:35:54
【问题描述】:

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

这里的想法是在左侧有一个等值线图,在右侧有一个饼图。 当我点击地图上的一个国家时,饼图会显示该国家的数据。 我使用 SharedData 对象,两个子图之间的链接工作正常。

问题是: 对于我的数据框中的第一个位置代码(在本例中为 AUS),饼图子图保持在应有的位置,但是当我单击另一个国家/地区时,饼图会移动到图的中心。

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

这是我的代码:

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

【问题讨论】:

    标签: r pie-chart r-plotly choropleth crosstalk


    【解决方案1】:

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

    • 我删除了饼图的域。
    • 我必须首先在子图中绘制饼图(饼图转到 剩下)。 (代码后说明)
    • 我定义了每个子图的宽度(饼图越小,饼图越大 等值线)。

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

    代码如下:

        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
    

    我无法按照我的意愿在右侧绘制饼图,因为我找不到正确设置域的方法。

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多