iskandarblue
iskandarblue

Reputation: 7526

Plotting a single row with NA values in R

I have a table flipped whose first row I would like to visualize with the plot() funciton in R. How is it possible to do this with a simple command given that NA values must not be present in the arguments of plot() ?

> flipped[1,]
  variable 26500 30000 30100 30700 31600 33700 33800 33900 34000 34600 34800 35100 35200 35300 35400 35600 35800
1      -20    NA     0    NA    NA    10    20    NA    NA    NA    30    NA    NA    NA    NA    40    NA    NA
  35900 36200 36300 36400 36700 36900 37000 37200 37800 37900 38000 38200 38800 39000 39100 39200 39700 39800 39900
1    NA    50    NA    NA    NA    NA    NA    60    NA    NA    NA    70    NA    NA    NA    80    NA    NA    NA
  40000 40200 40600 40700 40800 41700 41800
1    NA    90    NA    NA    NA   100    NA

na.omit() removes the entire row, and is not a parameter in the plot() funciton. As for the y axis, I understand that the colnames must be made numeric fist before being plotted :

as.numeric(colnames(flipped[2:ncol(flipped)]))
 [1] 26500 30000 30100 30700 31600 33700 33800 33900 34000 34600 34800 35100 35200 35300 35400 35600 35800 35900 36200
[20] 36300 36400 36700 36900 37000 37200 37800 37900 38000 38200 38800 39000 39100 39200 39700 39800 39900 40000 40200
[39] 40600 40700 40800 41700 41800

How would one go about plotting x(1st row with NAs removed) over y(corresponding colnames) in this case?

Upvotes: 1

Views: 35

Answers (1)

akrun
akrun

Reputation: 887098

We can use is.na on the unlisted row

v1 <- unlist(flipped[1, -1])
plot(v1[!is.na(v1)], names(v1)[!is.na(v1)])

Upvotes: 2

Related Questions