R语言 ggplot:“最小值/最大值无非缺失参数;返回Inf”

ltskdhd1  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(534)

我试图重现这个情节(我的版本:lat/lon by year),但在运行ggplot代码后不断收到以下警告:

> warnings()
Warning messages:
1: `stat_contour()`: Zero contours were generated
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
...
43: `stat_contour()`: Zero contours were generated
44: In min(x) : no non-missing arguments to min; returning Inf
45: In max(x) : no non-missing arguments to max; returning -Inf
46: Raster pixels are placed at uneven horizontal intervals and will be shifted
ℹ Consider using `geom_tile()` instead.

本以为this solution会删除其中一个警告,但它仍然存在...

> dput(sms2[1:5,])
structure(list(smooth = c("ti(CYR,Longitude,Latitude)", "ti(CYR,Longitude,Latitude)", 
"ti(CYR,Longitude,Latitude)", "ti(CYR,Longitude,Latitude)", "ti(CYR,Longitude,Latitude)"
), type = c("Tensor product int.", "Tensor product int.", "Tensor product int.", 
"Tensor product int.", "Tensor product int."), by = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    est = c(0.117025782747128, 0.113479508095921, 0.0933787695695134, 
    0.0895995641580288, 0.0755531029189105), se = c(0.185913143579915, 
    0.181005541009251, 0.152005779645215, 0.142402142342131, 
    0.119622723237741), CYR = c(2008L, 2008L, 2008L, 2008L, 2008L
    ), Longitude = c(-80.3026, -80.3034, -80.3075, -80.3076, 
    -80.31), Latitude = c(25.5694, 25.5684, 25.5618, 25.558, 
    25.5519)), row.names = c(NA, -5L), class = c("smooth_estimates", 
"tbl_df", "tbl", "data.frame"), tensor_term_order = list(`ti(CYR,Longitude,Latitude)` = c("Longitude", 
"Latitude", "CYR")))

est_lim <- c(-1, 1) * max(abs(sms2[["est"]]), na.rm = TRUE)

sms2 |> 
  mutate(fCYR = factor(CYR)) |>
  ggplot(aes(x = Longitude, y = Latitude, fill = est, group = fCYR)) + 
  geom_raster(aes(x = Longitude, y = Latitude, fill = est, group = fCYR)) +
  geom_contour(aes(z = est, group = fCYR, fill = NULL), colour = "black") +
  facet_wrap(~ fCYR) +
  scale_fill_distiller(palette = "RdBu", type = "div") +
  expand_limits(fill = est_lim)

结果图

uurv41yg

uurv41yg1#

首先......似乎需要插值来创建每个站点的完整网格:https://community.rstudio.com/t/r-warning-stat-contour-zero-contours-were-generated/111020/2
这里有一个简单的开始:

a <- sms2$Latitude
b <- sms2$Longitude
d <- expand.grid(a,b)
d$z <- rep(sms2$est)
d
ggplot(data = d, aes(Var1, Var2, z = z)) + geom_contour_filled()

相关问题