在 R 中使用 plotly 并排放置圆环图

Placing donut charts side by side using plotly in R

我根据以下 plotly 创建了圆环图:

library(plotly)
library(RColorBrewer)
test<-data_frame(Score=c("Green","Green","Yellow","Yellow","Clear","Clear","Red","Red"),Lang=c(rep("Eng",4),rep("Esp",4)))

test1<-data_frame(Score=c("Green","Yellow","Yellow","Yellow","Clear","Clear","Red","Red"),Lang=c(rep("Eng",4),rep("Esp",4)))

color_order<-c("Green","Clear","Yellow","Red")
colors<-c("#31a354","#bdbdbd","#fec44f","#de2d26")

a<-test %>%
mutate(Score=factor(Score,levels=color_order))%>%
arrange(Score)%>%
group_by(Score)%>%
summarize(count = n()) %>%
plot_ly(labels = ~Score,
      values = ~count,
      hoverinfo="skip",
      text = ~count,
      marker = list(colors = colors),
      legendgroup = ~Score) %>%
add_pie(hole = 0.6) %>%
layout(title = "test chart1", showlegend = TRUE,
     font=list(family="sans serif",color="#000"),
     plot_bgcolor="#f0f0f0",
     legend = list(orientation = 'h',font=list(size=28)),
     xaxis = list(title=paste0("Total: ",nrow(test)), showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
     yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

b<-test1 %>%
mutate(Score=factor(Score,levels=color_order))%>%
arrange(Score)%>%
group_by(Score)%>%
summarize(count = n()) %>%
plot_ly(labels = ~Score,
      values = ~count,
      hoverinfo="skip",
      text = ~count,
      marker = list(colors = colors),
      legendgroup = ~Score) %>%
add_pie(hole = 0.6) %>%
layout(title = "test chart2", showlegend = FALSE,
     font=list(family="sans serif",color="#000"),
     plot_bgcolor="#f0f0f0",
     legend = list(orientation = 'h',font=list(size=28)),
     xaxis = list(title=paste0("Total: ",nrow(test1)),showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
     yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

我试图将它们与情节 a 中的常见图例并排放置,并且我 运行 以下内容:

subplot(a,b,nrows = 1)

然而,我只看到一个似乎是两者结合的情节。我也尝试了这里的方法:,但它只是给了我一个甜甜圈中的初始风格甜甜圈。我怎样才能将它们与共同的传说并排放置?谢谢。

据此(https://plot.ly/r/pie-charts/),为了创建饼图子图,您需要使用域属性。您可以尝试这样的操作(根据您的需要调整 domain):

a <- test %>%
    mutate(Score=factor(Score,levels=color_order))%>%
    arrange(Score)%>%
    group_by(Score)%>%
    summarize(count = n())

b<-test1 %>%
    mutate(Score=factor(Score,levels=color_order))%>%
    arrange(Score)%>%
    group_by(Score)%>%
    summarize(count = n())

p <- plot_ly() %>%
    add_pie(data = a, labels = ~Score, values = ~count, hole = 0.6,
            name = "a", domain = list(x = c(0, 0.4), y = c(0.4, 1))) %>%
    add_pie(data = b, labels = ~Score, values = ~count, hole = 0.6,
            name = "b", domain = list(x = c(0.6, 1), y = c(0.4, 1)))  %>%
    layout(title = "test chart1", showlegend = TRUE,
           font=list(family="sans serif",color="#000"),
           plot_bgcolor="#f0f0f0",
           legend = list(orientation = 'h',font=list(size=28)),
           xaxis = list(title=paste0("Total: ",nrow(test)), showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
           yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))