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.
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.
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.
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.
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.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With