我有一個具有 4 個級別的因子變數(胸部):
1 2 3 4
23 49 84 143
我想創建一個條形圖。以正常方式,我有這個并且它有效:
barplot(summary(chest))
我想用 ggplot 做一個更精細的條形圖
ggplot(data=chest, aes(x="Chest_pain"))
geom_bar( fill="steelblue")
geom_text(aes(label=len), vjust=-0.3, size=3.5)
theme_minimal()
我得到“錯誤:data必須是一個資料框,或者其他可以被 強制轉換的物件fortify(),而不是一個因素。”
我該如何解決這個問題?
uj5u.com熱心網友回復:
看來我們可以像這樣重現您的資料:
chest <- factor(rep(1:4, times = c(23, 49, 84, 143)))
所以我們有
summary(chest)
#> 1 2 3 4
#> 23 49 84 143
并且可以使用您的代碼獲得基本 R 條形圖,如下所示:
barplot(summary(chest))

要獲得 ggplot 我們可以這樣做:
library(ggplot2)
ggplot(mapping = aes(x = chest))
geom_bar(fill = "steelblue")
geom_text(stat = "count", aes(label = stat(count)), vjust = -0.3, size = 3.5)
theme_minimal()

使用reprex v2.0.2創建于 2022-11-06
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529686.html
標籤:rggplot2条形图
