2017-08-02 124 views
0

我正在复制一些所有脚本(一年前编码),并发现我不再获得相同的绘图。我正在使用相同的数据集和相同的代码;唯一的区别是我的R安装版本和ggplot2 ---所以我认为这是这里的问题。(ggplot2更新?)带有百分比标签的堆叠式barplot

让我告诉你几个愚蠢的情节的问题。当有个标签生产堆叠barplots我会做这样的事情:

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")) 

结果会是这样的:

Stacked barplot#1

截至今天,如果我尝试完全相同的代码完全相同的数据集,我得到了以下情节:

Stacked barplot#2

正如你可以看到第l阿贝尔没有正确定位在酒吧上,颜色倒转,使得情节的阅读变得笨拙(就像堆积的情节已经不够尴尬)。

对不起,没有给你可重复的代码,但我相信我的问题只是我没有更新我的代码,因为ggplot2开发(或可能是plyr的问题?)如果你可以发现我的代码中的“旧”制作第二个不可思议的情节,我将非常感激并乐意从那里进行调查。

谢谢!

编辑:由于在评论中的建议,图中的百分比是不同的,因为我使用不同的国家(但相同的代码和相同的数据集)。我公司生产的不同版本R和GGPLOT2的准确,完全一样的情节,你可以看到,问题仍然存在:Stacked barplot#3

+0

其数据....是/否列需要被格式化为'factor'用'水平= C( “是”,“否)'则应该工作 – cephalopod

+0

感谢您的回复,第二张图是将变量“contplt2”转换为这两个层次的因子后生成的,我可能不完全理解您的建议:我会再次重新分析变量,但正如我上面提到的它已经有了这两个级别加上第三个标记为“NA”(我在代码中省略) – YSC

+0

请确保该因子的级别是正确的顺序 – Gregor

回答

0

尝试切换两次标签的contplt2,前后产生ess2后。
希望它能帮助你。

# 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

+0

非常感谢您的回答。你的解决方案完全奏效,但我仍然有一个问题:“为什么?”什么改变了这一点,迫使我重新考虑我的变量水平?这是一个_gpplot2_的东西吗?或者在计算百分比时_plyr_的工作方式是什么?或者我可能错过了其他的东西?再次感谢。 – YSC

+0

#YSC好问题YSC。我认为,如果你想找到答案,你应该比较新旧R系统上的'plyr'和'ggplot2'的结果... –