2017-08-11 28 views
1

按照问题2线/接听这里:geom_line显示当添加颜色以突出段

geom_line - different colour in the same line

我想强调我行的一部分。但是我有一个部分加入橙段之间的线(更容易看到图片):

my plot

我怎样才能避免这种情况? (奖励积分,如果你能摆脱统计摘要部分,并纳入像geom_bar的统计=“身份”一个总和部分给予每月总和)

代码:

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

只是补充说'yearmon'是在'zoo'包 –

+0

感谢@JannaMaas,我实际上添加它使用动物园包 – User632716

+1

我意味着谁想要帮助你的人:) –

回答

3

刚刚摆脱了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

+1

完美,非常感谢! – User632716