Is there a way to keep a piechart subplot from changing its domain with Plotly in R using crosstalk?
The idea here is to have a choropleth map on the left side and a piechart on the right side. When I click on one country on the map, the piechart shows data from that country. I use a SharedData object and the link between both subplots works fine.
The problem is: The piechart subplot stays where it should for the first location Code in my dataframe (AUS in this case), but when I click on another country, the piechart moves to the center of the plot.
Maybe this is a bug or it's not implemented yet?
Here is my code:
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)))
I seem to have found a workaround that answers the question.
Then when defining the layout of the subplot it was important to define the number of rows and columns of the subplot grid. This by itself already solves the problem, as you can play with the number of columns. For further tinkering with the pie chart size, one can then use the domain list.
Here is the code:
I could not plot the piechart on the right like I wanted because I could not find a way to set up the domain right.
On the reference to layout.grid (https://plotly.com/r/reference/layout/#layout-grid), the 'roworder' entry refers to the way grid columns are enumerated: "Note that columns are always enumerated from left to right."
I think that if they could be enumerated from right to left I would be able to put the piechart on the right and set up its domain properly.
So probably "layout.grid.columnorder" should be implemented?