在晶格xyplot中绘制每组面板数据的第一点 [英] plot the first point of each group of panel data in lattice xyplot
问题描述
我正在尝试使用组和面板创建折线图,在每个组的第一个值上叠加一个符号.此尝试仅绘制第一个组的第一个点,而对其他两个组不做任何事情.
I'm trying to create a line plot using groups and panels that superposes a symbol on the first value of each group. This attempt plots the first point of the first group only and does nothing for the other two groups.
library(lattice)
foo <- data.frame(x=-100:100, y=sin(c(-100:100)*pi/4))
xyplot( y~x | y>0, foo, groups=cut(x,3),
panel = function(x, y, subscripts, ...) {
panel.xyplot(x, y, subscripts=subscripts, type='l', ...)
# almost but not quite:
panel.superpose(x[1], y[1], subscripts=subscripts, cex=c(1,0), ...)
} )
可以理解一般解决方案,以允许在每个组和面板中绘制特定点(例如,第一,中间和端点).
General solutions would be appreciated, to allow plotting of specific points within each group and panel (e.g., first, middle, and endpoints).
推荐答案
(感谢这个很好的lattice
问题.)您应该使用下标,因为它是为面板选择单个数据点的机制:在这里,您想选择groups
by面板:groups[subscripts]
.一旦有了正确的分组变量,就可以使用它来拆分数据并选择每个组的第一个元素:
(Thanks for this good lattice
question.) You should use Subscripts because it is the mechanism for picking individual data points for panels : Here you want to pick the groups
by panel: groups[subscripts]
. Once you have the right grouping variables you can use it to split your data and pick the first element of each group:
## first points
xx = tapply(x,groups[subscripts],'[',1)
yy = tapply(y,groups[subscripts],'[',1)
## end points
xx = tapply(x,groups[subscripts],tail,1)
yy = tapply(y,groups[subscripts],tail,1)
您使用panel.points
(比基本panel.superpose
更高的水平)绘制点.
The you plot the point using panel.points
(higher level than the basic panel.superpose
).
library(lattice)
foo <- data.frame(x=-100:100, y=sin(c(-100:100)*pi/4))
xyplot( y~x | y>0, foo, groups=cut(x,3),
panel = function(x, y, subscripts, groups,...) {
panel.xyplot(x, y, subscripts=subscripts, type='l',groups=groups, ...)
# Note the use `panel.points` and the grouping using groups[subscripts]
panel.points(tapply(x,groups[subscripts],'[',1),
tapply(y,groups[subscripts],'[',1),
cex=2,pch=20, ...)
} )
如果要根据颜色组对点进行着色,则应在panel.points
中添加一个groups参数. (我把这个留给你练习)
In case you want to color points according the color groups, you should add a groups argument to panel.points
. (I leave you this as an exercise)
这篇关于在晶格xyplot中绘制每组面板数据的第一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!