Skip to content

Question on Scale bar

7 messages · Gabriel Cotlier, Micha Silver, Dewey Dunnington

#
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

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")
#
--
Micha Silver
cell: +972-523-665918
On Saturday, 12 July 2025 at 16:47, Gabriel Cotlier <gabiklm01 at gmail.com> wrote:

            
You might consider the `ggspatial` package that has a function: `annotation_scale()`

https://paleolimbot.github.io/ggspatial/reference/annotation_scale.html
Message-ID: <dToPcrlu8bdCVS9xVtduMn38wfWw7fZ6vScumW-YHAfxNCJuQgCqDs0uslBFodFZ89las-Sct3Jt5Tv6_FmL1G2C95qNV7o1knll3o0gUE8=@proton.me>
#
Dear Micha,
Thank you very much for the tip. I worked out, but I am concerned about the
warning that appears:

*Scale on map varies by more than 10%, scale bar may be inaccurate*

On Sat, Jul 12, 2025 at 5:01?PM Micha Silver <micha_silver+DEV at proton.me>
wrote:

  
  
#
--
Micha Silvercell: +972-523-665918
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.

        
On Sat, Jul 12, 2025 at 5:01?PM Micha Silver <[micha_silver+DEV at proton.me](mailto:micha_silver%2BDEV at proton.me)> wrote:

            
Message-ID: <TQKsAc81QsoytguW5-idjPO3a4W7TzUXsOfCsbL5mlZ4CPMqZyXhE4ImVm5SdkUFBZ9G98DhypDummQnCoOcYt9QvtmqgqDrul91fhpyPUA=@proton.me>
#
Yes is correct. Using CRS 3587 would correct this distortion?
Thanks a lot.

On Sun, Jul 13, 2025 at 5:48?AM Micha Silver <micha_silver+DEV at proton.me>
wrote:

  
  
#
--
Micha Silvercell: +972-523-665918
On Sunday, 13 July 2025 at 15:41, Gabriel Cotlier <gabiklm01 at gmail.com> wrote:

            
I don't think so
Check here:
https://epsg.io/3857

If it's important to keep distances accurate, you should look for an equidistant projection for your area. Maps of Europe are typically projected to EPSG:3035 which is equal area, not equidistant, but it's pretty close, and widely accepted.
Message-ID: <tgwLx6tWFfeOYhgJaEhYfUjiNxhzLMpn_hRu3XzHDhfkiEJdpuP7c6V0K20ZpzPaMof85TFbmTZOR-b5bybLVoMfXfHIvHR0IsBfpHVbKQA=@proton.me>
#
Glad the scale bar function from ggspatial is still useful!

I'm not sure that when I put that warning in that I anticipated the 
number of use-cases where this warning would be emitted. On most maps of 
large-scale areas you see in the wild, the scale bar will be wildly 
inaccurate for some places on the map and for most maps this is OK 
(unless your map is quite literally claiming to be a map of the distance 
between x and x, for example). The function of the scale bar is often to 
communicate order of magnitude, or often just has to exist as a 
requirement for a publication style guide.

What I'm getting at is that...I wrote that warning and I ignore it all 
the time :)

All the best,

-dewey
On 2025-07-13 10:22, Micha Silver via R-sig-Geo wrote: