0

I would like to plot last observation from my data.frame in different colour/shape. Now, one close entry about this topic is here at SO but not exactly what I need. This solution I'm posting here works, but I would be glad to have also other option/ways to deal with this matter within lattice as this seems clumsy somewhat to me.

Sample data:

set.seed(123)
data <- data.frame(x=rnorm(10,0,1),y=rnorm(10,1,2))
n <- nrow(data)

plot1 <- xyplot(x~y, data, col=ifelse(data$x==(data$x[n]), "red", "black"), 
                pch=ifelse(data$x==(data$x[n]), 19, 1), cex=ifelse(data$x==(data$x[n]), 2, 1))
print(plot1)

EDIT: Also the above solution seems not to work if the last two observations are exactly the same. And this is what I'm having in my data occasionally.

Maximilian
  • 4,177
  • 7
  • 46
  • 85

1 Answers1

2

You could try using rep I suppose:

n<-nrow(data)-1

plot1 <- xyplot(x~y, data, col=c(rep("red",n),"black"), 
            pch=c(rep(19,n),1), cex=c(rep(2,n),1))

It is just repeating the col, pch and cex "n" times and then adding 1 additional value for the last row. Here "n" is the number of rows-1. This assumes that you data is sorted for this to work. You could also add the graphical parameters in separate columns (i.e df$Col, df$Pch etc.) and do col=df$Col, This would allow you to highlight all sorts of things regardless of sorting.

John Paul
  • 12,196
  • 6
  • 55
  • 75