Reputation: 11686
I'm trying to get two donut plots side by side in plotly. However, I'm only getting one image. Any advice on what I'm missing?
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
Current Output:
Upvotes: 4
Views: 5587
Reputation: 8676
If you take r2evans
solution you can match it to your question as follows:
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
Result:
Upvotes: 1
Reputation: 160447
You're so close. This is an adaptation of your code to 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)))
(They are using add_trace
instead of a parent subplot
.)
If you want to add gridlines (per @Technophobe01's example), you would add
ax <- list(showline= TRUE)
plot_ly(...) %>% add_trace(...) %.%
layout(xaxis = ax, yaxis = ax)
Many more options for it, sapmles on their axes page.
Upvotes: 5