Last active
May 30, 2025 21:32
-
-
Save ar-puuk/c5c9443ac0868321f6e241623a3fab77 to your computer and use it in GitHub Desktop.
Revisions
-
ar-puuk revised this gist
May 30, 2025 . 1 changed file with 18 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,10 +3,13 @@ library(sf) 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)) @@ -28,18 +31,18 @@ shapefile_plot <- tm_shape(EU_eflsk_2_cropped) + 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 -
ar-puuk revised this gist
May 30, 2025 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,10 @@ 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"), -
ar-puuk created this gist
May 30, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ # Load necessary libraries library(sf) library(tmap) library(tmaptools) EU_eflsk_2 <- sf::st_read( "https://raw.githubusercontent.com/leakyMirror/map-of-europe/refs/heads/master/GeoJSON/europe.geojson" ) |> sf::st_make_valid() # 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) # Setting tmap to plot mode (static mode) tmap_mode("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 export using this: tmap_save( shapefile_plot, filename = file.path(proj.path, "project_folder", "shapefile_plot.png"), width = 1920, height = 1080, dpi = 300 )