Calculating percentage and making ggplot bar chart in R

Anthony Schmidt

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.

maydin

You can use prop.table for getting the percantages as,

data_perc <- as.data.frame(prop.table(table(data), 1))
data_perc <- data_perc[data_perc$status=="below",]


ggplot(data= data_perc, aes(x=state_name,y= Freq ,fill=state_name)) +
  geom_bar(stat="identity") + 
  coord_flip() +
  ggtitle("My Bar Chart")

gives,

enter image description here

Data:

data <- read.table(text="state_name status
State1 above
State1 above
State1 below
State1 below
State1 below
State1 above
State1 below
State1 below
State1 below
State1 above
State2 above
State2 NA
State2 NA
State2 NA
State2 NA
State3 below
State3 above
State3 above
State3 above
State3 below
State3 above
State3 below
State3 below
State3 above",header=T)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R: Pie chart with percentage as labels using ggplot2

Creating a Stacked Percentage Bar Chart in R with ggplot

R: ggplot stacked bar chart with counts on y axis but percentage as label

Adding percentage labels to a bar chart in ggplot2

Add percentage labels to stacked bar chart ggplot2

R - Clustered bar chart with varying widths in ggplot

R ggplot labels on stacked bar chart

Special Stacked Bar Chart R ggplot

R Stacked percentage bar plot with percentage of two factor variables with ggplot

Change histogram bar percentage label in R ggplot

Binary column sums into percentage stacked bar chart in ggplot

Most efficient way of getting bar chart with percentage labels with ggplot

R ggplot horizontal bar chart with thousand data

Aesthetics error in a bar chart (ggplot, R)

Making error bars on one group in bar chart in ggplot2

multivariate bar chart in R ggplot

Making a bar graph with ggplot and Shiny in R

Add legend to bar + line ggplot chart in R

Calculating progress bar percentage

Calculating the percentage for a SVG pie chart

R ggplot Sort Percent Stacked Bar Chart

plotting a percentage stacked bar chart in R

how to change the stacked bar chart using ggplot2 (percentage, sort) in R

Create percentage bar chart in Stata for categorical variables from R code

Create Stacked "Progress" Bar Chart in R with ggplot

How to create a stacked bar chart in r with ggplot

Creating a Stacked Percentage Bar Chart in R with ggplot with labels

Making a stacked bar chart with ggplot

How to plot Bar_Chart of accuracy scores as percentage value for multiple variables and subgroups using ggplot2 R?