1

I have two variables that I want to plot in R, one of them (B) is missing some data points. I want to draw a line between the existing points in B

This the image I get from R R output

I want to connect the blue points at 1, 5, and 10. If I replace NA with zeros, I get the same image since my Y range starts from 1. And I do not want to have the line going down to zero and making a point there.

This is the code I have so far:

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")
Lily
  • 816
  • 7
  • 18
  • 38

3 Answers3

3

when plotting you can always give x- and y-coordinates of your points, so just remove the NA from B and give the x-coordinates to lines(), so like:

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

Does this help?

Dominic
  • 46
  • 4
0

To have the x values along with the B values, you can also put them all in a data.frame and remove the rows with 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

Then you can just plot:

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")
Alexlok
  • 2,999
  • 15
  • 20
0

Using gglot2 and company could look like the following.

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))

Which gives us:

enter image description here

rjen
  • 1,938
  • 1
  • 7
  • 19