-2

I have a data frame with 3 columns of data that I would like to plot separately - 3 plots. The data has NA in it (in different places within the 3 columns). I basically want to interpolate the missing values and plot that segment of the line (multiple sections) in red and the remainder of the line black.

I have managed to use 'zoo' to create the interpolated data but am unsure how then to plot this data point a different colour. I have found the following Elegant way to select the color for a particular segment of a line plot? but was thinking I could use a for loop with if else statement to create the colour column as advised in the link - I would need 3 separate colour columns as I have 3 datasets.

Appreciate any help - cannot really provide an example as I'm unsure where to start! Thanks

Community
  • 1
  • 1
Darren J
  • 503
  • 2
  • 5
  • 16
  • 2
    I suggest you prepare a small reproducible example. Here are a few tips on how to proceed. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Jul 20 '14 at 15:36
  • It's almost always the case the people coming to R from the SAS or SPSS work incorrectly reach for a `for( ){if ( ) {} else{} }` when they should be first looking at `?ifelse`. – IRTFM Jul 20 '14 at 18:36
  • Thanks BondedDust - I made it far too complicated I agree. This is the code I used for this if anyone is interested UN.GRACE$Col <- ifelse(is.na(UN.GRACE[2:4]), "red", "black") – Darren J Jul 21 '14 at 11:58

1 Answers1

1

This is my solution. It assumes that the NAs are still present in the original data. These will be omitted in the first plot() command. The function then loops over just the NAs.

You will probably get finer control if you take the plot() command out of the function. As written, "..." gets passed to plot() and a type = "b" graph is mimicked - but it's trivial to change it to whatever you want.

# Function to plot interpolated valules in specified colours.
PlotIntrps <- function(exxes, wyes, int_wyes, int_pt = "red", int_ln = "grey",
      goodcol = "darkgreen", ptch = 20, ...) {

  plot(exxes, wyes, type = "b", col = goodcol, pch = ptch, ...)

  nas <- which(is.na(wyes))
  enn <- length(wyes)

  for (idx in seq(nas)) {
    points(exxes[nas[idx]], int_wyes[idx], col = int_pt, pch = ptch)
    lines(
      x = c(exxes[max(nas[idx] - 1, 1)], exxes[nas[idx]],
        exxes[min(nas[idx] + 1, enn)]),
      y = c(wyes[max(nas[idx] - 1, 1)], int_wyes[idx],
        wyes[min(nas[idx] + 1, enn)]),
      col = int_ln, type = "c")

    # Only needed if you have 2 (or more) contiguous NAs (interpolations)
    wyes[nas[idx]] <- int_wyes[idx]
  }
}

# Dummy data (jitter() for some noise)
x_data <- 1:12
y_data <- jitter(c(12, 11, NA, 9:7, NA, NA, 4:1), factor = 3)
interpolations <- c(10, 6, 5)

PlotIntrps(exxes = x_data, wyes = y_data, int_wyes = interpolations, 
  main = "Interpolations in pretty colours!",
  ylab = "Didn't manage to get all of these")

Cheers.

Dale
  • 91
  • 3