-- Micha Silvercell: +972-523-665918 > On Saturday, 12 July 2025 at 23:35, Gabriel Cotlier <[email protected]> > wrote: > > Dear Micha, > Thank you very much for the tip. I worked out, but I am concerned about the > warning that appears:
I assume that is because you are using WGS84 long/lat. So there is a difference in distances E-W vs N-S. For example, in S. Spain 1 degree N-S is about 110 km while 1 degree E-W is about 90 km. > Scale on map varies by more than 10%, scale bar may be inaccurate On Sat, Jul 12, 2025 at 5:01 PM Micha Silver <[[email protected]](mailto:micha_silver%[email protected])> wrote: > -- > Micha Silver > cell: +972-523-665918 > > On Saturday, 12 July 2025 at 16:47, Gabriel Cotlier <[email protected]> > wrote: > >> Hello, >> I would like to ask for help to add a scale bar to the following map below. >> Thanks a lot in advance. >> Kind regards, >> Gabriel > > You might consider the `ggspatial` package that has a function: > `annotation_scale()` > > https://paleolimbot.github.io/ggspatial/reference/annotation_scale.html > >> >> library(ggplot2) >> library(sf) >> library(rnaturalearth) >> library(rnaturalearthdata) >> >> # Get world map data >> world <- ne_countries(scale = "medium", returnclass = "sf") >> >> # Define the bounding box for Europe >> europe_extent <- st_bbox(c(xmin = -12, xmax = 35, ymin = 30, ymax = 72), >> crs = st_crs(4326)) >> >> # Filter for Europe, then for Italy and Iberian Peninsula >> europe <- st_crop(world, europe_extent) >> italy_and_iberian <- europe[europe$name %in% c("Spain", "Portugal", >> "Italy"),] >> >> # Load another shapefile >> # Replace "path/to/your/shapefile.shp" >> A <- st_read("path/to/your/shapefile1.shp" ) >> B <- st_read("path/to/your/shapefile2.shp" ) >> sq1 <- st_read("path/to/your/shapefile3.shp" ) >> sq2 <- st_read("path/to/your/shapefile4.shp" ) >> >> # Plot the map with the added shapefile >> ggplot(data = europe) + >> geom_sf(fill = "lightgray") + # Background map >> geom_sf(data = italy_and_iberian, fill = "skyblue") + # Highlighted areas >> geom_sf(data = A, fill = "lightgreen") + # Added shapefile >> geom_sf(data = B, fill = "lightblue") + # Added shapefile >> geom_sf(data = sq1, fill =NA, color="red", lwd=0.5) + # Added shapefile >> geom_sf(data = sq2, fill=NA, color ="red", lwd=0.5) + # Added shapefile >> coord_sf(xlim = c(-12, 35), ylim = c(30, 72), expand = FALSE) + # Set map >> boundaries. >> theme_bw() + >> labs(title = "Italian, Iberian in Europe") >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> R-sig-Geo mailing list >> [email protected] >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list [email protected] https://stat.ethz.ch/mailman/listinfo/r-sig-geo
