如何绘制线图并忽略 R 中的缺失值

How to I draw a line plot and ignore missing values in R

我有两个变量想在 R 中绘制,其中之一 (B) 缺少一些数据点。我想在 B

中的现有点之间画一条线

这是我从 R 得到的图像

我想连接 1、5 和 10 处的蓝点。如果我用零替换 NA,我会得到相同的图像,因为我的 Y 范围从 1 开始。而且我不想让线继续降到零并在那里做一个点。

这是我目前的代码:

A<-c(187.674646,101.734756,85.674646,57.327278,51.207435,49.438306,47.939082,44.17294,42.613277,39.916711)
B<-c(27.13350001,     NA   ,     NA   ,   NA   ,41.5181   ,     NA  ,     NA  ,   NA    ,  NA     ,79.18489934)

plot(A, type="o", pch=16, cex=1.5, lty=1, lwd=0.25,  col="red", ylim=graph_range, log="y", axes=FALSE, ann=FALSE)
axis(1, at=1:10, lab=c("1","2","3","4","5","6","7","8","9","10"),cex.axis=0.95, las=1)
yticks <-c(10,100,1000,10000)
axis(2, at=yticks, lab=c("10","100","1000","10000"),cex.axis=0.95)
box()
lines(B, type="o", pch=16, cex=1.5, lty=1, lwd =0.25, col="blue")

绘图时,您始终可以给出 x- 和 y-coordinates 点,因此只需从 B 中删除 NA 并将 x-coordinates 赋予 lines(),例如:

lines(c(1,5,10),B)

这有帮助吗?

要获得 x 值和 B 值,您还可以将它们全部放在一个 data.frame 中并删除带有 NA:

的行
A<-c(187.674646,101.734756,85.674646,57.327278,51.207435,49.438306,47.939082,44.17294,42.613277,39.916711)
B<-c(27.13350001,     NA   ,     NA   ,   NA   ,41.5181   ,     NA  ,     NA  ,   NA    ,  NA     ,79.18489934)

df_B <- data.frame(x=seq_along(B),
                   B=B)
df_B_no_na <- na.omit(df_B)

df_B_no_na
#>     x       B
#> 1   1 27.1335
#> 5   5 41.5181
#> 10 10 79.1849

然后你就可以绘图了:

plot(A, type="o", pch=16, cex=1.5, lty=1, lwd=0.25,  col="red",
     log="y", axes=FALSE, ann=FALSE, ylim=c(10, max(A)))
axis(1, at=1:10, lab=c("1","2","3","4","5","6","7","8","9","10"),cex.axis=0.95, las=1)
yticks <-c(10,100,1000,10000)
axis(2, at=yticks, lab=c("10","100","1000","10000"),cex.axis=0.95)
box()
lines(df_B_no_na, type="o",
      pch=16, cex=1.5, lty=1, lwd =0.25, col="blue")

使用 gglot2 和 company 可能如下所示。

library(dplyr)
library(ggplot2)

a <- c(187.674646,101.734756,85.674646,57.327278,51.207435,49.438306,47.939082,44.17294,42.613277,39.916711)
b <- c(27.13350001, NA, NA, NA, 41.5181, NA, NA, NA, NA, 79.18489934)

data <- tibble(A = a,
               B = b,
               C = c(which(!is.na(b)), rep(NA, 7)),
               D = c(b[!is.na(b)], rep(NA, 7)))

data %>%
  ggplot() +
  geom_point(aes(x = 1:nrow(data), y = A), color = 'red') +
  geom_line(aes(x = 1:nrow(data), y = A), color = 'red') +  
  geom_point(aes(x = 1:nrow(data), y = B),  color = 'blue') +
  geom_line(aes(x = C, y = D),  color = 'blue') +
  scale_y_log10() +
  labs(x = NULL, y = NULL) +
  theme_minimal() +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(colour = 'black', fill = NA, size = 1))

这给了我们: