YSC
YSC

Reputation: 75

(ggplot2 update?) Stacked barplot with percentage labels

I was reproducing some all scripts (coded over a year ago) and found out that I am no longer getting the same plots. I am using the same dataset and the same code; the only difference is the version of my R installation and ggplot2---so I am assuming that is the problem here.

Let me show you the problem with a couple of silly plots. When producing stacked barplots with percentage labels I would do something like:

ess2 <- ddply(ess, .(essround2), function(.){
res <- cumsum(prop.table(table(factor(.$contplt2))))
  res2 <- prop.table(table(factor(.$contplt2)))
  data.frame(lab=names(res), y=c(res), res2=res2, pos=cumsum(res2)-0.5*res2)
})

ggplot(ess[ess$contplt2!="NA",], aes(x=essround2))+
  geom_bar(aes(fill=contplt2), position="fill")+
  geom_text(data=ess2[ess2$lab!="NA",],
            aes(label=round(res2.Freq, 2), x=essround2, y=pos.Freq))+
  labs(x="ESS Round", y="Percent")+
  scale_x_discrete(breaks=c("R1", "R2", "R3", "R4", "R5", "R6"),
                   labels=c("2002", "2004", "2006", "2008", "2010", "2012"))+
  ggtitle("Contacted politicians")+
  scale_fill_manual(name="Contacted politician", values=c("#31a354", "#a1d99b"))

The result would be something like:

Stacked barplot#1

As today, if I try the exact same code with the exact same dataset, I get the following plot:

Stacked barplot#2

As you can see the labels are not positioned properly on the bars, and the colors get inverted making the reading of the plot awkward (as if stacked barplots were not awkward enough already).

Sorry for not giving you reproducible code, but I believe my problem is just me not updating my code as ggplot2 developed (or maybe is plyr the problem?) If you can spot something "old" in my code that might be producing the second, wonky plot I would be very grateful and happy to investigate from there myself.

Thanks!!!

EDIT: thanks to a suggestion in the comments, the percentages in the plots are different because I used different countries (but the same code and the same dataset). I produced the exact-exact same plot with a different version of R and ggplot2 and you can see that the problem persists: Stacked barplot#3

Upvotes: 1

Views: 6634

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24272

Try switching twice the labels of contplt2, before and after generating ess2.
Hope it can help you.

# Here I try to reproduce your dataset
ess <- data.frame(
essround2 = c(
c(rep(2002,76),rep(2002,100-76)),
c(rep(2004,78),rep(2004,100-78)),
c(rep(2006,81),rep(2006,100-81)),
c(rep(2008,79),rep(2008,100-79)),
c(rep(2010,79),rep(2010,100-79)),
c(rep(2012,82),rep(2012,100-82))
),
contplt2 = c(
c(rep("No",76),rep("Yes",100-76)),
c(rep("No",78),rep("Yes",100-78)),
c(rep("No",81),rep("Yes",100-81)),
c(rep("No",79),rep("Yes",100-79)),
c(rep("No",79),rep("Yes",100-79)),
c(rep("No",82),rep("Yes",100-82))
)
)

# First switch of contplt2 levels
ess$contplt2 <- factor(ess$contplt2, levels=levels(ess$contplt2)[c(2,1)])

library(plyr)
library(ggplot2)
ess2 <- ddply(ess, .(essround2), function(.){
res <- cumsum(prop.table(table(factor(.$contplt2))))
  res2 <- prop.table(table(factor(.$contplt2)))
  data.frame(lab=names(res), y=c(res), res2=res2, pos=cumsum(res2)-0.5*res2)
})

# Second switch of contplt2 levels
ess$contplt2 <- factor(ess$contplt2, levels=levels(ess$contplt2)[c(2,1)])


ggplot(ess[ess$contplt2!="NA",], aes(x=essround2))+
  geom_bar(aes(fill=contplt2), position="fill")+
  geom_text(data=ess2[ess2$lab!="NA",],
            aes(label=round(res2.Freq, 2), x=essround2, y=pos.Freq))+
  labs(x="ESS Round", y="Percent")+
  scale_x_discrete(breaks=c("R1", "R2", "R3", "R4", "R5", "R6"),
                   labels=c("2002", "2004", "2006", "2008", "2010", "2012"))+
  ggtitle("Contacted politicians")+
  scale_fill_manual(name="Contacted politician", values=c("#a1d99b", "#31a354"))

enter image description here

Upvotes: 1

Related Questions