geocompx / geocompr

Geocomputation with R: an open source book
https://r.geocompx.org/
Other
1.55k stars 582 forks source link

Ch. 5 Exercise #4 faulty solutions #1083

Closed smkerr closed 4 months ago

smkerr commented 7 months ago

Section 5.4 Exercise E4: The following code provided in 05-ex.Rmd produces faulty solutions:

library(sf)
library(terra)
library(dplyr)
library(spData)
library(spDataLarge)

world_sfc = st_geometry(world)
world_sfc_mirror = world_sfc * c(1, -1)
plot(world_sfc)
plot(world_sfc_mirror)

_05-ex_world_sfc_mirror

us_states_sfc = st_geometry(us_states)
us_states_sfc_mirror = us_states_sfc * c(1, -1)
plot(us_states_sfc)
plot(us_states_sfc_mirror)

_05-ex_us_states_sfc_mirror

## nicer plot
library(ggplot2)
library(ggrepel)
us_states_sfc_mirror_labels = st_centroid(us_states_sfc_mirror) |>
  st_coordinates() |>
  as_data_frame() |>
  mutate(name = us_states$NAME)
us_states_sfc_mirror_sf = st_set_geometry(us_states, us_states_sfc_mirror)
ggplot(data = us_states_sfc_mirror_sf) +
  geom_sf(color = "white") +
  geom_text_repel(data = us_states_sfc_mirror_labels, mapping = aes(X, Y, label = name), size = 3, min.segment.length = 0) +
   theme_void() 

_05-ex_us_states_sfc_mirror_labels

Here is my session info:

R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.2.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggrepel_0.9.4     ggplot2_3.4.4     spDataLarge_2.1.1 spData_2.3.0     
[5] dplyr_1.1.4       terra_1.7-71      sf_1.0-15        

loaded via a namespace (and not attached):
 [1] crayon_1.5.2       vctrs_0.6.5        cli_3.6.2          rlang_1.1.3       
 [5] DBI_1.2.2          KernSmooth_2.23-22 generics_0.1.3     glue_1.7.0        
 [9] colorspace_2.1-0   e1071_1.7-14       sp_2.1-3           scales_1.3.0      
[13] fansi_1.0.6        grid_4.3.2         munsell_0.5.0      classInt_0.4-10   
[17] tibble_3.2.1       lifecycle_1.0.4    compiler_4.3.2     codetools_0.2-19  
[21] Rcpp_1.0.12        pkgconfig_2.0.3    rstudioapi_0.15.0  farver_2.1.1      
[25] lattice_0.21-9     R6_2.5.1           class_7.3-22       tidyselect_1.2.0  
[29] utf8_1.2.4         pillar_1.9.0       magrittr_2.0.3     withr_3.0.0       
[33] gtable_0.3.4       tools_4.3.2        proxy_0.4-27       units_0.8-5       

I was able to implement a solution using the tmap package:

library(tmap)

world_sfc = st_geometry(world)
m <- matrix(c(1, 0, 0, -1), nrow = 2, ncol = 2)
world_sfc_mirror = world_sfc * m
tmap::qtm(world_sfc_mirror)

_05-ex_world_sfc_mirror_solution

Robinlovelace commented 7 months ago

Thanks for testing @smkerr and happy to take a look with a view to fixing what looks like a definite issue, unless someone gets there first.