0

I have some question regarding how to plot line graph in R with data containing NA value. Here is an extract of the data:

x y
NA 8
3 NA
NA 9
-3.11 2.37
-2.90 2.45
-3.45 2.02

I tried to do the following:

plot(na.omit(df$x), na.omit(df$y), type="l", 
 main="y vs x",
 xlab="x",
 ylab="y")

I got the error like this:

xy.coords(x, y, xlabel, ylabel, log) Error: 'x' and 'y' lengths differ

(a) How do I plot using the basic R plot function? (b) How do I plot using the GGplot2?

Thanks in advance.

-Updated-

When I plot a line graph in R, the graph goes from x=-3.11 to x=-2.90 to x=-3.45. How do I arrange it so that the graph goes from x=-3.45 to x=-3.11 to x=-2.90?

-Solved-

I have found the solution to the above question. Just sort the data frame first. df <- df[order(df$x), ]

mochalatte
  • 37
  • 4

2 Answers2

1

Using base graphics

plot(na.omit(df)["x"], na.omit(df)["y"])

using ggplot

ggplot(na.omit(df),aes(y=y,x=x))

user2974951
  • 9,535
  • 1
  • 17
  • 24
  • Thanks so much! In fact, I have just found another solution using the base graphics:
    na.omit(cbind(x=df$x, y=df$y)
    Thanks a lot!
    – mochalatte Jan 25 '21 at 08:17
1
# your data
x <- c(7, NA, 90, 3, 57, NA)
y <- c(9,8,4,NA, 9, 9)

# 1.)
plot(x~y, type="b", data=na.omit(data.frame(x,y)))

# 2.)
plot(x~y, type="b", subset=complete.cases(x,y))

# 3.)
s <- complete.cases(x,y)
plot(y[s], x[s], type="b")

@ mochalatte, Answer to questions in the comment:

The ~ (tilde) operator characterizes formulas in R. The variable on the left (in our example x) is the dependent variable.The variable on the right side (in our example y) is the independent variable. Usually a dependent variable x is explored by one or more independent variables. If you change like y~x then your dependent and independent variable is also changing. see: Use of ~ (tilde) in R programming Language and In R formulas, why do I have to use the I() function on power terms, like y ~ I(x^3)

Meaning of subset=complete.cases(x,y):

  • complete.cases: returns a logical vector indicating which cases are complete, i.e., have no missing values.
  • subset: returns subsets of vectors, matrices or data frames which meet conditions.

In our case x,y without NA. Hope this helps.

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thank you so much for your reply. Does x~y means with x as the x-axis data, y as the y-axis data? What happen if I reverse it as y~x? Also, what is the meaning of subset=complete.cases(x,y)? – mochalatte Jan 25 '21 at 09:00