且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据“集"或“因数"(而非组)为晶格中的图分配颜色

更新时间:2023-01-28 12:02:09

***使用panel.number()来实现,它可以在调用自定义面板函数的过程中访问当前正在绘制的面板的索引.

这是一个简单的可复制示例:

library(lattice)
lattice.options(default.theme = standard.theme(color = FALSE))
COLORS = c("blue", "gold", "red", "green", "black", "grey")

xyplot(mpg ~ disp | carb, data = mtcars,
       panel = function(x,y,...) { 
           panel.xyplot(x, y, col=COLORS[panel.number()], pch=16, ...)
       })

此处此处.

I have a lattice plot of experimental data that has 4 datasets (i.e. 4 different subsets in "Config." below), each dataset subdivided according to the config and then also grouped within each test type according to wind direction, "Dir". I am able to set the shape of the points according to the grouping "Dir" across all plots but i cannot then set colours according to "Config" so that for example the first plot has all red points, the second plot has all blue points etc. I am using an xyplot in lattice and the code for the plot is:

xyplot(ach[,"F"]~ach[,"Ar_sqrt"] | Config., type=c("p"),ach, groups=Dir, 
   auto.key=list(TRUE, space="bottom", columns=4, padding.text=(8)), 
   Outer=TRUE, allow.multiple=TRUE, as.table=TRUE, scales=list(tick.number=10),
   grid=TRUE, par.settings=simpleTheme(pch=points$shape, cex=1.75, col=1),
   abline=list(c(0,0.2013), col=2, type="l"), layout=c(1,4),  
   panel = function( x,y,...) {
     panel.abline(lm(y~x),col = "black", lwd=1)
     panel.xyplot( x,y,...)
   })

I cant attach an image as my rep is les sthan 10, apologies. I have come across different ways of setting colours, etc but they all seem to either use the grouping function.

the data set is available here, (i added columns for pch and col to try and use this but to no avail:

https://www.dropbox.com/sh/ug9kun9rycsb1fw/AABSXuMs9kEWSgeEAcTYhDkxa

any help or direction to duplicates i didnt find very much appreciated...

This is best accomplished using panel.number(), which gives you access, inside the call to a custom panel function, to the index of the panel being currently plotted.

Here's a simple reproducible example:

library(lattice)
lattice.options(default.theme = standard.theme(color = FALSE))
COLORS = c("blue", "gold", "red", "green", "black", "grey")

xyplot(mpg ~ disp | carb, data = mtcars,
       panel = function(x,y,...) { 
           panel.xyplot(x, y, col=COLORS[panel.number()], pch=16, ...)
       })

A couple more involved examples are given here and here.