1

I'm looking to recreate this sort of visualization How to create contour with wind animation using gganimate? with a time series wind speed data set, but I am encountering this error and can't figure out what's causing it:

Warning message: Computation failed in stat_contour_fill(): Supplied 2 columns to be assigned an empty list (which may be an empty data.table or data.frame since they are lists too). To delete multiple columns use NULL instead. To add multiple empty list columns, use list(list()).

The code:

pacman::p_load(ggplot2, tidyr, dplyr, raster,rgdal,sf,tidyverse,sp, gganimate, maptools, plyr, transformr, readxl, ggmap)


scale.breaks = scales::fullseq(range(test$speed), size = 10)

tim <- get_stamenmap(bbox = c(left = -105.2545,
                              bottom = 35.58840,
                              right = -105.6946,
                              top = 36.19384),
                     maptype = "terrain", 
                     crop = FALSE,
                     zoom = 11)


map.annotation <- list(
  annotation_raster(tim %>% unlist() %>%
                      alpha(0.4) %>% # change alpha setting for map here
                      matrix(nrow = dim(tim)[1], 
                             byrow = TRUE),
                    xmin = attr(tim, "bb")$ll.lon,
                    xmax = attr(tim, "bb")$ur.lon,
                    ymin = attr(tim, "bb")$ll.lat,
                    ymax = attr(tim, "bb")$ur.lat),
  coord_quickmap(xlim = c(attr(tim, "bb")$ll.lon, attr(tim, "bb")$ur.lon),
                 ylim = c(attr(tim, "bb")$ll.lat, attr(tim, "bb")$ur.lat),
                 expand = FALSE))

p.base <- ggplot(test, aes(x = longitude, y = latitude, z = speed))

p.base + 
  geom_contour_fill(breaks = scale.breaks) +
  facet_wrap(~DateTime) +
  map.annotation +
  scale_fill_gradient(low = "green", high = "red",
                      aesthetics = c("colour", "fill"),
                      limits = range(scale.breaks)) +
  theme_minimal()

data:

tibble::tribble(
  ~speed, ~dir, ~longitude,     ~latitude,     ~DateTime,
  8.6,    269,  -105.265514515, 35.4631886028, as.POSIXct("2022-04-01 20:11:00"),
  8.3,    271,  -105.25959775,  35.4632014724, as.POSIXct("2022-04-01 20:11:00"),
  8.5,    273,  -105.253680981, 35.463214052,  as.POSIXct("2022-04-01 20:11:00"),
  8.9,    272,  -105.247764209, 35.4632263417, as.POSIXct("2022-04-01 20:11:00"),
  9.3,    266,  -105.241847433, 35.4632383413, as.POSIXct("2022-04-01 20:11:00"),
  9.1,    259,  -105.235930653, 35.4632500508, as.POSIXct("2022-04-01 20:11:00"),
  7.9,    260,  -105.230013871, 35.4632614704, as.POSIXct("2022-04-01 20:11:00"),
  7.7,    271,  -105.224097085, 35.4632725999, as.POSIXct("2022-04-01 20:11:00"),
  8.4,    278,  -105.218180295, 35.4632834394, as.POSIXct("2022-04-01 20:11:00"),
  9,      276,  -105.212263503, 35.4632939889, as.POSIXct("2022-04-01 20:11:00"),
)
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
ZArmstrong
  • 67
  • 5
  • Please include all the `library()` calls necessary to run your sample code. – MrFlick Apr 24 '23 at 14:54
  • @MrFlick added library list at top of code snippet – ZArmstrong Apr 24 '23 at 15:19
  • 1
    you do not have package `metR` in your code, so modify it `pacman::p_load(ggplot2, tidyr, dplyr, metR, raster,rgdal,sf,tidyverse,sp, gganimate, maptools, plyr, transformr, readxl, ggmap)` – Manoj Kumar Apr 24 '23 at 15:51
  • @ManojKumar I added the MetR package and did an explicit call metR::geom_contour_fill and still getting the error – ZArmstrong Apr 24 '23 at 16:17
  • 1
    I don't want to take the time to experiment, but the first thing I would try is to use a larger test data set, and use breaks within the range of your data. – Ben Bolker Apr 24 '23 at 16:26
  • Please note that `geom_contour_filled` has a requirement: `x` and `y` must conform an equally spaced grid (see https://ggplot2.tidyverse.org/reference/geom_contour.html) while your test data conforms a straight line. (see with `geom_point()`). That's why you can't create your plot with this approach – dieghernan Jun 01 '23 at 10:27

0 Answers0