Plotly: Bar and pie charts side by side

谁说胖子不能爱 提交于 2019-12-10 17:25:47

问题


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.

Here is a sample code:

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)

How can I fix it?


回答1:


Let's try this -

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)))


来源:https://stackoverflow.com/questions/47063105/plotly-bar-and-pie-charts-side-by-side

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!