r - R中的情节:当连接到带有串扰的等值线时,饼图子图改变域
问题描述
有没有办法使用串扰来防止饼图子图在 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)))
解决方案
我似乎找到了解决问题的解决方法。
- 我删除了饼图的域。
- 我必须首先在子图中绘制饼图(饼图向左)。(代码后的解释)
- 我定义了每个子图的宽度(较小的饼图,较大的等值线)。
然后在定义子图的布局时,定义子图网格的行数和列数很重要。这本身已经解决了问题,因为您可以使用列数。为了进一步修改饼图的大小,可以使用域列表。
这是代码:
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”吗?
推荐阅读
- jira - 使用逻辑应用从 SharePoint 创建 JIRA 问题
- python - 如何在 python 中使用对象作为 vaue 进行多处理
- django - 'Post' 对象没有属性 'get_absolute_url' Django
- python - 在 Pandas 中打开和读取 zip 文件夹中的 txt 文件时出现问题
- arrays - 在c中将字符串转换为二维数组
- composer-php - 如何在 Windows 10 中更改作曲家版本
- react-native - 如何在 react-native 中保存和加载 tflite 版本的posenet?
- javascript - NestJS 事件发射器
- paddle-paddle - PaddleOCR 性能与 Tesseract 相比如何?
- python - 警告:张量流:模型是用形状构造的