# Load necessary libraries library(dplyr, warn.conflicts = FALSE) # For data manipulation library(rvest) # For web scraping library(janitor) # For cleaning data library(tidygeocoder) # For geocoding addresses library(RCzechia) # For Czech administrative regions data library(ggplot2) # For data visualization library(ggrepel) # For text labeling in plots # Get the directory path of the currently executing script file this_dir <- dirname(parent.frame(2)$ofile) # Set the working directory to the directory path obtained above setwd(this_dir) # Define the URL of the webpage containing information about spa towns in the Czech Republic url <- "https://www.mzcr.cz/seznam-lazenskych-mist-v-cr/" # Read the HTML content of the webpage html <- read_html(url) # Extract the table from the HTML content table <- html |> html_element("table") |> html_table() # Use the first row of the table as column names table <- table %>% row_to_names(1) # Clean up the data: Rename the location "Vráž" to "Vráž (okres Písek)" table <- table %>% mutate(Lokalita = case_when( Lokalita == "Vráž" ~ "Vráž (okres Písek)", TRUE ~ Lokalita )) # Geocode the locations in the table to obtain latitude and longitude coordinates lat_longs <- table %>% tidygeocoder::geocode(Lokalita, method = "arcgis", lat = latitude, long = longitude) # Convert latitude and longitude coordinates to spatial features poi <- lat_longs %>% st_as_sf(coords = c("longitude", "latitude"), crs = 4156) # Create a base plot of the Czech Republic plot1 <- ggplot() + geom_sf(data = republika("low"), color = "gray30", fill = NA) + # Outline of Czech Republic geom_sf( data = subset(RCzechia::reky(), Major == TRUE), # Major rivers color = "steelblue", alpha = .7 ) + # Plot spa towns on the map geom_sf(data = poi, color = "red", shape = 1, size = 1) + # Label spa towns on the map geom_text_repel(data = poi, aes(lat_longs$longitude, lat_longs$latitude, label = Lokalita), color = "black", size = 2.5) + labs(title = "Lázeňská místa v ČR") + # Set title of the plot theme_minimal() + # Set minimal theme for the plot theme(axis.title = element_blank()) # Remove axis titles # Save the plot as an image file ggsave("lazne.jpg", bg = "white", width = 30, units = "cm")