pie chart with percentage

Hi,

I am really struggling with a pie chart. I would like to make a pie chart for organisms in my data. I want percentages of used organisms in the pie chart but it is giving me all the time some errors.
I want to show with the pie chart what organisms in my data were the most often used.
In the attachment, I included my data. Thank you very much for help Venlafaxine.pdf (49.4 KB)

What have you tried so far? what is your specific problem?, we are more inclined towards helping you with specific coding problems rather than doing your work for you.

Could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

I am sorry for the lack of information. I am trying to make a pie chart with representation for every organism.
I used the following formula but it did not show a percentage and one representation of organism is there two times (Daphnia manga). I also made a new table just for organisms in alphabetical order.organismvenlafaxine.pdf (39.4 KB)

slices<-table(organismvenlafaxine)
pct<-round(slices/sum(slices)*100)
pie((slices),cex=0.4)

Please read the guide I gave you, a PDF file is not a copy/paste friendly way of sharing sample data.

Is it even possible to make a pie chart with data like these?

organism
adult male fathead minnows (Pimephales promelas)
adult male fathead minnows (Pimephales promelas)
American starsnail Lithopoma americanum
American starsnail Lithopoma americanum
American starsnail Lithopoma americanum
Chlorostoma funebralis
Chlorostoma funebralis
Chlorostoma funebralis
cuttlefish (Sepia officinalis)
cuttlefish (Sepia officinalis)
Daphnia magna
Daphnia magna
Daphnia magna
Daphnia magna
Daphnia magna
embryonic and larval fathead minnow (Pimephales promelas)
embryonic and larval fathead minnow (Pimephales promelas)
embryonic and larval fathead minnow (Pimephales promelas)
fathead minnow (Pimephales promelas)
fathead minnow (Pimephales promelas)
fathead minnow (Pimephales promelas)
Hybrid striped bass (Morone saxatilis x Morone chrysops)
Hybrid striped bass (Morone saxatilis x Morone chrysops)
Hybrid striped bass (Morone saxatilis x Morone chrysops)
juvenile meagre (Argyrosomus regius)
Lithopoma americanum
Lithopoma americanum
Lithopoma americanum
Nucella ostrina
Nucella ostrina
Nucella ostrina
Nucella ostrina
oyster drill Urosalpinx cinerea
oyster drill Urosalpinx cinerea
oyster drill Urosalpinx cinerea
Rainbow trout (Oncorhynchus mykiss)
Rainbow trout (Oncorhynchus mykiss)
Rainbow trout (Oncorhynchus mykiss)
Rainbow trout (Oncorhynchus mykiss)
Tegula fasciatus
Tegula fasciatus
Tegula fasciatus
Urosalpinx cinerea
Urosalpinx cinerea
Urosalpinx cinerea
Xenopus tropicalis
Xenopus tropicalis
Xenopus tropicalis
Xenopus tropicalis
Xenopus tropicalis
zebrafish (Danio rerio)
zebrafish (Danio rerio)
zebrafish (Danio rerio)
zebrafish (Danio rerio)
zebrafish (Danio rerio)
zebrafish (Danio rerio)
zebrafish (Danio rerio)
zebrafish larvae (Danio rerio)
zebrafish larvae (Danio rerio)
zebrafish larvae (Danio rerio)

Yes, it is.
Since you are not providing a reproducible example this is the best I can do for you, I hope it helps.

library(tidyverse)

# Sample data on a copy/paste friendly format (replace this with your own data)
sample_df <- data.frame(
  stringsAsFactors = FALSE,
          organism = c("adult male fathead minnows (Pimephales promelas)",
                       "adult male fathead minnows (Pimephales promelas)","American starsnail Lithopoma americanum",
                       "American starsnail Lithopoma americanum",
                       "American starsnail Lithopoma americanum","Chlorostoma funebralis",
                       "Chlorostoma funebralis","Chlorostoma funebralis",
                       "cuttlefish (Sepia officinalis)",
                       "cuttlefish (Sepia officinalis)","Daphnia magna","Daphnia magna","Daphnia magna",
                       "Daphnia magna","Daphnia magna",
                       "embryonic and larval fathead minnow (Pimephales promelas)",
                       "embryonic and larval fathead minnow (Pimephales promelas)",
                       "embryonic and larval fathead minnow (Pimephales promelas)",
                       "fathead minnow (Pimephales promelas)",
                       "fathead minnow (Pimephales promelas)","fathead minnow (Pimephales promelas)",
                       "Hybrid striped bass (Morone saxatilis x Morone chrysops)",
                       "Hybrid striped bass (Morone saxatilis x Morone chrysops)",
                       "Hybrid striped bass (Morone saxatilis x Morone chrysops)","juvenile meagre (Argyrosomus regius)",
                       "Lithopoma americanum","Lithopoma americanum","Lithopoma americanum",
                       "Nucella ostrina","Nucella ostrina","Nucella ostrina",
                       "Nucella ostrina","oyster drill Urosalpinx cinerea",
                       "oyster drill Urosalpinx cinerea",
                       "oyster drill Urosalpinx cinerea","Rainbow trout (Oncorhynchus mykiss)",
                       "Rainbow trout (Oncorhynchus mykiss)",
                       "Rainbow trout (Oncorhynchus mykiss)","Rainbow trout (Oncorhynchus mykiss)",
                       "Tegula fasciatus","Tegula fasciatus","Tegula fasciatus",
                       "Urosalpinx cinerea","Urosalpinx cinerea",
                       "Urosalpinx cinerea","Xenopus tropicalis","Xenopus tropicalis",
                       "Xenopus tropicalis","Xenopus tropicalis",
                       "Xenopus tropicalis","zebrafish (Danio rerio)","zebrafish (Danio rerio)",
                       "zebrafish (Danio rerio)","zebrafish (Danio rerio)",
                       "zebrafish (Danio rerio)","zebrafish (Danio rerio)",
                       "zebrafish (Danio rerio)","zebrafish larvae (Danio rerio)",
                       "zebrafish larvae (Danio rerio)",
                       "zebrafish larvae (Danio rerio)")
)

sample_df %>% 
    count(organism, name = "percent") %>% 
    mutate(percent = percent/sum(percent)) %>% 
    ggplot(aes(x="", y = percent, fill = factor(organism))) + 
    geom_col(position = 'stack', width = 1) +
    geom_text(aes(label = paste(round(percent * 100, 1), "%"), x = 1.3),
              position = position_stack(vjust = 0.5)) +
    theme_classic() +
    theme(plot.title = element_text(hjust=0.5),
          axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank()) +
    labs(fill = "Organism",
         x = NULL,
         y = NULL,
         title = "Pie Chart") + 
    coord_polar("y")

Created on 2020-08-06 by the reprex package (v0.3.0)

3 Likes

I guess another question is whether you should be making a pie chart with that sort of data. Surely there are better chart types to use.

Thank you very much. It helped a lot.

A pie chart looks like a terrible choice here. Even a flipped barchart has to be better.

dat1 <- sample_df %>%
count(organism, name = "percent") %>%
mutate(percent = percent/sum(percent))

ggplot(dat1, aes(organism , percent, fill = organism)) + geom_bar(stat = "identity") + coord_flip() + theme(legend.position="none")

1 Like

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