# Load necessary libraries
library(sf)
#> Linking to GEOS 3.13.1, GDAL 3.10.2, PROJ 9.5.1; sf_use_s2() is TRUE
library(tmap)
library(tmaptools)
url <- "https://gisco-services.ec.europa.eu/distribution/v2/nuts/shp/NUTS_RG_20M_2024_3035.shp.zip"
EU_eflsk_2 <- sf::read_sf(
paste0("/vsizip/","/vsicurl/", url)
) |>
sf::st_transform("EPSG:4326") |>
dplyr::filter(LEVL_CODE == 2 & CNTR_CODE != "TR")
# Defining the bounding box
bbox <- sf::st_bbox(c(xmin =- 25, ymin =15, xmax=35, ymax=70), crs = sf::st_crs(EU_eflsk_2))
# Cropping the shapefile to the bounding box
EU_eflsk_2_cropped <- sf::st_crop(EU_eflsk_2, bbox)
#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries
# Setting tmap to plot mode (static mode)
tmap_mode("plot")
#> ℹ tmap mode set to "plot".
# Creating the map
shapefile_plot <- tm_shape(EU_eflsk_2_cropped) +
tm_borders (col = "black", lwd = 1) + # Add borders
tm_layout (
inner.margins = c(0.1, 0.05, 0.05, 0.05), # Increase bottom margin
outer.margins = c(0.0001, 0.0001, 0.0001, 0.0001), # Small outer margins
frame = TRUE, # Add a frame around the map
frame.lwd = 0.5, # Set frame line width
asp = 0 # Set aspect ratio to automatic
)
# # Define the output directory and file path
# output_dir <- file.path(proj.path, "project_folder")
# plot_file <- file.path(output_dir, "shapefile_plot.png")
#
# # Save the plot as a png file
# png(filename= plot_file, width = 1920, height = 1080, res = 300)
#
# # Print plot
# print(shapefile_plot)
#
# # Close the graphics device to save the plot
# dev.off()
## Alternatively You can view the plot using this:
shapefile_plot## And export the map using this:
tmap_save(
shapefile_plot,
filename = file.path(proj.path, "project_folder", "shapefile_plot.png"),
width = 1920, height = 1080,
dpi = 300
)
#> Error: object 'proj.path' not foundCreated on 2025-05-30 with reprex v2.1.1.9000
Session info
sessioninfo::session_info()
#> Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", :
#> running command '"quarto"
#> TMPDIR=C:/Users/pukar/AppData/Local/Temp/RtmpchTmNk/file12e7888d78ca -V' had
#> status 1
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.5.0 (2025-04-11 ucrt)
#> os Windows 11 x64 (build 26100)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.utf8
#> ctype English_United States.utf8
#> tz America/New_York
#> date 2025-05-30
#> pandoc 3.7.0.2 @ C:/PROGRA~1/Pandoc/ (via rmarkdown)
#> quarto NA @ C:\\PROGRA~1\\Quarto\\bin\\quarto.exe
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> abind 1.4-8 2024-09-12 [1] CRAN (R 4.5.0)
#> base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.5.0)
#> class 7.3-23 2025-01-01 [2] CRAN (R 4.5.0)
#> classInt 0.4-11 2025-01-08 [1] CRAN (R 4.5.0)
#> cli 3.6.5 2025-04-23 [1] CRAN (R 4.5.0)
#> codetools 0.2-20 2024-03-31 [1] CRAN (R 4.5.0)
#> colorspace 2.1-1 2024-07-26 [1] CRAN (R 4.5.0)
#> cols4all 0.8 2024-10-16 [1] CRAN (R 4.5.0)
#> crosstalk 1.2.1 2023-11-23 [1] CRAN (R 4.5.0)
#> curl 6.2.3 2025-04-01 [1] https://ar-puuk.r-universe.dev (R 4.5.0)
#> data.table 1.17.99 2025-04-09 [1] https://ar-puuk.r-universe.dev (R 4.5.0)
#> DBI 1.2.3.9027 2025-03-18 [1] https://ar-puuk.r-universe.dev (R 4.5.0)
#> dichromat 2.0-0.1 2022-05-02 [1] CRAN (R 4.5.0)
#> digest 0.6.37 2024-08-19 [1] CRAN (R 4.5.0)
#> dplyr 1.1.4.9000 2025-05-11 [1] Github (tidyverse/dplyr@2d87e4f)
#> e1071 1.7-16 2024-09-16 [1] CRAN (R 4.5.0)
#> evaluate 1.0.3 2025-01-10 [1] CRAN (R 4.5.0)
#> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.5.0)
#> fs 1.6.6 2025-04-12 [1] CRAN (R 4.5.0)
#> generics 0.1.4 2025-05-09 [1] CRAN (R 4.5.0)
#> glue 1.8.0 2024-09-30 [1] CRAN (R 4.5.0)
#> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)
#> htmlwidgets 1.6.4 2023-12-06 [1] CRAN (R 4.5.0)
#> KernSmooth 2.23-26 2025-01-01 [1] CRAN (R 4.5.0)
#> knitr 1.50 2025-03-16 [1] CRAN (R 4.5.0)
#> lattice 0.22-7 2025-04-02 [1] CRAN (R 4.5.0)
#> leafem 0.2.4 2025-05-01 [1] Github (r-spatial/leafem@41cbf71)
#> leaflegend 1.2.1 2024-05-09 [1] CRAN (R 4.5.0)
#> leaflet 2.2.2 2024-03-26 [1] CRAN (R 4.5.0)
#> leaflet.providers 2.0.0 2023-10-17 [1] CRAN (R 4.5.0)
#> leafsync 0.1.0 2019-03-05 [1] CRAN (R 4.5.0)
#> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.5.0)
#> logger 0.4.0 2024-10-22 [1] CRAN (R 4.5.0)
#> lwgeom 0.2-14 2024-02-21 [1] CRAN (R 4.5.0)
#> magrittr 2.0.3.9000 2025-05-25 [1] Github (tidyverse/magrittr@21093d0)
#> maptiles 0.10.0 2025-05-11 [1] Github (riatelab/maptiles@8c35b5d)
#> microbenchmark 1.5.0 2024-09-04 [1] CRAN (R 4.5.0)
#> pillar 1.10.2.9000 2025-04-06 [1] https://ar-puuk.r-universe.dev (R 4.5.0)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.5.0)
#> png 0.1-8 2022-11-29 [1] CRAN (R 4.5.0)
#> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.5.0)
#> R6 2.6.1 2025-02-15 [1] CRAN (R 4.5.0)
#> raster 3.6-32 2025-03-28 [1] CRAN (R 4.5.0)
#> RColorBrewer 1.1-3 2022-04-03 [1] CRAN (R 4.5.0)
#> Rcpp 1.0.14 2025-01-12 [1] CRAN (R 4.5.0)
#> reprex 2.1.1.9000 2025-05-25 [1] Github (tidyverse/reprex@07cd5d7)
#> rlang 1.1.6.9000 2025-05-25 [1] Github (tidyverse/rlang@bd757b6)
#> rmarkdown 2.29 2024-11-04 [1] CRAN (R 4.5.0)
#> rstudioapi 0.17.1 2024-10-22 [1] CRAN (R 4.5.0)
#> s2 1.1.9 2025-05-23 [1] CRAN (R 4.5.0)
#> sessioninfo 1.2.3 2025-02-05 [1] CRAN (R 4.5.0)
#> sf * 1.0-21 2025-05-15 [1] CRAN (R 4.5.0)
#> sp 2.2-0 2025-02-01 [1] CRAN (R 4.5.0)
#> spacesXYZ 1.5-1 2025-02-10 [1] CRAN (R 4.5.0)
#> stars 0.6-8 2025-02-01 [1] CRAN (R 4.5.0)
#> terra 1.8-50 2025-05-09 [1] CRAN (R 4.5.0)
#> tibble 3.2.1.9054 2025-05-04 [1] https://tidyverse.r-universe.dev (R 4.5.0)
#> tidyselect 1.2.1.9000 2025-04-01 [1] https://ar-puuk.r-universe.dev (R 4.5.0)
#> tmap * 4.1 2025-05-18 [1] Github (r-tmap/tmap@e24262a)
#> tmaptools * 3.2 2025-04-13 [1] https://r-tmap.r-universe.dev (R 4.5.0)
#> units 0.8-7 2025-03-11 [1] CRAN (R 4.5.0)
#> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2)
#> viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.5.0)
#> withr 3.0.2 2024-10-28 [1] CRAN (R 4.5.0)
#> wk 0.9.4 2024-10-11 [1] CRAN (R 4.5.0)
#> xfun 0.52 2025-04-02 [1] CRAN (R 4.5.0)
#> XML 3.99-0.18 2025-01-01 [1] CRAN (R 4.5.0)
#> xml2 1.3.8 2025-03-14 [1] CRAN (R 4.5.0)
#> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.5.0)
#>
#> [1] C:/Users/pukar/AppData/Local/R/win-library/4.5
#> [2] C:/Program Files/R/R-4.5.0/library
#> * ── Packages attached to the search path.
#>
#> ──────────────────────────────────────────────────────────────────────────────