R:使用 ggplot geom_line 防止显示时间序列数据的断线

Slo*_*ner 2 r ggplot2

使用 ggplot2 我想画一条在特定日期后改变颜色的线。我原以为这会很简单,但在颜色变化的地方我在线条上中断了。最初我认为这是一个问题group(根据这个问题;另一个问题看起来也相关,但不完全是我需要的)。在美学上搞砸了group30 分钟后我无法修复它,所以如果有人能指出明显的错误......

截屏

代码:

require(ggplot2)

set.seed(1111)
mydf <- data.frame(mydate = seq(as.Date('2013-01-01'), by = 'day', length.out = 10),
    y = runif(10, 100, 200))
mydf$cond <- ifelse(mydf$mydate > '2013-01-05', "red", "blue")

ggplot(mydf, aes(x = mydate, y = y, colour = cond)) +
    geom_line() +
    scale_colour_identity(mydf$cond) +
    theme()
Run Code Online (Sandbox Code Playgroud)

Mar*_*ius 5

如果设置group=1,则 1 将用作所有数据点的组值,并且线将连接起来。

ggplot(mydf, aes(x = mydate, y = y, colour = cond, group=1)) +
  geom_line() +
  scale_colour_identity(mydf$cond) +
  theme()
Run Code Online (Sandbox Code Playgroud)