[英]How to add the actual size of the frequency of barplots in r with ggplot2?
我想在每個條形上方添加實際數字。 我試過這個,但沒有用。 我該如何解決? MP1 是 SPSS 文件,TBI 和 mp_1 是變異因子。
ggplot(MP1) +
geom_bar(aes(x=TBI), fill=mp_1))+
geom_text(aes(label=count), vjust=1.5, colour="white", size=3.5)
從我所看到的,您有一些拼寫錯誤/語法問題,例如在geom_bar()
行末尾有一個額外的“)”,並且您的geom_text()
沒有stat = "count"
。 這是一個使用 mtcars 數據集的可重現示例,它說明了一個潛在的解決方案:
library(ggplot2)
ggplot(mtcars) +
geom_bar(aes(x = cyl)) +
geom_text(stat = "count", aes(x = cyl, label = ..count..),
vjust = 1.5, colour = "white", size = 3.5)
由reprex 包( v2.0.0 ) 於 2021 年 7 月 28 日創建
我無法使用您的數據測試解決方案,因為您尚未提供任何數據(請參閱如何制作出色的 R 可重現示例),但是我對您的情況的猜測是:
ggplot(MP1) +
geom_bar(aes(x=TBI), fill="dodgerblue"))+
geom_text(stat = "count", aes(x=TBI, label=..count..), vjust=1.5, colour="white", size=3.5)
為了創建堆疊條形圖:
library(ggplot2)
ggplot(mtcars, aes(fill = factor(gear), x = factor(carb))) +
geom_bar(position = "stack") +
geom_text(stat = "count", aes(label = ..count..),
position = position_stack(vjust = 0.5))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.