一行中的多个 plotly 饼图 [英] Multiple plotly pie charts in one row

查看:24
本文介绍了一行中的多个 plotly 饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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 上提交了 issue.

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.

这篇关于一行中的多个 plotly 饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆