I have a factor variable (chest) with 4 levels:
1 2 3 4
23 49 84 143
I want to create a barplot. In the normal way I have this and it works:
barplot(summary(chest))
I wanted to do a more elaborate barplot using 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()
I got 'Error: data
must be a data frame, or other object coercible by fortify()
, not a factor.'
How can I resolve this?
CodePudding user response:
It seems we can reproduce your data like this:
chest <- factor(rep(1:4, times = c(23, 49, 84, 143)))
So that we have
summary(chest)
#> 1 2 3 4
#> 23 49 84 143
and can get a base R bar plot with your code as follows:
barplot(summary(chest))
To get a ggplot we can do:
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()
Created on 2022-11-06 with reprex v2.0.2