代码之家  ›  专栏  ›  技术社区  ›  Preston

当颜色添加到高亮段时,geom_线显示2条线

  •  1
  • Preston  · 技术社区  · 8 年前

    geom_line - different colour in the same line

    my plot

    代码:

    library(ggplot2)
    library(zoo)
    
    ggplot(b, aes(x = ym, y = widgets, colour = (month %in% c(1, 2, 3) & year == 2017))) +
      geom_line(stat='summary', fun.y = sum, size = 1.5, aes(group = 1)) +
      stat_summary(fun.y = sum, geom = "line") +
      theme(legend.position = "none")
    

    数据:

    b <- structure(list(ym = structure(c(2015, 2015, 2015.08333333333, 
    2015.08333333333, 2015.16666666667, 2015.16666666667, 2015.25, 
    2015.25, 2015.33333333333, 2015.33333333333, 2015.41666666667, 
    2015.41666666667, 2015.5, 2015.5, 2015.58333333333, 2015.58333333333, 
    2015.66666666667, 2015.66666666667, 2015.75, 2015.75, 2015.83333333333, 
    2015.83333333333, 2015.91666666667, 2015.91666666667, 2016, 2016, 
    2016.08333333333, 2016.08333333333, 2016.16666666667, 2016.16666666667, 
    2016.25, 2016.25, 2016.33333333333, 2016.33333333333, 2016.41666666667, 
    2016.41666666667, 2016.5, 2016.5, 2016.58333333333, 2016.58333333333, 
    2016.66666666667, 2016.66666666667, 2016.75, 2016.75, 2016.83333333333, 
    2016.83333333333, 2016.91666666667, 2016.91666666667, 2017, 2017, 
    2017.08333333333, 2017.08333333333, 2017.16666666667, 2017.16666666667, 
    2017.25, 2017.25, 2017.33333333333, 2017.33333333333, 2017.41666666667, 
    2017.41666666667, 2017.5, 2017.5, 2017.58333333333, 2017.58333333333
    ), class = "yearmon"), month = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 
    6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 1, 1, 2, 2, 3, 
    3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 
    1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8), year = c(2015, 
    2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
    2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
    2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 
    2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 
    2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
    2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017), widgets = c(104.58, 
    66.03, 113.16, 63.93, 31.58, 24.9, 29.38, 21.1, 52.51, 43.62, 
    32.02, 8.78, 28.84, 36.44, 14.57, 33.87, 18.99, 11.43, 33.6, 
    17.18, 23.39, 82.92, 72.38, 51.53, 78.38, 90.57, 53.22, 34.19, 
    65.97, 40.22, 41.17, 46.42, 23.06, 24.81, 39.71, 71.13, 1.35, 
    13.1, 3.6, 6.56, 26.66, 5.38, 7.7, 5.65, 11.39, 13.53, 2.42, 
    5.39, 5.46, 7.78, 8.92, 8.23, 8.04, 7.18, 18.47, 3.72, 18.27, 
    25.17, 11.82, 6.68, 8.63, 26.38, 19.56, 21.76), d = c(1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L)), class = "data.frame", row.names = c(NA, 
    -64L), .Names = c("ym", "month", "year", "widgets", "d"))
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   erc    8 年前

    只要摆脱 stat_summary() 部分,这是多余的无论如何,因为你有它 geom_line()

    ggplot(b, aes(x = ym, y = widgets, colour = (month %in% c(1, 2, 3) & year == 2017))) +
      geom_line(stat = 'summary', fun.y = sum, size = 1.5, aes(group = 1)) +
      #stat_summary(fun.y = sum, geom = "line") +
      theme(legend.position = "none")
    

    enter image description here

    推荐文章