How to color dots depending on a numerical threshold using the xyplot R-grid

I would like to colorize the dots in each panel based on the z value. The code I currently have does not distinguish the z value from each panel. I understand that I need a panel function and a panel. Maybe this will help, but I'm lost.

x <- c(1:10, 1:10) y <- c(10:1, 10:1) z <- c(1:10, seq(1,20, by=2)) a = c(rep("one",10),rep("two",10)) xyplot(y ~ x |a, panel=function(x,y, ...) { panel.xyplot(x,y, pch=20, cex=0.3, col = ifelse(z < 5, "red", "black")) } ) 

The correct plot will have only two points: two points in the two panel.

+4
source share
1 answer

This should be what you want:

 DF <- data.frame(x, y, z, a) xyplot(y ~ x | a, groups = z < 5, data = DF, col = c("black", "red"), pch=20, cex=0.3) 

enter image description here

To clarify the order of colors, z < 5 creates a logical vector. Since order(c(TRUE,FALSE)) sets FALSE to TRUE , this determines the order of the group colors. Therefore, all z<5 values ​​take a second color, and all other z values ​​take a first color.

+5
source

Source: https://habr.com/ru/post/1437701/


All Articles