R ::堆叠条形图聊天(ggplot)尝试在图表中创建多个条形图

时间:2016-10-23 18:10:04

标签: r ggplot2 bar-chart aesthetics

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

Dataset Example

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

这是我的数据:

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

条形图如下图所示:

Bar Chart

现在出现问题:

我编写了一个代码来为“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]
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

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

ggplot2要求您提供的所有变量都在您提供的data.frame中(代码中为dataset)。您正在data.frame 之外创建具有合理名称​​的新对象。您应该重命名变量。小时变量长度为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")

其输出为: enter image description here

首先考虑阅读a textbook on Rthe ggplot2 textbook

答案 1 :(得分:0)

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

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

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

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

enter image description here

此外,您可能希望消除堆积条中的重复颜色,以便您可以更轻松地查看每个公司的总生产量。为此,你需要使用weight美学而不是identity stat,如下所示:

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

enter image description here

希望这有帮助!