R Plotly 两个并排的图

标签 r plot plotly

我试图在 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

电流输出:

enter image description here

最佳答案

你是如此接近。这是您的代码对 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)))

side-by-side pies

(他们使用 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/

相关文章:

r - 在双边案例交叉设计中创建控制日期

r - 绘制常规二维图,但添加第三维作为热图

python - Jupyter notebook 不显示图形..(只是图形)

ios - 核心图:如何绘制 Y 轴平均值的图表?

python - Pandas :在x轴上绘制时间直方图

python - 带有 plotly 的等值线 map

python - 如何获得 Plotly 图的 x 轴范围?

r - 当参数是语言对象时,通过 do.call 添加 ggtitle

R:for循环中的文本进度条

r - 将三列转换为 unix 时间 R