Percent Stacked Barchart

#reproducable example
ThreeSpecies<-structure(list(Species = c("Pig", "Pig", "Pig", "Pig", "Pig",
"Human", "Human", "Human", "Human", "Human", "Mouse", "Mouse",
"Mouse", "Mouse", "Mouse"), Stage = structure(c(1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("2 cell",
"4 cell", "8 cell", "Morula", "Blastocyst"), class = "factor"),
Time of Appearance (hpi) and Standard Deviation = c("18.6 ± 2.15",
"38.8 ± 13.50", "78.6 ± 4.71", "101.1 ± 2.48", "133 ± 10.00",
"27.2 ± 3.6", "40 ± 5.4", "62.1 ± 9.8", "93.9 ± 9.8", "105.2 ± 6.3",
"7.2 ± 2.6", "30.7 ±3.7", "42.1 ±5.1", "54.6 ± 4.5", "68 ±4.7"
), Percentage of Time (%) = c(13.984962406, 29.1729323308,
59.098, 76.015037594, 100, 25.855513308, 38.23, 59.03, 89.2585551331,
100, 10.5882352941, 45.1470588235, 61.9117647059, 80.2941176471,
100)), row.names = c(NA, -15L), class = c("tbl_df", "tbl",
"data.frame"))

#Change order of factor levels as they are being used in numerical and then alphabetical order

ThreeSpecies$Stage <- factor(ThreeSpecies$Stage, levels = c("2 cell", "4 cell", "8 cell","Morula","Blastocyst"))

#load this

library(ggplot2)

plot graph
ggplot(ThreeSpecies,aes(Species, 'Percentage of Time (%)' , fill=Stage),stat="identity",position_stack(vjust=1))+ geom_bar() + theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + scale_y_continuous(labels = scales::percent_format(scale = 100))+ylab("Percentage of time to blastocyst(%)")+xlab("Species")+ guides(fill=guide_legend("Stage"))
Error: Discrete value supplied to continuous scale

The problem is the error message and f in the percent stacked bar chart or each species it needs to be scaled to 100% ( i have calculated the percentages of time spent at each stage in the three species myself.) but i when i orginally plotted this is was scaled to 30%

what solution can i do for the error message and the graph

I am not sure what graph you want to get but the following code does not throw an error. Notice that I put back ticks, `, not single quotes, around Percentage or Time (%) and I moved the stat and position arguments inside of geom_bar.

ggplot(ThreeSpecies, aes(Species, `Percentage of Time (%)` , fill=Stage)) +  
  geom_bar(stat="identity", position = position_stack(vjust = 1)) +  
  theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank()) + 
  scale_y_continuous(labels = scales::percent_format(scale = 100))+
  ylab("Percentage of time to blastocyst(%)")+
  xlab("Species")+ guides(fill=guide_legend("Stage"))

Percent stacked bar chart

The quantities on the y axis are already percentages and they sum to more than 100, so I don't understand what the stacking is intended to show.
The line

scale_y_continuous(labels = scales::percent_format(scale = 100))

displays your value of 100 in the Percentage of Time (%) column as 10000%. I would drop that line.

the stacking is intented to show the percentage of time that each species spends in the stages ( 2 , 4 , 8 cell, morula ) before reaching the last stage ( blastocyst) at 100%

I need the y axis to scale to 100%. in order to do that would i have to change the percentage of time into proportions instead

This topic was automatically closed 21 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.