簡體   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