2017-08-02 130 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'的結果... –