0

I am having trouble displaing the contributing fractions in my depth plot, as their the top number at each 10cm depth should be in black and the second in blue not alternating every depth step like it is displayed now.

enter image description here

This is my code so far:

idx_veg <- which(a$LU == 'Vegetated LU')
veg <- slab(a[idx_veg, ], fm= ~ TOC + TIC + Clay + Silt + Sand + Nitrogen..)
idx_mod <- which(a$LU == 'Moderately Degraded LU')
mod <- slab(a[idx_mod, ], fm= ~ TOC + TIC + Clay + Silt + Sand + Nitrogen..)
#combine with the collection-wide aggregate data
g <- make.groups(veg=veg, mod=mod)
xyplot(top ~ p.q50 | variable, groups=which, data=g, ylab='Depth [cm]',
   xlab='median bounded by 25th and 75th percentiles',
   lower=g$p.q25, upper=g$p.q75, ylim=c(60,-2), #ylim defines the y-axis depth
   panel=panel.depth_function,
   alpha=0.15, sync.colors=TRUE, #alpha = transparency of 25-75% interval
   par.settings=list(superpose.line=list(col=c('black', 'RoyalBlue'), lwd=1, lty=c(1,2))), #median line, colour and width
   prepanel=prepanel.depth_function,
   cf=g$contributing_fraction, cf.col=c('black', 'RoyalBlue'), cf.interval=10, #contributing fractions (right hand side x-axis)
   layout=c(6,1), strip=strip.custom(bg=grey(0.8)), #Layout = arrangement of panels of plots
   scales=list(x=list(tick.number=4, alternating=3, relation='free')),# x, y-axis
   auto.key=list(columns=2, lines=TRUE, points=FALSE)
)

The warning I need to deal with and which will probably elimate the problem is:

In if (is.na(cf.col)) { ... :
the condition has length > 1 and only the first element will be used

Thanks for any advice.

dww
  • 30,425
  • 5
  • 68
  • 111
Znerky
  • 1
  • 1
  • How much of that code is required to reproduce the error? It would be better to post a **minimal** example where you reduce the code and data as muuch as possiuble while still reproducing the error. https://stackoverflow.com/help/mcve Also, it is expected to post data in the question that allows others to reproduce by simply cutting and pasting: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dww May 28 '18 at 22:07

1 Answers1

0

The warning is quite helpful in that case. The function xyplot() has code that check for any NA value in the cf.col parameter. In your case, you specified cf.col=c('black', 'RoyalBlue') in the function call so the code was telling you that the NA-check will only be performed on the first element, which in your case is the black value.

if(is.na(c(-2,NA,2))) print("hi")
# Warning:
In if (is.na(c(-2, NA, 2))) print("hi") :
  the condition has length > 1 and only the first element will be used

hi wasn't printed, because the first value, -2 is not NA and hence the condition failed.

However, consider the following example:

if(is.na(c(NA,NA,2))) print("hi")
[1] "hi"
Warning message:
In if (is.na(c(NA, NA, 2))) print("hi") :
  the condition has length > 1 and only the first element will be used

Notice this time the first element is NA and so that condition evaluates to true, and hi is subsequently printed.

The function you use, xyplot() probably was expecting one value and not a vector, hence the is.na() implementation. If I was the author, I would add an additional code to check if it was a vector of length 2 and above prior to the is.NA call, and return an appropriate suggestion, or use the anyNA function or the many other equivalent:

if(anyNA(c(NA,NA,2))) print("hi")
[1] "hi"

No warnings!

Hope this answers your question!

onlyphantom
  • 8,606
  • 4
  • 44
  • 58