Pie Chart results don't match Table

Hi All, I'm pretty new to r and don't know a lot about statistics so please bear with me. I made some pie charts and a table for the same data but when I compare them I don't get the same results (the percentages are significantly different). The pie chart makes it look as though there is way more shatter than the table shows for each component. Any insight you can offer would be greatly appreciated!

Completeness Upper (1) Lower (2)
Shatter / Debris (1) 32.11% (n=1377) 27.48% (n=122)
Medial – Distal Fragment (2) 37.06% (n=1589) 32.66% (n=145)
Split (3) 0.19% (n=8) 0.23% (n=1)
Proximal Fragment (4) 6.16% (n=264) 3.66% (n=16)
Complete Flake (5) 24.49% (n=1050) 36.04% (n=160)
Sums 4288 444

I get the counts (n=) using the following code and then I divided each completeness state by the total number of specimens in the component (sums at bottom of table) and multiplied those numbers by 100 to get the percent.

library(MASS)

table1 = table(dat$deb_complet_match_BM, dat$Component)

table1

I used the following code to make the pie charts and the percentages for each slice doesn’t match what I get when I calculate the percent in the table.

all_deb_upper = subset(dat, dat$Component==1)

deb_upper = dat$deb_complet_match_BM

comp1 = (length(which(all_deb_upper==1)))

comp2 = (length(which(all_deb_upper==2)))

comp3 = (length(which(all_deb_upper==3)))

comp4 = (length(which(all_deb_upper==4)))

comp5 = (length(which(all_deb_upper==5)))

slice = c(comp1, comp2, comp3, comp4, comp5)

lbls = c("Shatter", "Medial-Distal Fragment", "Split", "Proxmial Fragment", "Complete")

pct <- round(slice/sum(slice)*100,2)

lbls <- paste(lbls, pct) # add percents to labels

lbls <- paste(lbls,"%",sep="") # ad % to labels

pie(slice, labels = lbls, main="LbDt-1 Debitage Completeness for Upper Componenent", col=rainbow(length(lbl)))

par(mfrow=c(2,1))# no of rows, second number = number of columns

all_deb_lower = subset(dat, dat$Component==2)

deb_lower = dat$deb_complet_match_BM

comp1 = (length(which(all_deb_lower==1)))

comp2 = (length(which(all_deb_lower==2)))

comp3 = (length(which(all_deb_lower==3)))

comp4 = (length(which(all_deb_lower==4)))

comp5 = (length(which(all_deb_lower==5)))

slice = c(comp1, comp2, comp3, comp4, comp5)

lbls = c("Shatter", "Medial-Distal Fragment", "Split", "Proxmial Fragment", "Complete")

pct <- round(slice/sum(slice)*100,2)

lbls <- paste(lbls, pct) # add percents to labels

lbls <- paste(lbls,"%",sep="") # ad % to labels

pie(slice, labels = lbls, main="LbDt-1 Debitage Completeness for Lower Componenent", col=rainbow(length(lbl)))

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like

Thanks, does this help?

Not really, we can't copy the sample data from a screenshot, please post the actual code.

Whoops! that makes sense.

completeness <-tibble::tribble(
                 ~Component, ~deb_complet_match_BM,
                          2,                     5,
                          2,                     1,
                          2,                     1,
                          2,                     2,
                          2,                     4,
                          2,                     1,
                          2,                     1,
                          2,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     2,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1,
                          1,                     1
                 )

I'm not sure I understand what your problem is, but this is how I would calculate the percentages and make the piecharts

library(tidyverse)
library(forcats)

completeness <- data.frame(
              Component = c(2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                            1, 1, 1, 1, 1),
   deb_complet_match_BM = c(5, 1, 1, 2, 4, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1,
                            1, 1, 1, 1, 1)
)

plot_data <- completeness %>% 
    mutate(deb_complet_match_BM = fct_recode(as_factor(deb_complet_match_BM),
                                  "Shatter" = "1",
                                  "Medial-Distal Fragment" = "2",
                                  "Split" = "3",
                                  "Proxmial Fragment" = "4",
                                  "Complete" = "5"),
           Component = fct_recode(as_factor(Component),
                                  "Upper Componenent" = "1",
                                  "Lower Componenent" = "2")) %>% 
    group_by(Component) %>%
    count(deb_complet_match_BM, name = "percent") %>% 
    mutate(percent = (percent / sum(percent)) * 100) 
plot_data
#> # A tibble: 6 x 3
#> # Groups:   Component [2]
#>   Component         deb_complet_match_BM   percent
#>   <fct>             <fct>                    <dbl>
#> 1 Upper Componenent Shatter                  92.9 
#> 2 Upper Componenent Medial-Distal Fragment    7.14
#> 3 Lower Componenent Shatter                  62.5 
#> 4 Lower Componenent Medial-Distal Fragment   12.5 
#> 5 Lower Componenent Proxmial Fragment        12.5 
#> 6 Lower Componenent Complete                 12.5

plot_data %>% 
    ggplot(aes(x = "", y = percent, fill = deb_complet_match_BM)) +
    geom_col() +
    facet_wrap(vars(Component)) +
    coord_polar(theta = "y", start=0) +
    theme_void() +
    theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank())

2 Likes

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