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,080 points
21 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 35,720 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
912 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,050 points
12 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
27 views
0 votes
1 answer

Filtering R data-frame with multiple conditions

You can use the 'filter' function from ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
2,603 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
• 35,720 points
17 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,000 points
37 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,000 points
18 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
49 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
• 35,720 points
15 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
• 35,720 points
25 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.