问题
I am trying to arrange two pie charts in a single row using the subplot
function from the plotly package but the resultant plot is not what I expect. I was able to do the same for line charts, etc but I am facing trouble plotting two pie charts in a single row. Following is the code that I have.
ds_r <- data.frame(labels = c("Baseline", "DTC", "Detailing", "Flex"),
values = c(63.5, 8.5, 20.6, 7.4))
ds_l <- data.frame(labels = c("Baseline"),
values = c(100))
plot_right <- plot_ly(ds_r, labels = labels, values = values, type = "pie") %>%
layout(title = "Sales - Decomposed")
plot_left <- plot_ly(ds_l, labels = labels, values = values, type = "pie") %>%
layout(title = "Total Sales")
subplot(plot_left, plot_right, nrows = 2)
The output is
If you check the red boxes at the bottom of the image, there are two sets of axes which means there are two plots but they are probably getting overlapped or something.
I want the output to be two pie charts in a single row without any axes. Any help or direction?
回答1:
library(plotly)
ds_r <- data.frame(labels1 = c("Baseline", "DTC", "Detailing", "Flex"),
values1 = c(63.5, 8.5, 20.6, 7.4))
ds_l <- data.frame(labels2 = c("Baseline"),
values2 = c(100))
df <- cbind(ds_r, ds_l)
plot_ly(df, labels = labels1, values = values1, type = "pie",
domain = list(x = c(0, 0.4)), showlegend = F) %>%
add_trace(labels = labels2, values = values2, type = "pie",
domain = list(x = c(0.6, 1)), showlegend = F)
I would add the picture, but plotlies are generally large files.
回答2:
There is new improved subplot
functionality coming in the development version of plotly
, but it appears the "bug" persists. I am not sure though how well it works with pie charts. I filed an issue on Github.
# devtools::install_github('ropensci/plotly")
library(plotly)
ds_r <- data.frame(labels1 = c("Baseline", "DTC", "Detailing", "Flex"),
values1 = c(63.5, 8.5, 20.6, 7.4))
ds_l <- data.frame(labels2 = c("Baseline"),
values2 = c(100))
p <- plot_ly(ds_r, labels = ~labels1, values = ~values1, type = "pie",
showlegend = F)
p2 <- plot_ly(ds_l,labels = ~labels2, values = ~values2, type = "pie",
showlegend = F)
subplot(p, p2, nrows = 1)
For more detail on subplot
, see the subplot vignette.
来源:https://stackoverflow.com/questions/38582112/multiple-plotly-pie-charts-in-one-row