根据“集"或“因数"(而非组)为晶格中的图分配颜色 [英] assigning colours to plots in lattice according to Set or factor, not groups

查看:73
本文介绍了根据“集"或“因数"(而非组)为晶格中的图分配颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实验数据的格子图,其中包含4个数据集(即以下配置"中的4个不同子集),每个数据集均根据配置进行细分,然后还根据风向在每种测试类型内进行分组,"Dir ".我能够根据所有图上的"Dir"分组来设置点的形状,但是我不能再根据"Config"来设置颜色,例如,第一个图具有所有红点,第二个图具有所有蓝点等等.我在晶格中使用xyplot,该图的代码为:

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,...)
   })

我无法附加图片,因为我的代表是les than 10,抱歉.我遇到了多种设置颜色的方法,等等,但是它们似乎都使用了分组功能.

数据集在此处可用,(我为pch和col添加了列以尝试使用它,但无济于事:

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

对重复的任何帮助或指示,我都非常感激...

解决方案

最好使用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.

这篇关于根据“集"或“因数"(而非组)为晶格中的图分配颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆