2

我想使用 lattice 包生成等高线图,其中 y 轴从顶部开始并到达底部(即顶部的最小值,底部的最大值)。如果我更改以下内容:

contourplot(Male ~ Age * Year, data=this.ds)

contourplot(Male ~ Age * rev(Year), data=this.ds)

然后图形被正确绘制,但 y 轴刻度标签没有反转。(即仍然从底部开始并到顶部。) - (有关完全可重现的示例,请参阅消息末尾。)

我认为答案涉及使用“比例”列表对象(所以我认为这是一个单行解决方案),但不确定它是什么。

非常感谢您的帮助,乔恩

完全可重现的示例:

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)

4

2 回答 2

2

更简洁的解决方案是反转指定 y 轴限制的长度为 2 的向量的顺序。设置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。我之所以这样做,是因为我想直接比较原始图和修改后的图。)

在此处输入图像描述

于 2016-06-12T17:39:00.913 回答
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

于 2013-09-07T19:19:07.363 回答