简体   繁体   中英

UPDATED: Line Plot Becomes Zig-Zag in R

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

Using base graphics

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

using ggplot

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

# 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, ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM