我试图在 plotly 中并排获得两个 donut chart 。但是,我只得到一张图像。关于我所缺少的任何建议?
library(dplyr)
library(plotly)
df1 <- as.data.frame(matrix(data = c("a","b", "c", 34,28, 29), nrow = 3, ncol = 2))
colnames(df1) <- c("category", "count")
df2 <- as.data.frame(matrix(data = c("Q","F", "G", 29,50, 76), nrow = 3, ncol = 2))
colnames(df2) <- c("group", "count")
p <- subplot(
plot_ly(df1, labels = category, values = count, type = "pie", hole = 0.6, showlegend = TRUE),
plot_ly(df2, labels = group, values = count, type = "pie", hole = 0.6, showlegend = TRUE),
margin = 0.05,
nrows = 2
)
p
电流输出:
最佳答案
你是如此接近。这是您的代码对 Plotly's pie charts examples 的改编。 :
plot_ly(df1, labels = category, values = count, type = "pie", hole = 0.6, showlegend = TRUE,
domain = list(x = c(0, 1), y = c(0.5, 1))) %>%
add_trace(data = df2, labels = group, values = count, type = "pie", hole = 0.6, showlegend = TRUE,
domain = list(x = c(0, 1), y = c(0, 0.5)))
(他们使用
add_trace
而不是父 subplot
。)如果您想添加网格线(根据@Technophobe01 的示例),您可以添加
ax <- list(showline= TRUE)
plot_ly(...) %>% add_trace(...) %.%
layout(xaxis = ax, yaxis = ax)
更多的选择,在他们的 axes页。
关于R Plotly 两个并排的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39260482/