【问题标题】:How to reverse the Y axis in a contourplot using the Lattice package如何使用 Lattice 包在等高线图中反转 Y 轴
【发布时间】:2013-09-07 17:54:26
【问题描述】:

我想使用 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)

【问题讨论】:

  • 请阅读thisthis
  • 谢谢。我添加了一个完全可重现的示例,试图让这一点更清楚。
  • 感谢一个很好的例子!一件小事,在这两个命令之间插入一个换行符:grid &lt;- expand.grid(wtr.marginal) grid[, "fit"] &lt;- c(predict(ozo.m, grid))(我不被允许做这个编辑)。然后,任何想要帮助您的人都可以很容易地复制粘贴您的代码并使用它。谢谢!

标签: r lattice


【解决方案1】:

更简洁的解决方案是反转指定 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 的原始调用中提供它。我只是这样做,因为我想直接比较原始图和修改后的图。)

【讨论】:

    【解决方案2】:

    您可以尝试在scales 参数中反转y 轴的labels

    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

    【讨论】:

    • 谢谢。我最终做了几乎相同的事情,尽管使用了更新命令。感觉就像一个肮脏的黑客,但现在效果很好。
    猜你喜欢
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多