How to show values instead of percentages and the sum in the center in PieDonut?

71 Views Asked by At

I have this table:

   Tip_migrene Populacija                    lvl2   Troskovi
1           EM        Svi                     PZZ  574345839
2           EM        Svi                     SZZ 1151543136
3           EM        Svi        bolnicko_lecenje   69967985
4           EM        Svi dijagnosticke_procedure 1983183163
5           EM        Svi            testovi_krvi   57333171
6           HM        Svi                     PZZ  107825242
7           HM        Svi                     SZZ  174517582
8           HM        Svi        bolnicko_lecenje    9822562
9           HM        Svi dijagnosticke_procedure  286431165
10          HM        Svi            testovi_krvi   13668088

and I tried to make a donut using PieDonut, but it only shows percentages, and I need to show values instead. This is the code I used and the donut I got:

PieDonut(T_Total_EM_vs_CM, aes(Tip_migrene, lvl2, count=Troskovi), 
         title = "EM vs. HM (Svi)")

Is it possible to show values instead of percentages in this PieDonut? (Do I possibly need to format values through some of the parameters of the PieDonut?)

Also, is it possible to put the total amount (sum) of all the values in the center of the donut?

2

There are 2 best solutions below

3
M-- On

This is the best we can achieve with these large labels. Basically, we create the labels beforehand, and would turn off the percentages. I am not able to put the total at the middle of the graph, so I put it in the title. If you want more flexibility, you need to experiment with ggplot2.

read.table(text = " Tip_migrene Populacija  lvl2                    Troskovi
                    EM          Svi         PZZ                     574345839
                    EM          Svi         SZZ                     1151543136
                    EM          Svi         bolnicko_lecenje        69967985
                    EM          Svi         dijagnosticke_procedure 1983183163
                    EM          Svi         testovi_krvi            57333171
                    HM          Svi         PZZ                     107825242
                    HM          Svi         SZZ                     174517582
                    HM          Svi         bolnicko_lecenje        9822562
                    HM          Svi         dijagnosticke_procedure 286431165
                    HM          Svi         testovi_krvi            13668088", 
           header = T, stringsAsFactors = F) -> T_Total_EM_vs_CM

library(dplyr)
library(ggplot2)
library(webr)

T_Total_EM_vs_CM %>% 
  mutate(lvl2_lbl = paste0(lvl2, "\n", Troskovi)) %>% 
  mutate(Tip_migrene_lbl = paste0(Tip_migrene, "\n", sum(Troskovi)), 
         .by = Tip_migrene) %>% 
 PieDonut(., aes(Tip_migrene_lbl, lvl2_lbl, count=Troskovi), 
         title = paste0("EM vs. HM (Svi)", "\n", 
                        "Total = ", sum(T_Total_EM_vs_CM$Troskovi)), 
         showRatioDonut = F, showRatioPie = F, showPieName = F,
         ## if you want more labels, but it will get messy
         # showRatioThreshold = 0.001, 
         r0 = 0.05, r1 = 0.5, r2 = 0.9, 
         titlesize = 4, start = 3.5)

Created on 2024-03-26 with reprex v2.0.2

6
Felix Jassler On

Looking at the source code for webr, it seemed straight-forward to add the option of displaying absolute values instead of percentages to the package. I forked the project to apply these changes; feel free to install this package and call the PieChart() function like so:

PieDonut(T_Total_EM_vs_CM, aes(Tip_migrene, lvl2, count=Troskovi), 
         title = "EM vs. HM (Svi)",
         showRatioPie = "absolute", showRatioDonut = "absolute"
)

# with a little more flexibility, see documentation for more information
PieDonut(T_Total_EM_vs_CM, aes(Tip_migrene, lvl2, count=Troskovi), 
         title = "EM vs. HM (Svi)",
         showRatioPie = "absolute", showRatioDonut = function(a,r) scales::scientific(a)
)

In the meantime, I'll open a pull request and see if it gets through.