Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

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
question from:https://stackoverflow.com/questions/65881038/updated-line-plot-becomes-zig-zag-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
# 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...