0

I am trying to include the count labels on stacked bar plots which represent percentages. I want to show x-amount of individuals make up the graphed percentages. However, when I include the count labels my y-axis gets blown out of proportion because it changes to match the count data, not the percentages. Also, the bars are removed from the graph too? I have reviewed other posts similar to this, such as: "How to add percentage or count labels above percentage bar plot?". I cannot find the error in my r command.

My command used is as follows:

sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+
  geom_bar(position="fill",stat="identity")+
  geom_text(aes(label=nDet),position=position_stack(vjust=0.5))+
  theme(axis.text.x=element_text(angle=90,hjust=1))+
  scale_y_continous(labels=scales::percent_format())

Example of data being graphed:

speciesSci         recvDeployName    nDet
1 Arenaria interpres Bucktoe Preserve    96
2 Arenaria interpres CHDE               132
3 Arenaria interpres Fortescue        22133
4 Arenaria interpres Mispillion        2031
5 Arenaria interpres Norbury           3709
6 Arenaria interpres Penn - DRL          49

What my graph looks like when I use the command example provided above:

Original Graph

11/17/19 Update: The r commands below seems promising:

sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+
  geom_text(aes(label=nDet),position="fill", stat="identity")+
  theme(axis.text.x=element_text(angle=90,hjust=1))+
  scale_y_continuous(labels=scales::percent_format())

Graph as of 11/17/2019

I just need to get the colored bars back onto the graph, representing the percentages.

Any help would be greatly appreciated. Thank you.

OTStats
  • 1,820
  • 1
  • 13
  • 22
Josh B
  • 39
  • 5
  • 1
    Please don't provide data as a picture, we can't easily use that to try out solutions. You are using `position_stack` in `geom_text`, but `position_fill` in `geom_bar`. That will make sure they will not line up. – Axeman Nov 15 '19 at 18:43
  • @Axeman Thanks for the comment, For future reference, how should I attach data? I do not see a place for a csv to be attached? I will try eliminating the `position_stack` within `geom_text` and see what that does. – Josh B Nov 15 '19 at 19:12
  • See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for some useful tips and tricks to improve your question. (You probably want `position_fill` in `geom_text`.) – Axeman Nov 15 '19 at 19:13
  • @Axeman I tried the suggestion and it's not giving the result stated within the original post. I tried the following: `sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+ geom_bar(stat="identity")+ geom_text(aes(label=nDet),position="fill")+ theme(axis.text.x=element_text(angle=90,hjust=1))+ scale_y_continous(labels=scales::percent_format())` and I got the following error: `Error in scale_y_continous(labels = scales::percent_format()) : could not find function "scale_y_continous"` Any further help is appreciated. I also edited the data image. – Josh B Nov 15 '19 at 23:51
  • You misspelled continuous – Axeman Nov 16 '19 at 01:30
  • Thank you for catching the spelling error. However, the bars are not present in the graph. Text labels of the count data are present, but the percentages represented by the bars are not present? Code used: `sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=speciesSci))+ geom_text(aes(label=nDet),position="fill", stat="identity")+ theme(axis.text.x=element_text(angle=90,hjust=1))+ scale_y_continuous(labels=scales::percent_format())` – Josh B Nov 16 '19 at 04:28

1 Answers1

0

This gave me the result I wanted:

sumplot<-ggplot(tagSummary,aes(x=recvDeployName,y=nDet,fill=factor(speciesSci))+geom_bar(positon="fill", stat="identity")+geom_text(aes(label=nDet),position = position_fill(vjust=0.5))+theme(axis.text.x=element_text(angle=90,hjust=1))+scale_y_continuous(labels=scales::percent_format()))

Correct Graph

Josh B
  • 39
  • 5