Stacked barchart - R programming with ggplot2

0 votes

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
• 14,070 points
42 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
• 40,420 points
0 votes

Use position = stack inside geom_bar()

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

answered Aug 22 by anonymous
• 25,580 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,700 points
1,398 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 in Data Analytics by Sophie may
• 9,530 points
20 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
47 views
0 votes
2 answers

Filtering R data-frame with multiple conditions

librarry(dplyr) dataset >%> filter((a ==1,b>15 ) | (a==2,b&g ...READ MORE

answered Aug 8 in Data Analytics by anonymous
5,334 views
0 votes
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
• 40,420 points
29 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,040 points
59 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 6, 2018 in Data Analytics by Maverick
• 10,040 points
31 views
0 votes
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21 in Data Analytics by anonymous
• 25,580 points
118 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
• 40,420 points
20 views
0 votes
2 answers

Integration of R with Java

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

answered Dec 4, 2018 in Data Analytics by Kalgi
• 40,420 points
36 views