0

我的问题是关于转换“星”类对象。作为时空克里金法的结果,我有一个星星对象,如下所示:

stars object with 2 dimensions and 1 attribute
attribute(s):
               Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
var1.pred  11.64967 22.82501 25.30697 24.78288 26.75081 31.55394
dimension(s):
     from   to     offset  delta refsys point
sfc     1 1228         NA     NA     NA  TRUE
time    1    4 2020-06-01 8 days   Date    NA
                                                      values
sfc  POINT (745.5006 4847.393),...,POINT (579.0006 4995.893)
time                                                    NULL

更准确地说,我想做的是将 sfc 维度“拆分”为“x”和“y”坐标。我以一种非常复杂的方式成功地做到了这一点,包括(1)构建四个数据框(每次一个),(2)通过

st_dimensions(x = x, y = y, .raster = c("x", "y"))

(3) 使用代码将 4 星对象组装成列表 (4)

r = do.call("c", MMMlist) 
r = st_redimension(r)  
setNames(r,"var1.pred") -> r
r = st_set_dimensions(r, "new_dim", values = c(1,2,3,4), names = "time", point = TRUE)

用于重新组装具有三个维度的星星对象:

stars object with 3 dimensions and 1 attribute
attribute(s):
               Min.  1st Qu.   Median     Mean  3rd Qu.     Max. NA's
var1.pred  11.64967 22.82501 25.30697 24.78288 26.75081 31.55394 3792
dimension(s):
     from to  offset delta refsys point values x/y
x       1 64 516.001   4.5     NA FALSE   NULL [x]
y       1 34 4847.39   4.5     NA FALSE   NULL [y]
time    1  4       1     1     NA  TRUE   NULL  

我可以与 ggplot() 一起使用:

ggplot() + geom_stars(data = r) +
    coord_equal() +
    facet_wrap(~time) +
    theme_void() +
    scale_fill_steps(n.breaks=8,low="blue",high="orange") +
    #scale_fill_viridis_c(option = "magma",na.value="white") +
    scale_x_discrete(expand=c(0,0))+
    scale_y_discrete(expand=c(0,0))

这是非常复杂且不优雅的,我相信有一种更直接的方法可以从包含 (x,y) 坐标的 sfc_POINT 对象中获取二维“x”和“y”。

谢谢,罗伯托

4

0 回答 0