0

The code below generates an appropriately stacked bar chart. However, I am trying to add labels to show the percentage in each fill location. With the code below, the percentage labels do not align with the fill boundaries and all "stack" near the 0 point.

This is the code that I used

pff <- read.table(header=TRUE, text='REGION Q99
Tunis NidaaTounes
Tunis Nepasvoter
Tunis Nahdha
Tunis Nepasvoter
Tunis Nahdha
Tunis Nahdha
Tunis NidaaTounes
Tunis Jabha
Tunis NidaaTounes
Tunis Autres
Tunis Nahdha
Tunis Nahdha
Tunis Autres
Tunis Jabha
Tunis Nepasvoter
Tunis Nepasvoter
Tunis CPR
Tunis Nahdha
Tunis Nepasvoter
Tunis Nepasvoter
Ariana Nahdha
Ariana Nepasvoter
Ariana NidaaTounes
Ariana CPR
Ariana NidaaTounes
Ariana NidaaTounes
Ariana NidaaTounes
Ariana CPR
Ariana Nahdha', stringsAsFactors=FALSE)

g <- ggplot(pff, aes(x = REGION, y =(..count..)/sum(..count..), fill=Q99))
g <- g + geom_bar(position="stack") + 
  labs(title="Vote par région") +
  labs(x=" ", y=" ")+
  labs(fill="Parti Politique")+
  coord_flip()+
  # scale_fill_manual(values=cbPalette)+
  scale_y_continuous(labels = percent)+
  theme_bw()+
  theme(panel.border = element_rect(colour = "white")) + 
  geom_text(aes( label = scales::percent((..count..)/sum(..count..)),
                 y=(..count..)/sum(..count..)), stat= "count", size=3)

enter image description here

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48
Asma Manai
  • 19
  • 1
  • 4
  • it's not the same question, I edit it – Asma Manai Sep 01 '16 at 12:11
  • 2
    It appears that the question is asking about the location of the text labels, rather than making a plot to show percentages. I actually don't think this is a duplicate. Quick version: add `position = "stack"` to the `geom_text` call. Right now, the positions are all starting from 0; you need them to build on top of each other. If the question gets unlocked, I will post as an answer. @zx8754 and @Procastinatus Maximus – Mark Peterson Sep 01 '16 at 12:59
  • Thank you very much @MarkPeterson it works but in the case when the position ="fill" i got this error Error: position_fill requires the following missing aesthetics: ymax – Asma Manai Sep 01 '16 at 14:11
  • Other than `position = "stack"` (shown nicely in [this question](http://stackoverflow.com/questions/18994631/center-labels-stacked-bar-counts-ggplot2)), it is fairly common to calculate the labels and y position outside of ggplot2 to make things look nicer. Lots of examples out there, including a basic one [here](http://stackoverflow.com/a/6645506/2461552) and [here](http://stackoverflow.com/questions/34903368/how-to-center-stacked-percent-barchart-labels-in-ggplot2) – aosmith Sep 01 '16 at 15:02

1 Answers1

1

I realize this is really old, but the solution you're looking for is to add position = position_stack(vjust = 0.5) to your geom_text() call. Like so:


g <- ggplot(pff, aes(x = REGION, y =(..count..)/sum(..count..), fill=Q99))
g <- g + geom_bar(position="stack") + 
  labs(title="Vote par région") +
  labs(x=" ", y=" ")+
  labs(fill="Parti Politique")+
  coord_flip()+
  # scale_fill_manual(values=cbPalette)+
  scale_y_continuous(labels = percent)+
  theme_bw()+
  theme(panel.border = element_rect(colour = "white")) + 
  geom_text(aes( label = scales::percent((..count..)/sum(..count..)),
                 y=(..count..)/sum(..count..)), stat= "count", size=3, position = position_stack(vjust = 0.5))
Connor Dibble
  • 517
  • 3
  • 13