I am just learning R
and trying to reproduce something that I can easily create in Excel
via a PivotTable
. I have the data below that lists state names and their status. I want to make a horizontal bar chart that shows the state name on the Y axis and the percentage below on the X axis.
state_name status
State 1 above
State 1 above
State 1 below
State 1 below
State 1 below
State 1 above
State 1 below
State 1 below
State 1 below
State 1 above
State 2 above
State 2 NA
State 2 NA
State 2 NA
State 2 NA
State 3 below
State 3 above
State 3 above
State 3 above
State 3 below
State 3 above
State 3 below
State 3 below
State 3 above
I can load the data but am not sure how to write the code to subset and create percentages.
Here is my poor attempt,
ggplot(data = subset(data, !is.na(status)), aes(y=state_name, x=count(status[below])/count(status))) +
geom_bar(stat="identity")
Any help would be greatly appreciated. I learn best through examples.