簡體   English   中英

我如何擺脫這個 bar_plot 中的百分比?

[英]How do I get rid of the percentages in this bar_plot?

我想在 ggplot2 中繪制一個閃避條形圖,顯示一個人是否有儲蓄,所有這些都由另一個變量區域顯示。 由於缺少數據,我想將它們作為另一個類別包含在 plot 中,這樣每個區域都會有三個條:一個表示有,一個表示沒有,一個表示缺少數據。 現在我制作了一個 plot,它准確地顯示了我想要的內容,我在 inte.net 上獲取了一些代碼,因為我確實無法正確執行。 唯一的問題是現在它在條形圖的頂部顯示百分比,這是我不想要的功能。 我該如何擺脫它? 或者至少我怎樣才能把它四舍五入? 在此處輸入圖像描述


ggplot(adatok, aes(x=as.factor(region), fill=as.factor(savings)))+
  geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) +
  geom_text(aes(label=scales::percent(..count../tapply(..count.., ..x.. ,sum)[..x..]),
    y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
            stat="count", position=position_dodge(0.9), vjust=-0.5)+
  scale_y_continuous(labels = scales::percent)

我將使用mtcars (及其cylgear )來演示兩個選項。

初始點:

library(ggplot)
data("mtcars")
ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear)))+
  geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) +
  geom_text(aes(label=scales::percent(..count../tapply(..count.., ..x.. ,sum)[..x..]),
    y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
            stat="count", position=position_dodge(0.9), vjust=-0.5)+
  scale_y_continuous(labels = scales::percent)

原情節

  1. 如果您不想要文字百分號,請刪除scales::percent (並查看0.72727272727272727類的數字)或round(100*., 1)它:

     ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear)))+ geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) + geom_text(aes(label=round(100*(..count../tapply(..count.., ..x.. ,sum)[..x..]), 1), y=..count../tapply(..count.., ..x.. ,sum)[..x..] ), stat="count", position=position_dodge(0.9), vjust=-0.5)+ scale_y_continuous(labels = scales::percent)

    沒有百分號

  2. 如果您根本不需要這些數字,請完全刪除geom_text(.)

     ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear)))+ geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) + scale_y_continuous(labels = scales::percent)

    根本沒有數字

暫無
暫無

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

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