Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacked Bar Plot in R

I've looked at the similar questions on here regarding stacked bar plots in R, but I'm still not having any luck.

I have created the following data frame:

        A   B   C   D   E   F    G      1 480 780 431 295 670 360  190      2 720 350 377 255 340 615  345      3 460 480 179 560  60 735 1260      4 220 240 876 789 820 100   75 

A:G represents the x-axis and the y-axis would be duration (seconds). How would I go about stacking the following data in R?

Thank you very much in advance for your time and help.

like image 337
Evelyn Avatar asked Dec 03 '13 10:12

Evelyn


People also ask

How do I plot a stacked bar in R?

In order to create a stacked bar chart, also known as stacked bar graph or stacked bar plot, you can use barplot from base R graphics. Note that you can add a title, a subtitle, the axes labels with the corresponding arguments or remove the axes setting axes = FALSE , among other customization arguments.

What are stacked bar plots?

A stacked bar graph (or stacked bar chart) is a chart that uses bars to show comparisons between categories of data, but with ability to break down and compare parts of a whole. Each bar in the chart represents a whole, and segments in the bar represent different parts or categories of that whole.

What is a stacked barplot in R?

A stacked barplot is a type of chart that displays quantities for different variables, stacked by another variable. This tutorial explains how to create stacked barplots in R using the data visualization library ggplot2.

How do you make a stacked bar graph in R?

In order to create a stacked bar chart, also known as stacked bar graph or stacked bar plot, you can use barplot from base R graphics. Note that you can add a title, a subtitle, the axes labels with the corresponding arguments or remove the axes setting axes = FALSE, among other customization arguments.

What is a grouped bar plot in R?

Grouped barplot in R A grouped barplot, also known as side by side bar plot or clustered bar chart is a barplot in R with two or more variables. The chart will display the bars for each of the multiple variables.

How to create a stacked bar plot for multiple variables?

The bar plot will display the stacked sum for each group of the variable. Setting stat = "identity" you can create a stacked bar plot for multiple variables. In this scenario you can pass other variable to aes, representing the value or count of that variable.


1 Answers

The dataset:

dat <- read.table(text = "A   B   C   D   E   F    G 1 480 780 431 295 670 360  190 2 720 350 377 255 340 615  345 3 460 480 179 560  60 735 1260 4 220 240 876 789 820 100   75", header = TRUE) 

Now you can convert the data frame into a matrix and use the barplot function.

barplot(as.matrix(dat)) 

enter image description here

like image 112
Sven Hohenstein Avatar answered Oct 02 '22 12:10

Sven Hohenstein