diff --git a/R/pivot-long-SpatVector.R b/R/pivot-long-SpatVector.R index 6646516f..76184e3f 100644 --- a/R/pivot-long-SpatVector.R +++ b/R/pivot-long-SpatVector.R @@ -36,31 +36,35 @@ #' have always the geometry of `data`. #' #' @examples +#' \donttest{ +#' #' library(dplyr) #' library(tidyr) #' library(ggplot2) +#' library(terra) #' -#' nc <- terra::vect(system.file("shape/nc.shp", package = "sf")) -#' -#' # Usually this is useful for faceting, see BIR -#' glimpse(nc) +#' temp <- rast((system.file("extdata/cyl_temp.tif", package = "tidyterra"))) +#' cyl <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) %>% +#' project(temp) #' -#' nc_pivoted <- nc %>% -#' pivot_longer(starts_with("BIR"), -#' names_to = "year", -#' values_to = "births" -#' ) %>% -#' mutate(year = as.numeric(gsub("BIR", "19", year))) +#' # Add average temp #' -#' nc_pivoted %>% -#' relocate(year, births) %>% +#' temps <- terra::extract(temp, cyl, fun = "mean", na.rm = TRUE, xy = TRUE) +#' cyl_temp <- cbind(cyl, temps) %>% #' glimpse() #' -#' ggplot(nc_pivoted) + -#' geom_spatvector(aes(fill = births)) + -#' facet_wrap(~year, ncol = 1) + -#' scale_fill_viridis_c(option = "cividis") + -#' labs(title = "Number of births in South Carolina") +#' # And pivot long for plot +#' cyl_temp %>% +#' pivot_longer( +#' cols = tavg_04:tavg_06, +#' names_to = "label", +#' values_to = "temp" +#' ) %>% +#' ggplot() + +#' geom_spatvector(aes(fill = temp)) + +#' facet_wrap(~label, ncol = 1) + +#' scale_fill_whitebox_c(palette = "muted") +#' } pivot_longer.SpatVector <- function(data, cols, ..., cols_vary = "fastest", names_to = "name", names_prefix = NULL, names_sep = NULL, names_pattern = NULL, diff --git a/R/pivot-wide-SpatVector.R b/R/pivot-wide-SpatVector.R index fde08d6c..30160ebb 100644 --- a/R/pivot-wide-SpatVector.R +++ b/R/pivot-wide-SpatVector.R @@ -46,26 +46,30 @@ #' have always the geometry of `data`. #' #' @examples -#' +#' \donttest{ #' library(dplyr) #' library(tidyr) #' library(ggplot2) #' -#' nc <- terra::vect(system.file("shape/nc.shp", package = "sf")) -#' -#' names(nc) -#' # Pivot longer -#' nc_long <- nc %>% -#' pivot_longer(contains("BIR"), names_to = "label", values_to = "n") %>% -#' select(label:n) %>% -#' glimpse() -#' +#' cyl <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) #' -#' # Undo -#' nc_long %>% -#' pivot_wider(names_from = label, values_from = n) %>% +#' # Add extra row with info +#' xtra <- cyl %>% +#' slice(c(2, 3)) %>% +#' mutate( +#' label = "extra", +#' value = TRUE +#' ) %>% +#' rbind(cyl, .) %>% #' glimpse() #' +#' # Pivot by geom +#' xtra %>% +#' pivot_wider( +#' id_cols = iso2:name, values_from = value, +#' names_from = label +#' ) +#' } pivot_wider.SpatVector <- function(data, ..., id_cols = NULL, diff --git a/codemeta.json b/codemeta.json index d0150d01..84d3dc1d 100644 --- a/codemeta.json +++ b/codemeta.json @@ -344,7 +344,7 @@ "SystemRequirements": null }, "keywords": ["r", "terra", "ggplot-extension", "r-spatial", "rspatial", "r-package", "rstats", "rstats-package", "cran", "cran-r"], - "fileSize": "2289.359KB", + "fileSize": "2289.634KB", "citation": [ { "@type": "ScholarlyArticle", diff --git a/data-raw/holyroodpark.R b/data-raw/holyroodpark.R index df4aa15d..aa85a119 100644 --- a/data-raw/holyroodpark.R +++ b/data-raw/holyroodpark.R @@ -34,5 +34,5 @@ ncell(xtraclip) unlink("data-raw/holyroodpark.tif") unlink("vignettes/articles/holyroodpark.tif") -writeRaster(final_rast, "data-raw/holyroodpark.tif") -writeRaster(final_rast, "vignettes/articles/holyroodpark.tif") +writeRaster(xtraclip, "data-raw/holyroodpark.tif") +writeRaster(xtraclip, "vignettes/articles/holyroodpark.tif") diff --git a/data-raw/holyroodpark.tif b/data-raw/holyroodpark.tif index 78f37af6..74c69763 100644 Binary files a/data-raw/holyroodpark.tif and b/data-raw/holyroodpark.tif differ diff --git a/man/pivot_longer.SpatVector.Rd b/man/pivot_longer.SpatVector.Rd index 0adc9db6..abba969c 100644 --- a/man/pivot_longer.SpatVector.Rd +++ b/man/pivot_longer.SpatVector.Rd @@ -136,31 +136,35 @@ have always the geometry of \code{data}. } \examples{ +\donttest{ + library(dplyr) library(tidyr) library(ggplot2) +library(terra) -nc <- terra::vect(system.file("shape/nc.shp", package = "sf")) - -# Usually this is useful for faceting, see BIR -glimpse(nc) +temp <- rast((system.file("extdata/cyl_temp.tif", package = "tidyterra"))) +cyl <- vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) \%>\% + project(temp) -nc_pivoted <- nc \%>\% - pivot_longer(starts_with("BIR"), - names_to = "year", - values_to = "births" - ) \%>\% - mutate(year = as.numeric(gsub("BIR", "19", year))) +# Add average temp -nc_pivoted \%>\% - relocate(year, births) \%>\% +temps <- terra::extract(temp, cyl, fun = "mean", na.rm = TRUE, xy = TRUE) +cyl_temp <- cbind(cyl, temps) \%>\% glimpse() -ggplot(nc_pivoted) + - geom_spatvector(aes(fill = births)) + - facet_wrap(~year, ncol = 1) + - scale_fill_viridis_c(option = "cividis") + - labs(title = "Number of births in South Carolina") +# And pivot long for plot +cyl_temp \%>\% + pivot_longer( + cols = tavg_04:tavg_06, + names_to = "label", + values_to = "temp" + ) \%>\% + ggplot() + + geom_spatvector(aes(fill = temp)) + + facet_wrap(~label, ncol = 1) + + scale_fill_whitebox_c(palette = "muted") +} } \seealso{ \code{\link[tidyr:pivot_longer]{tidyr::pivot_longer()}} diff --git a/man/pivot_wider.SpatVector.Rd b/man/pivot_wider.SpatVector.Rd index 5300c51f..0f8da5e7 100644 --- a/man/pivot_wider.SpatVector.Rd +++ b/man/pivot_wider.SpatVector.Rd @@ -143,26 +143,30 @@ have always the geometry of \code{data}. } \examples{ - +\donttest{ library(dplyr) library(tidyr) library(ggplot2) -nc <- terra::vect(system.file("shape/nc.shp", package = "sf")) +cyl <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra")) -names(nc) -# Pivot longer -nc_long <- nc \%>\% - pivot_longer(contains("BIR"), names_to = "label", values_to = "n") \%>\% - select(label:n) \%>\% - glimpse() - - -# Undo -nc_long \%>\% - pivot_wider(names_from = label, values_from = n) \%>\% +# Add extra row with info +xtra <- cyl \%>\% + slice(c(2, 3)) \%>\% + mutate( + label = "extra", + value = TRUE + ) \%>\% + rbind(cyl, .) \%>\% glimpse() +# Pivot by geom +xtra \%>\% + pivot_wider( + id_cols = iso2:name, values_from = value, + names_from = label + ) +} } \seealso{ \code{\link[tidyr:pivot_wider]{tidyr::pivot_wider()}} diff --git a/vignettes/articles/faqs.Rmd b/vignettes/articles/faqs.Rmd index c39e5269..3ce1f0b0 100644 --- a/vignettes/articles/faqs.Rmd +++ b/vignettes/articles/faqs.Rmd @@ -299,7 +299,7 @@ blurriness on your data. Also, modify the parameter `maxcell` to avoid resampling and force the **ggplot2** map to be on **EPSG:3857** with `ggplot2::coord_sf(crs = 3857)`: -```{r blurry-tile} +```{r blurry-tile, fig.show='hold'} library(terra) library(tidyterra) library(ggplot2) diff --git a/vignettes/articles/holyroodpark.tif b/vignettes/articles/holyroodpark.tif index 78f37af6..74c69763 100644 Binary files a/vignettes/articles/holyroodpark.tif and b/vignettes/articles/holyroodpark.tif differ