[英]percentage plot using ggplot2
我想 plot 類似於下圖,但我有一個錯誤“stat_count() 只能有 x 或 y 美學。”。 所以我把它改成stat="identity"。 但是結果和我在網上找到的例子不一樣。
網上找的例子:
示例來自: https://sebastiansauer.github.io/percentage_plot_ggplot2_V2/
我的代碼
geom_bar(aes(y=pct_change, fill=factor(Year)), stat="count")+
geom_text(aes( label = scales::percent(pct_change),
y= pct_change ), stat= "count", vjust = -.5) +
ylab("Avg Unit Price") +
facet_grid(~Type.of.Sale) +
scale_y_continuous(labels = scales::percent)
這是mtcars
數據集的示例,您可以如何完成任務:
library(tidyverse)
ggplot(mtcars, aes(x = cyl, group=am))+
geom_bar(aes(y=..prop.., fill=factor(..x..)), stat="count")+
geom_text(aes( label = scales::percent(..prop..),
y= ..prop..), stat= "count", vjust = -.5) +
ylab("Avg Unit Price") +
facet_grid(~am) +
scale_y_continuous(labels = scales::percent)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.