我想使用R中subplot
包中的plotly
函数并排绘制条形图和饼图。但是,在图形的中心绘制一个大饼图,覆盖条形图。
以下是示例代码:
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)
我该如何解决?
答案 0 :(得分:2)
让我们尝试一下 -
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)))