在自定义函数中使用xyplot,在R中使用面板函数
我正在尝试在在自定义函数中使用xyplot,在R中使用面板函数,r,function,lattice,R,Function,Lattice,我正在尝试在lattice图形包中生成一个自定义xyplot。这是一个由两条线组成的绘图,其中绘制了范围限制 # Raw dataframe in long format df <- data.frame( Response = c(runif(100), rnorm(100)), Trial = c(rep("A", 100), rep("C", 100)), Year = 1:10, Rep = rep(1:10, each = 10)) # Agg
lattice
图形包中生成一个自定义xyplot
。这是一个由两条线组成的绘图,其中绘制了范围限制
# Raw dataframe in long format
df <- data.frame(
Response = c(runif(100), rnorm(100)),
Trial = c(rep("A", 100), rep("C", 100)),
Year = 1:10,
Rep = rep(1:10, each = 10))
# Aggregate the data (take mean/min/max across "Rep" variable
gdf <- do.call(data.frame, aggregate(Response ~ Year + Trial, data = df,
FUN = function(x) c(avg = mean(x), mini = min(x), maxi = max(x))))
# Plot using xyplot (without making this a function)
my.panel.bands <- function(x, y, upper, lower, fill, col,
subscripts, ..., font, fontface){
upper <- upper[subscripts]
lower <- lower[subscripts]
panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
col = fill, alpha = 0.2, border = FALSE, ...)
}
f1 <- formula(Response.avg ~ Year)
p <- xyplot(x = f1, data = gdf, groups = Trial,
col=c("red", "blue"), pch = 16,
scales = list(x = list(rot = 45)),
xlab = 'Year', ylab = 'Response',
layout = c(1, 1),
ylim = c(min(gdf$Response.mini), max(gdf$Response.maxi)),
upper = gdf$Response.maxi,
lower = gdf$Response.mini,
panel = function(x, y, ...){
panel.superpose(x, y, panel.groups = my.panel.bands,
type = 'l', fill = c("red", "blue"),...)
panel.xyplot(x, y, type = 'b', cex = 0.6, lty = 1, ...)
}
)
png("panel_plot.png")
print(p)
dev.off()
最后,如果我将变量名作为字符串传递给group
参数,并修改panel\u plot()
函数以在data.frame中重新定义一个新变量,那么它将按预期工作,但这似乎是一种奇怪的方式
panel_plot <- function(f, df, grouper, xlabel, ylabel,
ylim, upper_border, lower_border, mfcol){
df$grouper <- df[, grouper]
p <- xyplot(x = f, data = df, groups = grouper,
...
p <- panel_plot(f1, gdf, grouper = "Trial",
...
实际上,请考虑将您使用的两个解决方案组合在一起:<代码>匹配>调用()/Calp>列表和<代码> EVAL(CALL(),…)< /代码>。单靠他们两个人都不管用
panel_plot <- function(f, df, grouper, xlabel, ylabel,
ylim, upper_border, lower_border, mfcol){
ll <- as.list(match.call(expand.dots = FALSE)[-1])
my_panel <- function(x, y, ...){
panel.superpose(x, y, panel.groups = my.panel.bands,
type = 'l', fill = c("red", "blue"),...)
panel.xyplot(x, y, type = 'b', cex = 0.6, lty = 1, ...)
}
p <- eval(call("xyplot",
x = ll$f,
data = ll$df,
groups = ll$grouper,
xlab = ll$xlabel, ylab = ll$ylabel,
ylim = ll$ylim,
layout = ll$mfcol,
upper = ll$upper_border,
lower = ll$lower_border,
panel = my_panel
)
)
return(p)
}
f1 <- formula(Response.avg ~ Year)
p <- panel_plot(f1, gdf, grouper = Trial,
xlabel = "Year", ylabel = "Response",
ylim = c(min(gdf$Response.mini), max(gdf$Response.maxi)),
upper_border = gdf$Response.maxi,
lower_border = gdf$Response.mini,
mfcol = c(1, 1))
p
panel_plot <- function(f, df, grouper, xlabel, ylabel,
ylim, upper_border, lower_border, mfcol){
ll <- as.list(match.call(expand.dots = FALSE)[-1])
my_panel <- function(x, y, ...){
panel.superpose(x, y, panel.groups = my.panel.bands,
type = 'l', fill = c("red", "blue"),...)
panel.xyplot(x, y, type = 'b', cex = 0.6, lty = 1, ...)
}
p <- eval(call("xyplot",
x = ll$f,
data = ll$df,
groups = ll$grouper,
xlab = ll$xlabel, ylab = ll$ylabel,
ylim = ll$ylim,
layout = ll$mfcol,
upper = ll$upper_border,
lower = ll$lower_border,
panel = my_panel
)
)
return(p)
}
f1 <- formula(Response.avg ~ Year)
p <- panel_plot(f1, gdf, grouper = Trial,
xlabel = "Year", ylabel = "Response",
ylim = c(min(gdf$Response.mini), max(gdf$Response.maxi)),
upper_border = gdf$Response.maxi,
lower_border = gdf$Response.mini,
mfcol = c(1, 1))
p
panel_plot <- function(f, df, grouper, xlabel, ylabel,
ylim, upper_border, lower_border, mfcol){
df$grouper <- eval(as.name(deparse(substitute(grouper))), df, .GlobalEnv)
p <- xyplot(x = f, data = df, groups = grouper,
...
}