如何在 ggplot 堆栈条形图上添加总计线

How to add total line on a ggplot stack bar chart

我很难在这个堆叠条形图的总和之上添加一行:

demandDriversdf = structure(list(year = structure(c(1356998400, 1388534400, 1420070400,
                                      1451606400, 1483228800), tzone = "GMT", tclass = c("POSIXct",
                                                                                         "POSIXt"), class = c("POSIXct", "POSIXt")), one= c(12.4882571461364,
                                                                                                                                            13.0984912135388, 12.1908055157534, 8.35335266490711, 4.38593754938248
                                                                                         ), two= c(8.73113484771066, -4.34931681021004, -3.04955505552055,
                                                                                                   -1.69136803847247, 3.06500464975644), three= c(0.0669199673877559,
                                                                                                                                                -0.194488564805058, 0.721483847234409, 2.85829802643513, 6.14894193920574
                                                                                                   ), four= c(6.98748008979101, 3.7122726468811, -15.0029846301367,
                                                                                                                     -20.3768539034347, 9.38948700033012)), .Names = c("year", "one",
                                                                                                                                                                       "two", "three", "four"), row.names = c("2013-01-01", "2014-01-01",
                                                                                                                                                                                                                "2015-01-01", "2016-01-01", "2017-01-01"), class = "data.frame")

    demandDriversdf2 = reshape2::melt(demandDriversdf, id.vars=c("year"), value.name="driver")
    rowS = rowSums(demandDriversdf[,setdiff(colnames(demandDriversdf),"year")])
    demandDriversdf2 = rbind(demandDriversdf2, data.frame(year = names(rowS), variable="Total", driver = rowS))
    demandDriversdf2$year=substr(demandDriversdf2$year,1,4)
    demandDriversdf2_1 <- subset(demandDriversdf2,driver >= 0 & variable!="Total")
    demandDriversdf2_2 <- subset(demandDriversdf2,driver < 0 & variable!="Total")
    gdemandDrivers = ggplot2::ggplot() + 
      ggplot2::geom_bar(data = demandDriversdf2_1, aes(x=year, y=driver, fill=variable),stat = "identity") +
      ggplot2::geom_bar(data = demandDriversdf2_2, aes(x=year, y=driver, fill=variable),stat = "identity") +
      ggplot2::geom_line(data = subset(demandDriversdf2, variable=="Total"), aes(x=year, y=driver)) +
      ggplot2::scale_fill_brewer(palette = 2, type = "qual")

我收到这个奇怪的警告

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

并且理想情况下希望线条为黑色并带有黑色点。

只需将 group = 1 添加到 geom_line() 中的 aes():

library(ggplot2)

ggplot() + 
  geom_bar(data = demandDriversdf2_1, aes(x=year, y=driver, fill=variable),stat = "identity") +
  geom_bar(data = demandDriversdf2_2, aes(x=year, y=driver, fill=variable),stat = "identity") +
  geom_line(data = subset(demandDriversdf2, variable=="Total"),   
            aes(x=year, y=driver, group = 1)) +
  scale_fill_brewer(palette = 2, type = "qual")

这样做的原因:

For line graphs, the data points must be grouped so that it knows which points to connect. In this case, it is simple -- all points should be connected, so group=1. When more variables are used and multiple lines are drawn, the grouping for lines is usually done by variable.

参考:R Cookbook,章节:图表 Bar_and_line_graphs_(ggplot2),折线图。