Pang Chung Yang Pang Chung Yang - 1 month ago 10
R Question

Stacked Bar Chat (ggplot) in R:: Trying to create more than one bar in the chart

I am currently trying to create a stacked bar chart based on the dataset below:

Dataset Example

Explanation of data:
Every odd column represents the company variable and every even column represents the production by that company.
Every two columns(the company and the production) represents the production patterns for that hour.

This is my data:

structure(list(Hour = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), X1 = structure(c(4L,
5L, 5L, 5L, 5L, 2L, 3L, 5L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("", "B", "C", "Company", "D"), class = "factor"),
X1.1 = structure(c(10L, 5L, 7L, 9L, 2L, 4L, 8L, 3L, 6L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"30", "31", "49", "5", "63", "73", "83", "86", "Production"
), class = "factor"), X2 = structure(c(4L, 5L, 2L, 5L, 5L,
2L, 5L, 5L, 2L, 3L, 2L, 2L, 3L, 5L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("", "A", "B", "Company", "D"), class = "factor"),
X2.1 = structure(c(15L, 10L, 12L, 6L, 11L, 13L, 3L, 14L,
5L, 4L, 2L, 9L, 8L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"15", "32", "34", "36", "5", "50", "52", "58", "71", "73",
"74", "78", "98", "Production"), class = "factor"), X3 = structure(c(5L,
2L, 2L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 4L, 6L, 4L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "A", "B", "C", "Company",
"D"), class = "factor"), X3.1 = structure(c(17L, 6L, 15L,
3L, 4L, 16L, 13L, 7L, 11L, 9L, 5L, 8L, 10L, 14L, 12L, 2L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "1", "11", "14",
"19", "33", "42", "43", "50", "57", "68", "81", "82", "84",
"85", "95", "Production"), class = "factor"), X4 = structure(c(4L,
5L, 1L, 1L, 5L, 5L, 5L, 5L, 1L, 1L, 5L, 5L, 3L, 3L, 3L, 5L,
2L, 2L, 5L, 2L, 5L, 5L), .Label = c("A", "B", "C", "Company",
"D"), class = "factor"), X4.1 = structure(c(21L, 1L, 18L,
12L, 20L, 10L, 5L, 6L, 4L, 11L, 16L, 9L, 3L, 7L, 13L, 19L,
8L, 17L, 4L, 2L, 15L, 14L), .Label = c("100", "2", "24",
"28", "3", "38", "4", "40", "42", "43", "47", "48", "54",
"64", "69", "7", "71", "81", "9", "97", "Production"), class = "factor"),
X5 = structure(c(5L, 6L, 6L, 3L, 6L, 6L, 6L, 6L, 2L, 2L,
6L, 6L, 6L, 3L, 6L, 3L, 6L, 3L, 4L, 1L, 1L, 1L), .Label = c("",
"A", "B", "C", "Company", "D"), class = "factor"), X5.1 = structure(c(18L,
12L, 3L, 9L, 14L, 10L, 16L, 2L, 17L, 13L, 5L, 13L, 4L, 7L,
6L, 2L, 15L, 11L, 8L, 1L, 1L, 1L), .Label = c("", "0", "1",
"12", "25", "30", "34", "38", "39", "45", "46", "58", "60",
"68", "73", "78", "97", "Production"), class = "factor"),
X6 = structure(c(5L, 3L, 4L, 3L, 6L, 6L, 3L, 3L, 2L, 3L,
6L, 3L, 6L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"A", "B", "C", "Company", "D"), class = "factor"), X6.1 = structure(c(16L,
9L, 4L, 5L, 8L, 11L, 15L, 6L, 10L, 7L, 14L, 3L, 12L, 2L,
13L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "1", "29",
"3", "34", "4", "42", "48", "65", "68", "70", "8", "92",
"95", "96", "Production"), class = "factor")), .Names = c("Hour",
"X1", "X1.1", "X2", "X2.1", "X3", "X3.1", "X4", "X4.1", "X5",
"X5.1", "X6", "X6.1"), class = "data.frame", row.names = c(NA,
-22L))


I was able to use the code below to create a chart for the first hour:

dataset <- read_excel("Example.csv")
hour = 1
Production <- dataset[, 2]
Company <- dataset[, 1]
ggplot(data = dataset, aes(x = hour, y = Production, fill = Company)) +
geom_bar(stat = "identity")


The bar chart is pictured below:

Bar Chart

Now the problem:

I have written a code to create a dataset for the "Company" variable and the "Production" variable. But when I run the code, I have this error:


Aesthetics must be either length 1 or the same as the data (21): x, y, fill



I am wondering what technical error am I committing and how I can solve this.
This is my code:

hour <- matrix(0, 1, 2)
hour[1, 1] = 1
hour[1, 2] = 2
Production <- matrix(0, 22, 2)
for (i in 1:2) {
Production[1:22, i] <- dataset[1:22, (2 * i)]
}
Company <- matrix(0, 22, 2)
for (i in 1:2) {
Company[1:22, i] <- dataset[1:22, (2 * i) - 1]
}


Any help would be greatly appreciated.

Answer

From what I understand in the question, you are trying to create a bar chart that shows production in each hour separated by company, where each bar is a different hour.

Firstly ggplot2 works with data.frames where each variable is a different column, so your first step should be to convert your data into this format. There are several ways of doing so.

With that it is very easy to get what you need:

ggplot(data = df2, aes(x = Hour, y = Production, fill = Company)) +
  geom_bar(stat = 'identity')

enter image description here