使用 geom_line 连接缺失值 [英] Connecting across missing values with geom_line

查看:53
本文介绍了使用 geom_line 连接缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚是否可以使用 geom_line 连接缺失值.例如,在下面的链接中,F 方面的时间 3 处缺少值.在这种情况下,我想要一条线来连接时间 2 和时间 4.有没有办法做到这一点?

https://farm8.staticflickr.com/7061/6964089563_b150e0c2a6.jpg/p>

我有一个累积值的数据框,如下所示:

head(累计)个别系列时间价值1 A x 1 -1.0088212 A x 2 -2.2737123 A x 3 -3.4306104 A x 4 -4.6188605 A x 5 -4.8930756 A x 6 -5.836532

我正在绘制的内容:

ggplot(cumulative, aes(x=Time,y=Value, shape=series)) +geom_point() +geom_line(aes(linetype=series)) +facet_wrap(~个人,ncol=3)

Richie 的回答非常彻底,但我想展示一些更简单的东西.由于线没有绘制到 NA 点,另一种方法是在绘制线时删除这些点.这隐含地在点之间进行了线性插值(就像直线一样).

使用 Richie 的回答中的 dfr,无需计算 z 步骤:

ggplot(dfr, aes(x,y)) +geom_point() +geom_line(data=dfr[!is.na(dfr$y),])

就此而言,在这种情况下,可以对整个事物进行子集化.

ggplot(dfr[!is.na(dfr$y),], aes(x,y)) +geom_point() +geom_line()

I'm trying to figure out if it's possible to connect across missing values using geom_line. For example, in the link below there are missing values at time 3 in facet F. I'd like a line to connect time 2 and 4 in that case. Is there a way to achieve this?

https://farm8.staticflickr.com/7061/6964089563_b150e0c2a6.jpg

I have a data frame of cumulative values like so:

head(cumulative)

  individual series Time     Value
1          A      x    1 -1.008821
2          A      x    2 -2.273712
3          A      x    3 -3.430610
4          A      x    4 -4.618860
5          A      x    5 -4.893075
6          A      x    6 -5.836532

Which I'm plotting with:

ggplot(cumulative, aes(x=Time,y=Value, shape=series)) + 
    geom_point() + 
    geom_line(aes(linetype=series)) + 
    facet_wrap(~ individual, ncol=3)

解决方案

Richie's answer is very thorough, but I wanted to show something simpler. Since lines are not drawn to NA points, another approach is drop these points when drawing lines. This implicitly makes a linear interpolation between points (as straight lines do).

Using dfr from Richie's answer, without needing the calculation of z step:

ggplot(dfr, aes(x,y)) + 
  geom_point() +
  geom_line(data=dfr[!is.na(dfr$y),])

For that matter, in this case the subsetting could be done for the whole thing.

ggplot(dfr[!is.na(dfr$y),], aes(x,y)) + 
  geom_point() +
  geom_line()

这篇关于使用 geom_line 连接缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆