我想使用晶格封装生成一个等值线图,其中y轴从顶部开始并向下移动(即顶部的最小值,底部的最大值)。 如果我更改以下内容:
contourplot(Male ~ Age * Year, data=this.ds)
到
contourplot(Male ~ Age * rev(Year), data=this.ds)
然后正确绘制图形,但y轴刻度标记不反转。 (即仍然从底部开始并转到顶部。) - (参见完整可重复示例的消息末尾。)
我认为答案涉及使用'scale'列表对象(所以我认为这有一个单行解决方案)但我不确定它是什么。
非常感谢,Jon
完全可重复的例子:
library(lattice)
attach(environmental)
ozo.m <- loess((ozone^(1/3)) ~ wind * temperature * radiation,
parametric = c("radiation", "wind"), span = 1, degree = 2)
w.marginal <- seq(min(wind), max(wind), length.out = 50)
t.marginal <- seq(min(temperature), max(temperature), length.out = 50)
r.marginal <- seq(min(radiation), max(radiation), length.out = 4)
wtr.marginal <- list(wind = w.marginal, temperature = t.marginal,
radiation = r.marginal)
grid <- expand.grid(wtr.marginal)
grid[, "fit"] <- c(predict(ozo.m, grid))
照常绘图:
contourplot(fit ~ wind * temperature, data = grid,
cuts = 10, region = TRUE,
xlab = "Wind Speed (mph)",
ylab = "Temperature (F)",
main = "Cube Root Ozone (cube root ppb)")
对温度使用rev()函数:
contourplot(fit ~ wind * rev(temperature), data = grid,
cuts = 10, region = TRUE,
xlab = "Wind Speed (mph)",
ylab = "Temperature (F)",
main = "Cube Root Ozone (cube root ppb)")
detach()
我想这样,y(温度)轴标签反转以及y轴值。 (即它读取90,80,70,60从下到上而不是60,70,80,90)
答案 0 :(得分:1)
您可以尝试在labels
参数中反转y轴的scales
。
library(lattice)
contourplot(fit ~ wind * rev(temperature), data = grid,
cuts = 10, region = TRUE,
xlab = "Wind Speed (mph)",
ylab = "Temperature (F)",
main = "Cube Root Ozone (cube root ppb)",
scales = list(y = list(
labels = seq(from = 100, to = 60, by = -10))))
说实话,我认为seq(from = 90, to = 60, by = -10)
应该有用,但它会给标签80, 70, 60, NA
。
答案 1 :(得分:1)
更清洁的解决方案是颠倒指定y轴限制的长度为二的向量的顺序。设置ylim=rev(range(temperature))
的效果非常好,但是对于更精确匹配原始布局的绘图,您可能希望使用extendrange()
来绘制更大的范围。
## OP's example figure
a <- contourplot(fit ~ wind * temperature, data = grid,
cuts = 10, region = TRUE,
xlab = "Wind Speed (mph)",
ylab = "Temperature (F)",
main = "Cube Root Ozone (cube root ppb)")
## Same figure with the y-axis reversed
b <- update(a, ylim = rev(extendrange(temperature, f=0.01)))
## Plot figs side-by-side to check that that worked
library(gridExtra)
grid.arrange(a, b, ncol=2)
(虽然我在上面使用了update()
来添加修改后的ylim
,但也可以在contourplot
的原始调用中提供它。我只是这样做了我想直接比较原始图和修改图。)