我正在尝试在R中从一个相当复杂的数组中绘图。我想生成一个3×3的图像,每个图像上都有红色和蓝色的点。
我使用了apply循环的结构,它可以工作,但是我想通过每一行来改变y轴的最大值。
通常我会在其他语言中使用一个计数器,比如i,在R中,apply的东西完全让我困惑!
我使用了apply循环的结构,它可以工作,但是我想通过每一行来改变y轴的最大值。
通常我会在其他语言中使用一个计数器,比如i,在R中,apply的东西完全让我困惑!
par(mfrow=c(3,3),pty="s") # a 3 by 3 graphic
set.seed(1001)
x <- 1:54 # with 1 to 54 along the x axis
y <- array(rexp(20), dim=c(54,6,3,2)) # and the y axis coming
# from an array with dimensions as shown.
ymax <- c(1,0.1,0.3) # three different y maximum values I want
# on the graphic, one for each row of graphs
counter <- 1 # a counter, starting at 1,
# as I would use in a traditional loop
apply(y[,3:5,,], 2, function(i) # my first apply, which only considers
# the 3rd, 4th and 5th columns
{
yy <- ymax[counter] # using the counter to select my ylimit maximum
apply(i, 2, function (ii) # my second apply, considering the 3rd
# dimension of y
{
plot(x,ii[,1], col="blue", ylim=c(0,yy))
# plotting the 4th dimension
points(x,ii[,2], col="red")
# adding points in a different
# colour from the 4th dim.
})
})
非常感谢您的想法,我们非常感激!
祝好 凯特
apply
。请使用for
循环或重塑数据并使用 ggplot2 或 lattice。 - Roland