在 R 中使用带有 ggplot2 的折线图覆盖带有多个条形图的条形图
Overlaying a Bar chart with multiple bars with a line graph with ggplot2 in R
我正在尝试使用 ggplot2 创建一个特定的图表,这是我的数据集。
df.1 <- data.frame(
Month = c("Dec-17", "Jan-18", "Feb-18", "Mar-18", "Apr-18", "May-18"),
Total_1 = c(25, 14, 8, 16, 137, 170),
Total_2 = c(3, 2, 3, 2, 18, 27),
Total_3 = c(5, 4, 3, 2, 16, 54)
)
我希望 Total_2 和 Total_3 是条形图,Total_1 是折线图,都在同一个图表上。我的编码已经走到这一步了:
df.1 <- melt(df.1,id.vars = "Month")
#reorder the month column so it isn't alphabetical
df.1$Month <- factor(df.1$Month, levels(df.1$Month)[c(2,4,3,5,1,6)])
#partition my data into the 2 different graphs I need
df.1.1 <- df.1[7:18,]
df.1.2 <- df.1[1:6,]
ggplot(data = df.1.1, aes(x = df.1.1$Month, y = df.1.1$value, fill = df.1.1$variable)) +
geom_bar(position = position_dodge(),stat = 'identity') +
geom_line(data = df.1.2, aes(x=df.1.2$id, y=df.1.2$value, group=1))
然后这给了我错误:
Error: Aesthetics must be either length 1 or the same as the data (6): x, y, fill
我的ggplot代码的前半部分,即:
ggplot(data = df.1.1, aes(x = df.1.1$Month, y = df.1.1$value, fill = df.1.1$variable)) +
geom_bar(position = position_dodge(),stat = 'identity')
这给了我下面的图表:
我只需要将线条部分放在上面。我可以使用这段代码自行生成折线图:
ggplot(data = df.1.2, aes(x = df.1.2$Month, y = df.1.2$value, group = 1)) +
geom_line()
看起来像这样:
非常感谢您提供任何帮助,让这个工作正常进行,如果您需要更多信息,请不要害怕询问。
谢谢!
这里的主要问题是 ggplot
调用中的 fill
。因为您在 ggplot(aes())
中拥有它,所以它正在传播到 geom_bar
和 geom_line
。如果你只是在 ggplot(aes()) 中设置 fill = variable
,你不会得到错误,但你会在图例中有一个 Total_1,这不是我认为你想要的。
您也不需要在 aes()
中使用 df.1.1$
和 df.1.2$
。您想将数据放在数据参数中,然后将变量放在 aes()
中,而无需再次调用数据框。
这是一个解决方案。
ggplot(data = df.1.1, aes(x = Month, y = value)) +
geom_bar(aes(fill = variable), position = position_dodge(),stat = 'identity') +
geom_line(data = df.1.2, aes(x=Month, y=value, group=1))
另一个注意事项是您可以使用 geom_col
而不是 geom_bar
和 stat='identity'
。
ggplot(data = df.1.1, aes(x = Month, y = value)) +
geom_col(aes(fill = variable), position = position_dodge()) +
geom_line(data = df.1.2, aes(x=Month, y=value, group=1))
我正在尝试使用 ggplot2 创建一个特定的图表,这是我的数据集。
df.1 <- data.frame(
Month = c("Dec-17", "Jan-18", "Feb-18", "Mar-18", "Apr-18", "May-18"),
Total_1 = c(25, 14, 8, 16, 137, 170),
Total_2 = c(3, 2, 3, 2, 18, 27),
Total_3 = c(5, 4, 3, 2, 16, 54)
)
我希望 Total_2 和 Total_3 是条形图,Total_1 是折线图,都在同一个图表上。我的编码已经走到这一步了:
df.1 <- melt(df.1,id.vars = "Month")
#reorder the month column so it isn't alphabetical
df.1$Month <- factor(df.1$Month, levels(df.1$Month)[c(2,4,3,5,1,6)])
#partition my data into the 2 different graphs I need
df.1.1 <- df.1[7:18,]
df.1.2 <- df.1[1:6,]
ggplot(data = df.1.1, aes(x = df.1.1$Month, y = df.1.1$value, fill = df.1.1$variable)) +
geom_bar(position = position_dodge(),stat = 'identity') +
geom_line(data = df.1.2, aes(x=df.1.2$id, y=df.1.2$value, group=1))
然后这给了我错误:
Error: Aesthetics must be either length 1 or the same as the data (6): x, y, fill
我的ggplot代码的前半部分,即:
ggplot(data = df.1.1, aes(x = df.1.1$Month, y = df.1.1$value, fill = df.1.1$variable)) +
geom_bar(position = position_dodge(),stat = 'identity')
这给了我下面的图表:
我只需要将线条部分放在上面。我可以使用这段代码自行生成折线图:
ggplot(data = df.1.2, aes(x = df.1.2$Month, y = df.1.2$value, group = 1)) +
geom_line()
看起来像这样:
非常感谢您提供任何帮助,让这个工作正常进行,如果您需要更多信息,请不要害怕询问。
谢谢!
这里的主要问题是 ggplot
调用中的 fill
。因为您在 ggplot(aes())
中拥有它,所以它正在传播到 geom_bar
和 geom_line
。如果你只是在 ggplot(aes()) 中设置 fill = variable
,你不会得到错误,但你会在图例中有一个 Total_1,这不是我认为你想要的。
您也不需要在 aes()
中使用 df.1.1$
和 df.1.2$
。您想将数据放在数据参数中,然后将变量放在 aes()
中,而无需再次调用数据框。
这是一个解决方案。
ggplot(data = df.1.1, aes(x = Month, y = value)) +
geom_bar(aes(fill = variable), position = position_dodge(),stat = 'identity') +
geom_line(data = df.1.2, aes(x=Month, y=value, group=1))
另一个注意事项是您可以使用 geom_col
而不是 geom_bar
和 stat='identity'
。
ggplot(data = df.1.1, aes(x = Month, y = value)) +
geom_col(aes(fill = variable), position = position_dodge()) +
geom_line(data = df.1.2, aes(x=Month, y=value, group=1))