簡體   English   中英

geom_bar + position_dodge問題

[英]Issue with geom_bar + position_dodge

我想用條形圖繪制誤差線和文本。 為了繪制出美觀且易讀的圖,我希望在geom_text元素的左邊(對於左邊的欄)和右邊(對於右邊的欄)顯示geom_text 我會使用position_dodge來做到這一點,但是如果不對這些小節進行分組的話,這不會產生任何影響。

這是我的代碼:

df1 <- data.frame(e=c(0.02,0.02), na=c("A","B"), value=c(0.25, 0.75))

ggplot(df1, aes(x=factor(na), y=value, fill=factor(na)))+
   geom_bar(stat="identity", width=0.2)+
   geom_errorbar(aes(ymin = value-e, ymax = value+e), width = 0.01, size=0.5, 
        alpha=.6)+
   geom_text(aes(label=paste0(round(value*100,0),"%"), vjust=-0.5), 
        size=5)  

這將產生相同的情節:

ggplot(df1, aes(x=factor(na), y=value, fill=factor(na)))+
   geom_bar(stat="identity", width=0.2)+
   geom_errorbar(aes(ymin = value-e, ymax = value+e), width = 0.01, size=0.5, 
        alpha=.6, position=position_dodge(width=0.2))+
   geom_text(aes(label=paste0(round(value*100,0),"%"), vjust=-0.5), 
        size=5)

在此處輸入圖片說明

如何左右移動錯誤欄?

謝謝

您可以使用x美感來指定誤差線的x位置。 盡管通常的處理習慣是使誤差線居中,並移動文本(最好垂直放置)

ggplot(df1, aes(x=factor(na), y=value, fill=factor(na)))+
  geom_bar(stat="identity", width=0.2)+
  geom_errorbar(aes(x = as.integer(factor(na)) +c(-0.1,0.1), ymin = value-e, ymax = value+e), width = 0.01, size=0.5, 
                alpha=.6)+
  geom_text(aes(label=paste0(round(value*100,0),"%"), vjust=-0.5), 
            size=5)

暫無
暫無

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

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