Stacked barchart - R programming with ggplot2

+1 vote

I want to create a stacked bar chart (using any library like ggplot2) for the below data format.

sno  type  state
t1  type1   A
t2  type2   C
t3  type3   A
t4  type1   B
t5  type3   B
t6  type3   B
t7  type3   C
t8  type2   A
t9  type2   C
t10 type2   B

Want to render chart with 'state' on X-axis and 'type' on y axis

Nov 5, 2018 in Data Analytics by Hannah
• 18,520 points
952 views

2 answers to this question.

0 votes

Use ggplot2 to map the variables to the aesthetics just as you described. You don't want type on the y-axis - you want type on another axis, in this case fill for stacks of different colors. The default y aesthetic for a bar plot is count.

library(ggplot2)

mydata <- read.csv(
  text = "s no,type,state
  t1,type1,A
  t2,type2,C
  t3,type3,A
  t4,type1,B
  t5,type3,B
  t6,type3,B
  t7,type3,C
  t8,type2,A
  t9,type2,C
  t10,type2,B")
ggplot(mydata, aes(x = state, fill = type)) + 
  geom_bar()
answered Nov 5, 2018 by Kalgi
• 52,350 points
0 votes

Use position = stack inside geom_bar()

ggplot(mydata, aes(x = state, fill/color = type)) + geom_bar(position = "stack")

answered Aug 23, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

0 votes
1 answer

How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by MD 8,874 views
0 votes
1 answer

R Programming: Finding items with exceptional sequence

Here's a code that will help with ...READ MORE

answered Feb 27, 2019 in Data Analytics by Sophie may
• 10,620 points
720 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,047 views
+1 vote
1 answer

Plotting standard error in R

You can probably use dot plot for ...READ MORE

answered Nov 6, 2018 in Data Analytics by Kalgi
• 52,350 points
691 views
0 votes
1 answer

How to color geom_bar by y-axis values?

You can use cut ggplot(cars, aes(x = as.factor(cyl))) + ...READ MORE

answered Dec 6, 2018 in Data Analytics by Maverick
• 10,840 points
1,320 views
0 votes
1 answer

Loop to automate the plotting process in R

Try something like this: library(tidyverse) data(iris) ## create a grid ...READ MORE

answered Dec 7, 2018 in Data Analytics by Maverick
• 10,840 points
1,698 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,839 views
0 votes
1 answer

Most common errors faced when programming with R

These the two most common errors I ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
700 views
0 votes
2 answers

Integration of R with Java

there is a package called rjava that ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
991 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP