ggplot2 - Nested pie chart in R with counts also - Stack Overflow

I have been using this data set:Classes ‘data.table’ and 'data.frame':38 obs. of2 variab

I have been using this data set:

Classes ‘data.table’ and 'data.frame':  38 obs. of  2 variables:
 $ Class: chr  "ADTKD" "ADTKD" "Ciliopathy" "Cystic" ...
 $ Gene : chr  "REN" "UMOD" "NPHP1" "OFD1" ...
 - attr(*, ".internal.selfref")=<externalptr> 

I then run the following code:

library(ggplot2)
library(webr)
library(dplyr)
library(data.table)
library(dplyr)
library(ggpie)
devtools::install_github("jassler/webr")
data <- fread("unexplained ESRF/table.txt")

data_for_plot <- data %>%
  count(Class, Gene) %>%
  group_by(Class) %>%
  mutate(perc = n / sum(n),                # Percentage for inner pie (genes within classes)
         ypos = cumsum(perc) - 0.5 * perc) # Position labels inside each slice

class_summary <- data %>%
  count(Class) %>%
  mutate(perc = n / sum(n),                # Percentage for outer pie (classes)
         ypos = cumsum(perc) - 0.5 * perc) # Position labels outside each class slice

I then run:

PieDonut(data, aes(pies=Class, donuts=Gene), labelposition = 1,
         showRatioDonut = T, showRatioPie = T, ratioByGroup = F, showPieName = F)

And get: [![enter image description here][1]][1]

I would like the same but with the absolute count per category also.

I have tried following: How to show values instead of percentages and the sum in the center in PieDonut?

By running:

PieDonut(data,aes(Class, Gene), showRatioPie = "absolute", showRatioDonut = function(a,r) scales::scientific(a))

But I get this error:

> PieDonut(data,aes(Class, Gene), showRatioPie = "absolute", showRatioDonut = function(a,r) scales::scientific(a))
Error in if (showRatioPie) { : argument is not interpretable as logical

Not sure what I am doing wrong?

data: > dput(x)
structure(list(Class = c("ADTKD", "ADTKD", "Ciliopathy", "Cystic", 
"ADTKD", "Ciliopathy", "ADTKD", "Cystic", "ADTKD", "Systemic", 
"Glomerulopathy", "Collagenopathy", "Tubulopathy", "Systemic", 
"Ciliopathy", "Collagenopathy", "Glomerulopathy", "Cystic", "Tubulopathy", 
"Glomerulopathy", "Collagenopathy", "CAKUT", "Tubulopathy", "ADTKD", 
"Collagenopathy", "Collagenopathy", "CAKUT", "Ciliopathy", "Glomerulopathy", 
"Ciliopathy", "CAKUT", "Collagenopathy", "Cystic", "Collagenopathy", 
"Glomerulopathy", "ADTKD", "Collagenopathy", "CAKUT"), Gene = c("REN", 
"UMOD", "NPHP1", "OFD1", "UMOD", "NPHP1", "UMOD", "TSC2;PKD1", 
"UMOD", "EVC", "LMX1B", "COL4A4", "CLDN16", "GLA", "NPHP4", "COL4A4", 
"INF2", "PKD2", "CLDN16", "INF2", "COL4A4", "SALL1", "CLCN5", 
"UMOD", "COL4A4", "COL4A5", "PPP1R12A", "NPHP1", "TRIM8", "NPHP3", 
"17q12", "COL4A5", "PKD1", "COL4A3", "SMARCAL1", "UMOD", "COL4A5", 
"PAX2")), row.names = c(NA, -38L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x5556b1306180>)

Many thanks!

  [1]: .png

I have been using this data set:

Classes ‘data.table’ and 'data.frame':  38 obs. of  2 variables:
 $ Class: chr  "ADTKD" "ADTKD" "Ciliopathy" "Cystic" ...
 $ Gene : chr  "REN" "UMOD" "NPHP1" "OFD1" ...
 - attr(*, ".internal.selfref")=<externalptr> 

I then run the following code:

library(ggplot2)
library(webr)
library(dplyr)
library(data.table)
library(dplyr)
library(ggpie)
devtools::install_github("jassler/webr")
data <- fread("unexplained ESRF/table.txt")

data_for_plot <- data %>%
  count(Class, Gene) %>%
  group_by(Class) %>%
  mutate(perc = n / sum(n),                # Percentage for inner pie (genes within classes)
         ypos = cumsum(perc) - 0.5 * perc) # Position labels inside each slice

class_summary <- data %>%
  count(Class) %>%
  mutate(perc = n / sum(n),                # Percentage for outer pie (classes)
         ypos = cumsum(perc) - 0.5 * perc) # Position labels outside each class slice

I then run:

PieDonut(data, aes(pies=Class, donuts=Gene), labelposition = 1,
         showRatioDonut = T, showRatioPie = T, ratioByGroup = F, showPieName = F)

And get: [![enter image description here][1]][1]

I would like the same but with the absolute count per category also.

I have tried following: How to show values instead of percentages and the sum in the center in PieDonut?

By running:

PieDonut(data,aes(Class, Gene), showRatioPie = "absolute", showRatioDonut = function(a,r) scales::scientific(a))

But I get this error:

> PieDonut(data,aes(Class, Gene), showRatioPie = "absolute", showRatioDonut = function(a,r) scales::scientific(a))
Error in if (showRatioPie) { : argument is not interpretable as logical

Not sure what I am doing wrong?

data: > dput(x)
structure(list(Class = c("ADTKD", "ADTKD", "Ciliopathy", "Cystic", 
"ADTKD", "Ciliopathy", "ADTKD", "Cystic", "ADTKD", "Systemic", 
"Glomerulopathy", "Collagenopathy", "Tubulopathy", "Systemic", 
"Ciliopathy", "Collagenopathy", "Glomerulopathy", "Cystic", "Tubulopathy", 
"Glomerulopathy", "Collagenopathy", "CAKUT", "Tubulopathy", "ADTKD", 
"Collagenopathy", "Collagenopathy", "CAKUT", "Ciliopathy", "Glomerulopathy", 
"Ciliopathy", "CAKUT", "Collagenopathy", "Cystic", "Collagenopathy", 
"Glomerulopathy", "ADTKD", "Collagenopathy", "CAKUT"), Gene = c("REN", 
"UMOD", "NPHP1", "OFD1", "UMOD", "NPHP1", "UMOD", "TSC2;PKD1", 
"UMOD", "EVC", "LMX1B", "COL4A4", "CLDN16", "GLA", "NPHP4", "COL4A4", 
"INF2", "PKD2", "CLDN16", "INF2", "COL4A4", "SALL1", "CLCN5", 
"UMOD", "COL4A4", "COL4A5", "PPP1R12A", "NPHP1", "TRIM8", "NPHP3", 
"17q12", "COL4A5", "PKD1", "COL4A3", "SMARCAL1", "UMOD", "COL4A5", 
"PAX2")), row.names = c(NA, -38L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x5556b1306180>)

Many thanks!

  [1]: https://i.sstatic/XICk8vFc.png
Share Improve this question edited Mar 6 at 17:38 stefan 127k6 gold badges38 silver badges76 bronze badges Recognized by R Language Collective asked Mar 6 at 16:46 tacrolimustacrolimus 5322 silver badges15 bronze badges 2
  • 1 The arguments showRatioPie & showRatioDonut are expecting a logical value (TRUE or FALSE). – Susan Switzer Commented Mar 6 at 18:05
  • 1 @SusanSwitzer OP is probably referring to my solution, where I changed a specific part of the package itself such that the arguments also accept non-logical values. Unfortunately, the project maintainer has not yet responded to my merge request of my fork. – Felix Jassler Commented Mar 7 at 9:21
Add a comment  | 

1 Answer 1

Reset to default 1

One option would be to compute the counts and proportions manually then "add" them to the category names.

library(ggplot2)
library(webr)
library(dplyr, warn = FALSE)

ratio_class <- data |>
  count(Class, name = "count_class") |>
  mutate(
    ratio_class = count_class / sum(count_class)
  )

data |>
  count(Class, Gene, name = "count_gene") |>
  left_join(
    ratio_class,
    by = "Class"
  ) |>
  mutate(
    ratio_gene = count_gene / sum(count_gene),
    label_class = sprintf("%s\n(%s, %s)", Class, scales::percent(ratio_class), count_class),
    label_gene = sprintf("%s\n(%s, %s)", Gene, scales::percent(ratio_gene), count_gene)
  ) |>
  PieDonut(
    aes(
      pies = label_class, donuts = label_gene, count = count_gene
    ),
    labelposition = 1,
    showRatioDonut = FALSE,
    showRatioPie = FALSE,
    ratioByGroup = FALSE,
    showPieName = FALSE
  )

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961638a4603422.html

相关推荐

  • ggplot2 - Nested pie chart in R with counts also - Stack Overflow

    I have been using this data set:Classes ‘data.table’ and 'data.frame':38 obs. of2 variab

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信