Home > OS >  Pie-Donut Chart in R
Pie-Donut Chart in R

Time:04-08

I would like to create a Pie-Donut chart like the next one:

enter image description here

For this I am using the next code:

PieDonut(df1, aes(Species, Costs, count=Entries),
         ratioByGroup = F, explode = 1, r0 = 0.5, r1 = 0.7)

But I am not getting the two different (independent) charts. This is the result: enter image description here

As you can see the ratios of both groups are not the same: The first donut should be according to the variable "Costs" and the second "Entries"

This is the data I am using:

structure(list(Species = c("Cercopagis pengoi", "Dreissena polymorpha"
), Costs = c(0.27, 33.27), Entries = c(7L, 5L), fraction = c(0.00805008944543828, 
0.991949910554562), ymax = c(0.00805008944543828, 1), ymin = c(0, 
0.00805008944543828)), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

I would really appreciate some help, Thanks in advance.

CodePudding user response:

Here I got a plotly option. You can create two pies with plotly with one for the hole and one for the outer circle using two add_pie. You can use the following code:

library(plotly)
library(dplyr)

plot_ly(df1) %>%
  add_pie(labels = ~`Species`, values = ~`Costs`, 
          type = 'pie', hole = 0.7, sort = F,
          marker = list(line = list(width = 2))) %>%
  add_pie(df1, labels = ~`Species`, values = ~`Entries`, 
          domain = list(
            x = c(0.15, 0.85),
            y = c(0.15, 0.85)),
          sort = F) %>%
  layout(title = "Chart", 
         legend = list(title = list(text = "Species")))

Output:

enter image description here

Data

df1 <- structure(list(Species = c("Cercopagis pengoi", "Dreissena polymorpha" ), Costs = c(0.27, 33.27), Entries = c(7L, 5L), fraction = c(0.00805008944543828,  0.991949910554562), ymax = c(0.00805008944543828, 1), ymin = c(0,  0.00805008944543828)), row.names = c(NA, -2L), class = c("tbl_df",  "tbl", "data.frame"))
  • Related