我试图在阴谋中并排获得两个甜甜圈。但是,我只得到一张图片。关于我失踪的任何建议?
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
当前输出:
答案 0 :(得分:5)
你这么近。这是您的代码改编为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&#39的例子),你会添加
ax <- list(showline= TRUE)
plot_ly(...) %>% add_trace(...) %.%
layout(xaxis = ax, yaxis = ax)
它的更多选项,在axes页面上的sapmles。
答案 1 :(得分:1)
如果你采用r2evans
解决方案,可以按照以下方式将其与您的问题相匹配:
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")
p2<- subplot(
plot_ly(df1, labels = category, values = count, type = "pie", hole = 0.6, showlegend = TRUE,
domain = list(x = c(0, 0.5), y = c(0, 1))),
add_trace(data = df2, labels = group, values = count, type = "pie", hole = 0.6,
domain = list(x = c(0.5, 1), y = c(0, 1))),
margin = 0.05)
p2
结果: