Skip to content

Commit

Permalink
updating sf vignette for noSuggets
Browse files Browse the repository at this point in the history
  • Loading branch information
JoFrhwld committed Sep 29, 2023
1 parent 43b6ba1 commit c81ee87
Showing 1 changed file with 51 additions and 15 deletions.
66 changes: 51 additions & 15 deletions vignettes/sf-operations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@ knitr::opts_chunk$set(
Because `{densityarea}` can return `{sf}` polygons, this can allow you to use its functionality (see [this cheat sheet](https://github.com/rstudio/cheatsheets/blob/main/sf.pdf) for some examples).

```{r setup}
# package dependencies
library(densityarea)
library(dplyr)
library(tidyr)
library(purrr)
library(stringr)
library(sf)
```

```{r setup2, eval=FALSE}
# package suggests
library(tidyr)
library(stringr)
library(ggplot2)
```

```{r include=F}
tidyr_inst <- require(tidyr)
stringr_inst <- require(stringr)
ggplot2_inst <- require(ggplot2)
all_suggest <- all(c(tidyr_inst, stringr_inst, ggplot2_inst))
```

## Density polygons as simple features

As a first example, we'll estimate how much different data clusters overlap in the `s01` dataset.
Expand Down Expand Up @@ -83,7 +94,7 @@ vowel_polygons

We can plot these directly by using the `sf::geom_sf()` geom for ggplot2.

```{r fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
```{r, eval = ggplot2_inst, fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
ggplot(vowel_polygons) +
geom_sf(
aes(fill = plt_vclass),
Expand All @@ -104,7 +115,7 @@ vowel_polygons |>
vowel_polygons
```

```{r fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
```{r, eval=ggplot2_inst, fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
ggplot(vowel_polygons) +
geom_sf(
aes(fill = area),
Expand All @@ -115,7 +126,7 @@ ggplot(vowel_polygons) +

Or, we can get the polygon centroids and plot them.

```{r fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
```{r, eval=ggplot2_inst, fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
vowel_polygons |>
st_centroid() |>
ggplot()+
Expand All @@ -142,7 +153,7 @@ vowel_intersections

This data frame contains a polygon for each unique intersection of the input polygons, with a new `n.overlaps` column.

```{r fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
```{r, eval=ggplot2_inst, fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
ggplot(vowel_intersections) +
geom_sf(
aes(fill = n.overlaps)
Expand All @@ -152,13 +163,19 @@ ggplot(vowel_intersections) +

The labels of the new overlapping regions aren't very informative, but we can create some new labels by using the indices in the `origins` column.

```{r}
```{r, eval=stringr_inst, include=stringr_inst}
new_label <- function(indices, labels){
str_c(labels[indices],
collapse = "~")
}
```

```{r, eval=!stringr_inst, include=!stringr_inst}
new_label <- function(indicies, labels){
paste0(labels[indicies], collapse = "~")
}
```

```{r}
vowel_intersections |>
mutate(
Expand All @@ -174,7 +191,7 @@ vowel_intersections |>
vowel_intersections
```

```{r fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
```{r eval = ggplot2_inst, fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
ggplot(vowel_intersections)+
geom_sf(
aes(fill = groups)
Expand All @@ -184,7 +201,7 @@ ggplot(vowel_intersections)+

We can also calculate the areas of these new polygons, and compare them to the original areas (which have been preserved in `areas`.

```{r fig.width=5, fig.height=3, out.width="80%", fig.align='center'}
```{r eval=ggplot2_inst, fig.width=5, fig.height=3, out.width="80%", fig.align='center'}
vowel_intersections |>
mutate(
group_area = st_area(geometry),
Expand All @@ -204,9 +221,16 @@ There are also a number of spatial filters and merges that can be used interesti

```{r}
library(sfheaders)
```

```{r, eval=F}
library(forcats)
```

```{r, include=F}
forcats_inst <- require(forcats)
```

```{r}
s01 |>
sfheaders::sf_point(
Expand All @@ -219,7 +243,7 @@ s01 |>
s01_sf
```

```{r fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
```{r eval=ggplot2_inst, fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
s01_sf |>
ggplot()+
geom_sf()
Expand Down Expand Up @@ -250,7 +274,7 @@ s01_sf |>
covered_by_iy
```

```{r fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
```{r eval=ggplot2_inst, fig.width=5, fig.height=5, out.width="80%", fig.align='center'}
covered_by_iy |>
mutate(plt_vclass = plt_vclass |>
fct_infreq() |>
Expand All @@ -263,7 +287,7 @@ covered_by_iy |>

Obviously, the 80% probability density area for `iy` is not a homogeneous region.

```{r fig.width=5, fig.height=3, out.width="80%", fig.align='center'}
```{r eval=ggplot2_inst, fig.width=5, fig.height=3, out.width="80%", fig.align='center'}
covered_by_iy |>
mutate(plt_vclass = plt_vclass |>
fct_infreq() |>
Expand Down Expand Up @@ -309,7 +333,7 @@ s01 |>

There are 5 probability level polygons for each vowel category in `vowel_probs`. We join the random vowel's data onto this set of polygons with `st_join()`.

```{r}
```{r, eval=tidyr_inst, include=tidyr_inst}
vowel_probs |>
st_join(
rand_vowel,
Expand All @@ -319,6 +343,18 @@ vowel_probs |>
vowel_within
```

```{r, eval=!tidyr_inst, include=!tidyr_inst}
vowel_probs |>
st_join(
rand_vowel,
.predicate = st_covers
) |>
filter(
!is.na(plt_vclass.y)
) ->
vowel_within
```

Now, for each vowel category in this new data frame, let's get the *smallest* probability polygon (i.e. where the random point is closest to the center probability mass).

```{r}
Expand All @@ -330,7 +366,7 @@ vowel_within |>
vowel_min_prob
```

```{r fig.width=5, fig.height = 5, out.width="80%"}
```{r eval = ggplot2_inst, fig.width=5, fig.height = 5, out.width="80%"}
vowel_min_prob |>
ggplot()+
geom_sf(aes(fill = prob)) +
Expand Down

0 comments on commit c81ee87

Please sign in to comment.