簡體   English   中英

(ggplot2更新?)帶有百分比標簽的堆疊條形圖

[英](ggplot2 update?) Stacked barplot with percentage labels

我正在重現所有腳本(一年多以前的編碼),發現我不再得到相同的劇情。 我正在使用相同的數據集和相同的代碼; 唯一的區別是我的R安裝和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)
})

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

結果將是這樣的:

堆積的barplot#1

與今天一樣,如果我嘗試使用完全相同的數據集使用完全相同的代碼,則會得到以下圖表:

堆積的barplot#2

如您所見,標簽在條形圖上的位置不正確,並且顏色反轉,使繪圖的讀取變得笨拙(好像堆疊的條形圖還不夠笨拙)。

抱歉,沒有提供可復制的代碼,但我相信我的問題是我沒有像ggplot2開發的那樣更新我的代碼(或者是plyr的問題?)如果您發現我的代碼中有些“舊”的內容可能會產生第二個,我將不勝感激,很高興自己在那里進行調查。

謝謝!!!

編輯:由於評論中的建議,圖中的百分比不同,因為我使用的是不同的國家(但使用相同的代碼和相同的數據集)。 我用不同版本的R和ggplot2制作了完全相同的圖,您可以看到問題仍然存在: 堆積的barplot#3

嘗試在生成ess2之前和之后兩次切換contplt2的標簽。
希望它能對您有所幫助。

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM