Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

r - Plotting Donut Chart with Plotly

I want to plot this two pie charts on the same plot. Below you can see my data:

library(plotly)
library(dplyr)
library(grid)

    Share<- structure(list(Y = c(2018, 2018, 2019, 2019), Categorie = c("Before","After", "Before","After"), Share = c(1.0,99., 3, 97)), row.names = c(NA,-4L), class = c("tbl_df", "tbl", "data.frame")) 

I try with this lines of code but I can't find solution.

 fig1 <- plot_ly(Share, labels = ~Categorie , values = ~Share, type = 'pie')

 fig2 <- plot_ly(Share, labels = ~Categorie , values = ~Share, type = 'pie')
          
          
PLOT<-subplot(fig1,fig2,nrows=2,margin = 0.05)
      

So can anybody help me how to solve this and make plot like pic below but with Donut Charts:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try adjusting the following example to your data: Placing donut charts side by side using plotly in R

suppressPackageStartupMessages(invisible(
    lapply(c("plotly", "dplyr"), require, character.only = TRUE)))

Share <- structure(list(Y = c(2018, 2018, 2019, 2019), 
                        Categorie = c("Before","After", "Before","After"), 
                        Share = c(1.0,99., 3, 97)), 
                   row.names = c(NA,-4L), class = c("tbl_df", "tbl", "data.frame")) 

p <- plot_ly() %>%
    add_pie(data=Share, labels = ~Categorie , values = ~Share, hole = 0.6,
            name = "fig1", domain = list(x = c(0, 0.4), y = c(0.4, 1))) %>%
    add_pie(data=Share, labels = ~Categorie , values = ~Share, hole = 0.6,
            name = "fig2", domain = list(x = c(0.6, 1), y = c(0.4, 1)))  %>%
    layout(title = "Donut sub-plots", showlegend = TRUE,
           font=list(family="sans serif", color="#000"),
           plot_bgcolor="#f0f0f0",
           legend = list(orientation = 'h',font=list(size=28)),
           xaxis = list(title="", showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
           yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
p

Created on 2021-01-21 by the reprex package (v0.3.0)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...