0
votes

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

1
its your data....the Yes/No column needs to be formatted as a factor with levels = c("Yes", "No) then is should work - cephalopod
Thanks for your response. The second plot was produced having converted the variable "contplt2" to a factor with those two levels. I might not be understanding fully your suggestion: I'll re-factor the variable again, but as I mentioned above it already had those two levels plus a third one labelled to "NA" (which I omit in the code) - YSC
Make sure the levels of the factor are the right order. - Gregor Thomas
@YSC You have also different percentages in the plot! Why ? Is your dataset changed ? - Marco Sandri
@MarcoSandri sorry, did not explain that in OP (I'll edit it). Percentages are different because the second plot I produced today and I uploaded uses a different country, but the code I'm reproducing from my old script is exactly the same (the dataset is the same too). - YSC

1 Answers

0
votes

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