For a better version of the stars vignettes see https://r-spatial.github.io/stars/articles/
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, dev = "png") ev = TRUE knitr::opts_chunk$set(fig.height = 4.5) knitr::opts_chunk$set(fig.width = 6)
This vignette shows how some of the tidyverse verbs can be used on
stars objects.
The stars and tidyverse packages are loaded by
library(stars) library(dplyr)
Methods now available for class stars are
methods(class = "stars")
We will work with a three-band section of a landsat image:
system.file("tif/L7_ETMs.tif", package = "stars") %>% read_stars -> x x
sliceslice slices a sub-array out of the cube; this is done by specifying
the dimension on which to act, and the slice number.
x %>% slice(band, 6) -> x6 x6
It returns a lower-dimensional array if a single element is selected along the slice dimension.
filterSimilar to slice, filter selects on dimensions but evaluates their values
rather than their index: in
x %>% filter(x > 289000, x < 291000, band > 3) -> x7 x7
the subarray is created based on the x coordinate values.
Note that filter converts the object to a tbl_cube, and uses
the dplyr filter method for tbl_cube objects. This has the
limitation that stars objects with rectilinear, curvilinear or
simple feature geometries cannot be handled. For such objects, using
regular [ selection or using st_crop may be an alternative.
pullpull pulls out an array from a stars object:
x %>% pull(1) -> x8 class(x8) dim(x8)
mutatex %>% mutate(band2 = 2 * L7_ETMs.tif) -> x2 x2
selectselect selects an attribute, or a set of attributes:
x2 %>% select(band2) -> x9 x9
geom_starsgeom_raster is a ggplot2 geom function that accepts stars
objects as its data argument and
geom_raster, geom_rect and geom_sf depending on
whether the geometry is regular, rectilinear or has vector geometriesAn example use is
library(ggplot2) library(viridis) ggplot() + geom_stars(data = x) + coord_equal() + facet_wrap(~band) + theme_void() + scale_fill_viridis() + scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.