如何获取我的点以连接图并显示具有NA值的趋势? [英] How can I get my points to connect in a plot and show a trend with NA values in data?

查看:179
本文介绍了如何获取我的点以连接图并显示具有NA值的趋势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组基本数据,我在其中测量24个月内的浓度. 某些月份还没有采样,因此我的列表中有6个NA值.

I have a basic set of data, where I measure concentration over time for 24 months. Some months have not been sampled, so there are 6 NA values in my list.

na.omit()函数删除了我的NA值,并为我提供了所要查找的图形,但它破坏了我的坐标轴.

The na.omit() function removes my NA values and gives me the graph I am looking for, but it ruins my axis.

(1)

 plot(time, pt, type="o", pch=16, col="blue", 
    xlab="Time (months) relative to implant", ylab="Concentration (ng/ml)", 
    main="Concentration Overtime", 
    xaxt='n')
    axis(1, at=seq(-1, 24, by=1))

(2)

 plot(na.omit(pt), type="o", pch=16, col="blue", xlab="Time (months) relative 
        to implant", ylab="Concentration (ng/ml)", 
        main="Concentration Overtime", 
        xaxt='n')
        axis(1, at=seq(-1, 24, by=1))


我的图看起来像这样


My graph looks like this

图1

图2

推荐答案

使用Gwang-Jin Kim的答案中的数据以及将NA所在的位置划线的建议,这是一个重做.我将在下面保留以前的答案.

Using data from Gwang-Jin Kim's answer and the suggestion of dashing the lines where NAs exist, here's a redo. I'll keep the previous answer below.

lines(因此是plot(..., type="l"))要求所有组件的lty都相同,因此要获得虚线部分,您需要在每个点对点上使用segments.

lines (and therefore plot(..., type="l")) requires that lty be the same for all components, so to get dashed sections you need to use segments over each point-to-point.

为了获得额外的荣誉,我在x轴上添加了红点,当NA值存在时,数据被丢弃.

For extra credit, I'm including red dots along the x-axis where data was dropped at the time the NA value exists.

df <- data.frame(time=c(-1, 0, 1:24),
                 pt=c(7.0, 6.9, NA, 5.5, 5, 3, 14, NA, 23, NA, 14.5, 7, 9, NA,
                      11, 8, 5.2, 5.3, NA, 5, 3, NA, 1.5, NA, NA, 2))
len <- nrow(df)
notna <- !is.na(df$pt)
df$dashes <- c(TRUE, !notna[-len])
df0 <- df[notna,]
len0 <- nrow(df0)

plot(pt ~ time, data=df0,
     type="p", pch=16, col="blue", 
     xlab="Time (months) relative to implant", ylab="Concentration (ng/ml)", 
     main="Concentration Overtime", 
     xaxt='n')
points(df$time, par('usr')[3] * is.na(df$pt), pch = 16, col = "red")
ign <- Map(segments, df0$time[-len0], df0$pt[-len0],
           df0$time[-1], df0$pt[-1],
           1, 1+df0$dashes[-1])
axis(1, at=seq(-1, 24, by=1))

这是一个猜测:

notna <- !is.na(pt)
plot(time[notna], pt[notna], type="o", pch=16, col="blue", 
    xlab="Time (months) relative to implant", ylab="Concentration (ng/ml)", 
    main="Concentration Overtime", 
    xaxt='n')
    axis(1, at=seq(-1, 24, by=1))

第二个代码中的一个问题

One problem in your second code

plot(na.omit(pt), ...)

是因为您没有包含time,因此R自然会用seq_along(na.omit(pt))填充(几乎与1:length(na.omit(pt))相同),这对于您来说会丢失time数据.通过使用[notna]子集这两个向量,我们可以保留绘图所需的数据.

is that you have not included time, so R naturally fills in with seq_along(na.omit(pt)) (almost the same as 1:length(na.omit(pt))), which for you is losing your time data. By using [notna] to subset both vectors, we are preserving the data you need for the plot.

这篇关于如何获取我的点以连接图并显示具有NA值的趋势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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