情节:条形图和饼图并排 [英] Plotly: Bar and pie charts side by side

查看:104
本文介绍了情节:条形图和饼图并排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用R中plotly包中的subplot函数并排绘制条形图和饼图.但是,大饼图绘制在图形的中心,并覆盖条形图阴谋.

I would like to plot a bar and a pie chart side by side using the subplot function in the plotly package in R. However, a big pie chart is plotted in the center of the graph, overlaying the bar plot.

这是示例代码:

Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)

bar <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'stack')

pie <- plot_ly(data, labels = ~Animals, values = ~LA_Zoo, type = 'pie', hole = 0.6)

subplot(bar, pie)

我该如何解决?

推荐答案

让我们尝试一下-

library(plotly)

#sample data
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)

#plot
plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar') %>%
  layout(yaxis = list(title = 'Count'), xaxis = list(domain = c(0, 0.5)), barmode = 'stack') %>%
  add_trace(data, labels = ~Animals, values = ~LA_Zoo, type = 'pie', hole = 0.6,
            domain = list(x = c(0.5, 1)))

这篇关于情节:条形图和饼图并排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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