简体   繁体   English

如何在 R 中的 spplot 中操作图

[英]How to manipulate plots in spplot in R

I am trying to use spplot to visualize plots from different months.我正在尝试使用 spplot 来可视化不同月份的图。 I'd like to change this figure so the same months are in the same columns to easily compare.我想更改这个数字,以便相同的月份在相同的列中以便于比较。 I would like to push May 2016 5 panels in, so all the rest of the months are in line.我想将 2016 年 5 月的 5 个面板推入,因此所有其余月份都符合要求。 I hope this makes sense.我希望这是有道理的。 click here for figure单击此处查看图

I have missing data for Dec2017 for now which is why it's blacked out.我现在缺少 2017 年 12 月的数据,这就是它被涂黑的原因。

Here is my code:这是我的代码:

stack_months <- stack(May2016, June2016, July2016, Aug2016, Sep2016, Oct2016, Nov2016, Dec2016, January2017, Febuary2017, March2017, April2017, May2017, June2017, July2017, July2017, Aug2017, Sep2017, Oct2017, Nov2017, Dec2017, January2018, Febuary2018, March2017, April2018, May2018, June2017, July2017, July2018, Aug2018, Sep2018, Oct2018, Nov2018, Dec2018, January2019, Febuary2019, March2019, April2019, May2019, June2019, July2019, July2019)
spplot(stack_months, col.regions=viridis(20), names.attr = c("May2016", "June2016", "July2016", "Aug2016", "Sep2016", "Oct2016", "Nov2016", "Dec2016", 
                                                             "Jan2017", "Feb2017", "March2017", "April2017", "May2017", "June2017", "July2017", "July2017", "Aug2017", "Sep2017", "Oct2017", "Nov2017", "Dec2017",
                                                             "Jan2018", "Feb2018", "March2017", "April2018", "May2018", "June2017", "July2017", "July2018", "Aug2018", "Sep2018", "Oct2018", "Nov2018", "Dec2018", 
                                                             "Jan2019", "Feb2019", "March2019", "April2019", "May2019", "June2019", "July2019", "July2019"), layout = c(12,4))

Is there an easy way to manipulate the panels?有没有简单的方法来操作面板?

Note that you have type some of the months twice, for example July2017 appeared 3 times and March2017, 2 times, June2017 2x and July2019 2x.请注意,您输入了某些月份两次,例如,July2017 出现了 3 次,March2017 出现了 2 次,June2017 2x 和 July2019 2x。

What I have below are complete months from May2016 to July2019, so that when you plot, the months will align.我下面的内容是从 May2016 到 July2019 的完整月份,这样当你绘图时,月份就会对齐。

library(raster)
library(sp)
library(viridis)
library(lattice)

months=c("May2016", "June2016", "July2016", "Aug2016", "Sep2016", "Oct2016", 
"Nov2016", "Dec2016", "Jan2017", "Feb2017", "March2017", "April2017", 
"May2017", "June2017", "July2017","Aug2017", "Sep2017", 
"Oct2017", "Nov2017", "Dec2017","Jan2018", "Feb2018", "March2018", 
"April2018", "May2018", "Jun2018", "July2018", "Aug2018", 
"Sep2018", "Oct2018", "Nov2018", "Dec2018","Jan2019", "Feb2019", 
"March2019", "April2019", "May2019", "June2019", "July2019")

I don't have your data, so I simulate something for the image:我没有你的数据,所以我为图像模拟了一些东西:

r <- raster(system.file("external/test.grd", package="raster"))
stack_months = do.call(stack,lapply(months,function(i)runif(1)*r))

You defined layout to be 12,4 so you will have 48 entries which are filled by row.您将布局定义为 12,4,因此您将有 48 个按行填充的条目。 In your case, the first 4 will not be plotted and the last 5 will not be plotted:在您的情况下,不会绘制前 4 个,也不会绘制后 5 个:

SKIP = rep(FALSE,12*4)
SKIP[1:4] = TRUE
SKIP[44:48] = TRUE

Then we plot using the SKIP above:然后我们使用上面的 SKIP 绘图:

spplot(stack_months, col.regions=viridis(20),
layout = c(12,4),
strip = strip.custom(par.strip.text = list(cex = 0.65)),
names.attr = months,
skip=SKIP
)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM