一行中有多个绘图饼图 [英] Multiple plotly pie charts in one row
问题描述
我正在尝试使用plotly包中的subplot
函数将两个饼形图布置在一行中,但是生成的图不是我期望的.我能够对折线图执行相同的操作,但是在单行中绘制两个饼图时遇到了麻烦.以下是我的代码.
I am trying to arrange two pie charts in a single row using the subplot
function from the plotly package but the resultant plot is not what I expect. I was able to do the same for line charts, etc but I am facing trouble plotting two pie charts in a single row. Following is the code that I have.
ds_r <- data.frame(labels = c("Baseline", "DTC", "Detailing", "Flex"),
values = c(63.5, 8.5, 20.6, 7.4))
ds_l <- data.frame(labels = c("Baseline"),
values = c(100))
plot_right <- plot_ly(ds_r, labels = labels, values = values, type = "pie") %>%
layout(title = "Sales - Decomposed")
plot_left <- plot_ly(ds_l, labels = labels, values = values, type = "pie") %>%
layout(title = "Total Sales")
subplot(plot_left, plot_right, nrows = 2)
输出为
如果选中图像底部的红色框,则有两组轴,这意味着有两个图,但是它们可能会重叠或发生某些变化.
If you check the red boxes at the bottom of the image, there are two sets of axes which means there are two plots but they are probably getting overlapped or something.
我希望输出是在单行中没有任何轴的两个饼图.有帮助或指示吗?
I want the output to be two pie charts in a single row without any axes. Any help or direction?
推荐答案
plotly
的开发版本中提供了新的改进的subplot
功能,但看来错误"仍然存在.我不确定饼图是否能很好地工作.我在Github上提交了问题.
There is new improved subplot
functionality coming in the development version of plotly
, but it appears the "bug" persists. I am not sure though how well it works with pie charts. I filed an issue on Github.
# devtools::install_github('ropensci/plotly")
library(plotly)
ds_r <- data.frame(labels1 = c("Baseline", "DTC", "Detailing", "Flex"),
values1 = c(63.5, 8.5, 20.6, 7.4))
ds_l <- data.frame(labels2 = c("Baseline"),
values2 = c(100))
p <- plot_ly(ds_r, labels = ~labels1, values = ~values1, type = "pie",
showlegend = F)
p2 <- plot_ly(ds_l,labels = ~labels2, values = ~values2, type = "pie",
showlegend = F)
subplot(p, p2, nrows = 1)
有关subplot
的更多详细信息,请参见子情节小插图
For more detail on subplot
, see the subplot vignette.
这篇关于一行中有多个绘图饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!