R 中的堆积条形图 (ggplot) :: 尝试在图表中创建多个条形图

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

我目前正在尝试根据以下数据集创建堆积条形图:

数据说明: 每个奇数列代表公司变量,每个偶数列代表该公司的生产。 每两列(公司和生产)代表该小时的生产模式。

这是我的数据:

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))

我能够使用下面的代码创建第一个小时的图表:

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")

条形图如下图:

现在的问题:

我编写了一个代码来为 "Company" 变量和 "Production" 变量创建数据集。但是当我 运行 代码时,我有这个错误:

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

我想知道我犯了什么技术错误,我该如何解决。 这是我的代码:

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]
}

如有任何帮助,我们将不胜感激。

不清楚您要做什么。例如。 data.frame 中的变量命名不正确,Hour 甚至不在 data.frame.

ggplot2 要求所有变量都在您提供的 data.frame 中(即代码中的 dataset )。您正在 data.frame 之外创建具有合理名称 的新对象。您应该改为重命名变量。 hour 变量的长度为 1,因此不清楚您要用它做什么。

这是我根据您的代码所能想到的最佳结果:

#load data
dataset = structure(list(X1 = structure(c(4L, 4L, 4L, 4L, 2L, 3L, 4L, 4L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"B", "C", "D"), class = "factor"), X1.1 = c(5L, 73L, 86L, 30L, 
49L, 83L, 31L, 63L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), X2 = structure(c(4L, 2L, 4L, 4L, 2L, 4L, 4L, 2L, 3L, 
2L, 2L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"A", "B", "D"), class = "factor"), X2.1 = c(71L, 74L, 5L, 73L, 
78L, 32L, 98L, 36L, 34L, 15L, 58L, 52L, 50L, NA, NA, NA, NA, 
NA, NA, NA, NA), X3 = structure(c(2L, 2L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 4L, 5L, 4L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"A", "B", "C", "D"), class = "factor"), X3.1 = c(33L, 85L, 11L, 
14L, 95L, 82L, 42L, 68L, 50L, 19L, 43L, 57L, 84L, 81L, 1L, NA, 
NA, NA, NA, NA, NA), X4 = structure(c(4L, 1L, 1L, 4L, 4L, 4L, 
4L, 1L, 1L, 4L, 4L, 3L, 3L, 3L, 4L, 2L, 2L, 4L, 2L, 4L, 4L), .Label = c("A", 
"B", "C", "D"), class = "factor"), X4.1 = c(100L, 81L, 48L, 97L, 
43L, 3L, 38L, 28L, 47L, 7L, 42L, 24L, 4L, 54L, 9L, 40L, 71L, 
28L, 2L, 69L, 64L), X5 = structure(c(5L, 5L, 3L, 5L, 5L, 5L, 
5L, 2L, 2L, 5L, 5L, 5L, 3L, 5L, 3L, 5L, 3L, 4L, 1L, 1L, 1L), .Label = c("", 
"A", "B", "C", "D"), class = "factor"), X5.1 = c(58L, 1L, 39L, 
68L, 45L, 78L, 0L, 97L, 60L, 25L, 60L, 12L, 34L, 30L, 0L, 73L, 
46L, 38L, NA, NA, NA), X6 = structure(c(3L, 4L, 3L, 5L, 5L, 3L, 
3L, 2L, 3L, 5L, 3L, 5L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
"A", "B", "C", "D"), class = "factor"), X6.1 = c(65L, 3L, 34L, 
48L, 70L, 96L, 4L, 68L, 42L, 95L, 29L, 8L, 1L, 92L, NA, NA, NA, 
NA, NA, NA, NA)), .Names = c("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, 
-21L))

#rename and add Hour
names(dataset)[1:2] = c("Company", "Production")
dataset$Hour = 1

#plot
library(ggplot2)
ggplot(dataset, aes(Hour, Production, fill = Company)) +
  geom_bar(stat = "identity")

其输出为:

考虑阅读 a textbook on R first and the ggplot2 textbook

根据我在问题中的理解,您正在尝试创建一个条形图来显示按公司分隔的每个小时的产量,其中每个条形代表不同的时间。

首先,ggplot2 与 data.frames 一起工作,其中每个变量都是不同的列,因此您的第一步应该是将数据转换为 this format。有几种方法可以做到这一点。

有了它,很容易得到你需要的东西:

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

此外,您可能希望消除堆叠条形图中重复的颜色,以便您可以更轻松地查看每家公司的总产量。为此,您需要使用 weight 美学而不是 identity 统计数据,如下所示:

ggplot(data = df2, aes(x = Hour, weight = Production)) +
  geom_bar(aes(fill = Company))

希望对您有所帮助!