Error plotting PieDonut plot in R

Hi,

I am interested in plotting the PieDonut graph in R, but I keep getting the below error and unsuccessful in fixing this. Am I missing anything? Please assist. Thank you.

library(ggplot2)
library(webr)
library(dplyr)
dput(Data_summary)
structure(list(Type = c("Array", "Array", "Array", "Array", "Array", 
                        "Array", "Array", "Array", "Array", "Array", "Array", "Array", 
                        "Array", "Array", "Array", "Array", "Array", "Seq", "Seq", "Seq", 
                        "Seq", "Seq"), Pop = c("A", "A", "A", "A", "A", "A", "A", "A", 
                                               "P", "P", "P", "P", "P", "P", "N", "N", "B", "A", "A", "A", "A", 
                                               "N"), Sample = c("WB", "WB", "WB", "WB", "PB", "PB", "PB", "PB", 
                                                                "WB", "WB", "WB", "WB", "WB", "PB", "WB", "WB", "WB", "WB", "WB", 
                                                                "WB", "PB", "WB"), Design = c("Vivo", "Vivo", "Vivo", "Vivo", 
                                                                                              "Vivo", "Vivo", "Vitro", "Vitro", "Vivo", "Vivo", "Vivo", "Vivo", 
                                                                                              "Vitro", "Vivo", "Vivo", "Vivo", "Vivo", "Vivo", "Vivo", "Vivo", 
                                                                                              "Vivo", "Vivo"), Condition = c("GroupA", "GroupB", "GroupC", 
                                                                                                                             "GroupD", "GroupA", "GroupB", "GroupA", "GroupD", "GroupA", "GroupB", 
                                                                                                                             "GroupC", "GroupD", "GroupA", "GroupD", "GroupA", "GroupD", "GroupD", 
                                                                                                                             "GroupA", "GroupB", "GroupD", "GroupA", "GroupA"), Frequency = c(10, 
                                                                                                                                                                                              3, 3, 2, 2, 1, 1, 1, 2, 9, 3, 1, 1, 5, 3, 1, 1, 8, 1, 1, 1, 1
                                                                                                                             )), row.names = c(NA, -22L), class = "data.frame")
#>     Type Pop Sample Design Condition Frequency
#> 1  Array   A     WB   Vivo    GroupA        10
#> 2  Array   A     WB   Vivo    GroupB         3
#> 3  Array   A     WB   Vivo    GroupC         3
#> 4  Array   A     WB   Vivo    GroupD         2
#> 5  Array   A     PB   Vivo    GroupA         2
#> 6  Array   A     PB   Vivo    GroupB         1
#> 7  Array   A     PB  Vitro    GroupA         1
#> 8  Array   A     PB  Vitro    GroupD         1
#> 9  Array   P     WB   Vivo    GroupA         2
#> 10 Array   P     WB   Vivo    GroupB         9
#> 11 Array   P     WB   Vivo    GroupC         3
#> 12 Array   P     WB   Vivo    GroupD         1
#> 13 Array   P     WB  Vitro    GroupA         1
#> 14 Array   P     PB   Vivo    GroupD         5
#> 15 Array   N     WB   Vivo    GroupA         3
#> 16 Array   N     WB   Vivo    GroupD         1
#> 17 Array   B     WB   Vivo    GroupD         1
#> 18   Seq   A     WB   Vivo    GroupA         8
#> 19   Seq   A     WB   Vivo    GroupB         1
#> 20   Seq   A     WB   Vivo    GroupD         1
#> 21   Seq   A     PB   Vivo    GroupA         1
#> 22   Seq   N     WB   Vivo    GroupA         1

# Pie-Donut chart
PieDonut(Data_summary, aes(Condition, Type, count=Frequency), title = "Distribution")

Error

Error in spread():
! Each row of output must be identified by a unique combination of keys.
Keys are shared for 20 rows:

  • 1, 2, 3, 4, 5, 6
  • 7, 8, 9
  • 10, 11, 12
  • 14, 15
  • 17, 18, 19, 20, 21, 22
    Run rlang::last_error() to see where the error occurred.

Created on 2023-06-30 with reprex v2.0.2

I have never used PieDonut() but I think the problem in that you do not have unique combinations of Condition and Type For example, row 1 and row 5 both have Condition == GroupA and Type == Array`. My guess is that you cannot use PieDonut.

If you describe what you are doing in subject matter terms someone here may be able to suggest an alternative.

@jrkrideau thank you for the response. Basically, I am interested to plot a concentric pie chart or nested pie chart to show the frequency of each column. Here are some of the examples:

  1. PieDonut plots
  2. Nested Pie Chart in R
  3. How to Create and Publish a Nested Pie Chart in R with the Plotly Package
  4. Multiple Levels Nested PieChart with Annotation in R

It looks only have 7 unique combinations it your data set.

Calling your data dat1. Note: for reasons of laziness, I am treating treating this as a data.table rather than a data.frame.

So you will need to do

install.packages("data.table")

Your Data in data.table format

dat1  <-  structure(list(Type = c("Array", "Array", "Array", "Array", "Array", 
"Array", "Array", "Array", "Array", "Array", "Array", "Array", 
"Array", "Array", "Array", "Array", "Array", "Seq", "Seq", "Seq", 
"Seq", "Seq"), Pop = c("A", "A", "A", "A", "A", "A", "A", "A", 
"P", "P", "P", "P", "P", "P", "N", "N", "B", "A", "A", "A", "A", 
"N"), Sample = c("WB", "WB", "WB", "WB", "PB", "PB", "PB", "PB", 
"WB", "WB", "WB", "WB", "WB", "PB", "WB", "WB", "WB", "WB", "WB", 
"WB", "PB", "WB"), Design = c("Vivo", "Vivo", "Vivo", "Vivo", 
"Vivo", "Vivo", "Vitro", "Vitro", "Vivo", "Vivo", "Vivo", "Vivo", 
"Vitro", "Vivo", "Vivo", "Vivo", "Vivo", "Vivo", "Vivo", "Vivo", 
"Vivo", "Vivo"), Condition = c("GroupA", "GroupB", "GroupC", 
"GroupD", "GroupA", "GroupB", "GroupA", "GroupD", "GroupA", "GroupB", 
"GroupC", "GroupD", "GroupA", "GroupD", "GroupA", "GroupD", "GroupD", 
"GroupA", "GroupB", "GroupD", "GroupA", "GroupA"), Frequency = c(10, 
3, 3, 2, 2, 1, 1, 1, 2, 9, 3, 1, 1, 5, 3, 1, 1, 8, 1, 1, 1, 1
)), row.names = c(NA, -22L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x56522adf5f60>)

we can compress it into the 7 unique elements and plot a simple barplot.

library(data.table); library(tidyverse)

dat2  <- dat1[, keyby = .(Type, Condition), .(sum_freq = sum(Frequency))]

ggplot (dat2, aes( Type, sum_freq, fill = Condition)) +
  geom_col(position = "dodge") +
    ylab("Frequency")

I believe you can do the same compression using the tidyverse group_by command
I do not have library(webr) installed but I think you should be able to get your doughnut plot using dat2.

1 Like

@jrkrideau thank you very much. I will try this.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.