我可以根據計數而不是按比例來訂購我的情節。我想將 x 軸上的條按“c”的比例排列。這是我的代碼
long<- data.frame(
Name = c("abc","abc","abc","gif","gif","gif","xyz","xyz","xyz"),
variable = c("a","b","c","a","b","c","c","b","a"),
value = c(4,6,NA,2,8,1,6,NA,NA))
long_totals <- long %>%
group_by(Name) %>%
summarise(Total = sum(value, na.rm = T))
p<-long %>%
mutate(variable = fct_relevel(variable, c("c", "b", "a"))) %>%
arrange(variable) %>%
mutate(Name = fct_inorder(Name))
p %>%
ggplot()
aes(x = Name,
y = value,
fill = variable)
geom_bar(position = "fill",
stat = "summary")
geom_text(data = long_totals,
aes(y = 100,
x = Name,
label = Total),
size = 7,
position = position_fill(vjust = 1.02))
scale_y_continuous(labels = scales::percent_format())
另外,我正在使用 geom_text 繪制總數
uj5u.com熱心網友回復:
像這樣按組添加比例 c ,生成時p:
... %>%
group_by(Name) %>%
mutate(prop_c = sum(value[variable=="c"], na.rm=T)/sum(value, na.rm=T))
然后繪圖,使用reorder:
ggplot()
geom_col(data= p,aes(x = reorder(Name,prop_c, decreasing=T),y = value,fill = variable),position = "fill")
geom_text(data = long_totals, aes(y = 100,x = Name,label = Total),size = 7,position = position_fill(vjust = 1.02))
scale_y_continuous(labels = scales::percent_format())

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477211.html
