我有這個資料框,我應用了 ggplot :
df = data.frame(x =rep(1:5,3),
z = rep(1:3,each = 5),
y = 100:114 )
df
ggplot(df) aes(x=x,fill=x,y=y)
geom_col(position = 'dodge')
facet_wrap(~z)
geom_text(aes(label = y),
position = position_dodge(1),
vjust=-1,hjust=0,color = 'white' )
theme_dark()
scale_fill_gradient(low = 'orange',high = 'red')
但是在我的圖中,我想更改頻率并使它們與每個 z 變數類別相對應的百分比。
感謝幫助
uj5u.com熱心網友回復:
在這種情況下,最好預先計算百分比,然后直接繪制:
library(tidyverse)
df %>%
group_by(z) %>%
mutate(
y_pct = y / sum(y)
) %>%
ggplot(.) aes(x=x,fill=x,y=y)
geom_col(position = 'dodge')
facet_wrap(~z)
geom_text(aes(label = sprintf('%0.1f%%', y_pct * 100)),
position = position_dodge(1),
vjust=-1,hjust=0,color = 'white' )
theme_dark()
scale_fill_gradient(low = 'orange',high = 'red')

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/520926.html
上一篇:多年來使用回圈為選擇整齊的人口普查變數制作代碼簿,呼叫回圈資料集時遇到麻煩
下一篇:在基因表達資料中找到最接近的匹配
