Creating map from dataframe

I have a dataframe with this structure:

structure(list(fecha = c("2024-07-29T12:51:17", "2024-07-29T12:52:19", 
"2024-07-29T12:52:19", "2024-07-29T12:53:14", "2024-07-29T12:53:14", 
"2024-07-29T12:54:32", "2024-07-29T12:56:07", "2024-07-29T12:56:32", 
"2024-07-29T12:57:34", "2024-07-29T13:01:21"), lat = c(42.5126, 
42.4781, 42.4887, 42.5162, 42.5204, 42.4963, 42.4903, 42.5123, 
42.5148, 42.4965), lon = c(-6.6907, -6.5679, -6.6065, -6.6177, 
-6.6688, -6.6272, -6.616, -6.6607, -6.6161, -6.6434), peakCurrent = c(-12L, 
-7L, -8L, -8L, -9L, -11L, -13L, 10L, -9L, -15L)), row.names = 2:11, class = "data.frame")

I want to make a map with Open Street Map as base, but I get an empty graphic.

HI @jynusmac -

Without seeing the code you tried, it will be hard for anyone to give you suggestions about what is wrong. Can you copy your code snippet into the thread?

Best,
Randy

Yes, for sure. First I convert df to an sf object:

df_sf <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326)

And then plot with ggplot

ggplot() +
        annotation_map_tile(type = "osm", zoom = 10) +
        geom_sf(data = df_sf, aes(geometry = geometry), color = "red", alpha = 0.5) +
        labs(title = "Polygon",
             x = "lon",
             y = "lat") +
        coord_sf(xlim = c(xmin, xmax), ylim = c(ymin, ymax), expand = FALSE)

And then an empty graphic

1 Like

If you want to do Leaflet map, which an interactive map:

leaflet() %>%
addProviderTiles("OpenStreetMap.Mapnik", group = "Mapnik")  %>%
  addPolygons(data = df_sf,
              fillColor = "red",
              fillOpacity = 0.5,
              color = "red",
              weight = 1,
              popup = ~paste0("<strong>Name: </strong>", df_sf$name, "<br>"),
              highlightOptions = highlightOptions(weight = 2, color = "blue", bringToFront = TRUE)) %>%
  setView(lng = mean(c(xmin, xmax)), lat = mean(c(ymin, ymax)), zoom = 10) %>%
  fitBounds(lng1 = xmin, lat1 = ymin, lng2 = xmax, lat2 = ymax)
1 Like

Could you run the following code and share the output here, in a code block?

dput(df)
1 Like
> dput(df)
structure(list(fecha = c("2024-07-29T12:51:17", "2024-07-29T12:52:19", 
"2024-07-29T12:52:19", "2024-07-29T12:53:14", "2024-07-29T12:53:14", 
"2024-07-29T12:54:32", "2024-07-29T12:56:07", "2024-07-29T12:56:32", 
"2024-07-29T12:57:34", "2024-07-29T13:01:21"), lat = c(42.5126, 
42.4781, 42.4887, 42.5162, 42.5204, 42.4963, 42.4903, 42.5123, 
42.5148, 42.4965), lon = c(-6.6907, -6.5679, -6.6065, -6.6177, 
-6.6688, -6.6272, -6.616, -6.6607, -6.6161, -6.6434), peakCurrent = c(-12L, 
-7L, -8L, -8L, -9L, -11L, -13L, 10L, -9L, -15L)), row.names = 2:11, class = "data.frame")

also

> dput(df_sf)
structure(list(fecha = c("2024-07-29T12:51:17", "2024-07-29T12:52:19", 
"2024-07-29T12:52:19", "2024-07-29T12:53:14", "2024-07-29T12:53:14", 
"2024-07-29T12:54:32", "2024-07-29T12:56:07", "2024-07-29T12:56:32", 
"2024-07-29T12:57:34", "2024-07-29T13:01:21"), peakCurrent = c(-12L, 
-7L, -8L, -8L, -9L, -11L, -13L, 10L, -9L, -15L), geometry = structure(list(
    structure(c(-6.6907, 42.5126), class = c("XY", "POINT", "sfg"
    )), structure(c(-6.5679, 42.4781), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6065, 42.4887), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6177, 42.5162), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6688, 42.5204), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6272, 42.4963), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.616, 42.4903), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6607, 42.5123), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6161, 42.5148), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6434, 42.4965), class = c("XY", "POINT", 
    "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
    input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), bbox = structure(c(xmin = -6.6907, 
ymin = 42.4781, xmax = -6.5679, ymax = 42.5204), class = "bbox"), class = c("sfc_POINT", 
"sfc"))), row.names = 2:11, sf_column = "geometry", agr = structure(c(fecha = NA_integer_, 
peakCurrent = NA_integer_), class = "factor", levels = c("constant", 
"aggregate", "identity")), class = c("sf", "data.frame"))
1 Like

I get this without using annotation_map_tile(), so maybe that's where the issue might be?

original sf data, saved as `df_sf`
structure(list(fecha = c("2024-07-29T12:51:17", "2024-07-29T12:52:19", 
"2024-07-29T12:52:19", "2024-07-29T12:53:14", "2024-07-29T12:53:14", 
"2024-07-29T12:54:32", "2024-07-29T12:56:07", "2024-07-29T12:56:32", 
"2024-07-29T12:57:34", "2024-07-29T13:01:21"), peakCurrent = c(-12L, 
-7L, -8L, -8L, -9L, -11L, -13L, 10L, -9L, -15L), geometry = structure(list(
    structure(c(-6.6907, 42.5126), class = c("XY", "POINT", "sfg"
    )), structure(c(-6.5679, 42.4781), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6065, 42.4887), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6177, 42.5162), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6688, 42.5204), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6272, 42.4963), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.616, 42.4903), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6607, 42.5123), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6161, 42.5148), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6434, 42.4965), class = c("XY", "POINT", 
    "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
    input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), bbox = structure(c(xmin = -6.6907, 
ymin = 42.4781, xmax = -6.5679, ymax = 42.5204), class = "bbox"), class = c("sfc_POINT", 
"sfc"))), row.names = 2:11, sf_column = "geometry", agr = structure(c(fecha = NA_integer_, 
peakCurrent = NA_integer_), class = "factor", levels = c("constant", 
"aggregate", "identity")), class = c("sf", "data.frame")) -> df_sf
library(tidyverse)
df_sf |> 
  ggplot() +
  geom_sf()

Created on 2024-07-29 with reprex v2.0.2

Yes, but what I need to do is to add a basemap to that data, hence the use of annotation_map_tile that seems not to work

Do you get any errors, or just an empty plot?

Only an empty plot, no more info

Not sure what's going wrong, but there's definitely something buggy going on: The first time I use reprex, I get this output:

original sf data saved as `df_sf`
structure(list(fecha = c("2024-07-29T12:51:17", "2024-07-29T12:52:19", 
"2024-07-29T12:52:19", "2024-07-29T12:53:14", "2024-07-29T12:53:14", 
"2024-07-29T12:54:32", "2024-07-29T12:56:07", "2024-07-29T12:56:32", 
"2024-07-29T12:57:34", "2024-07-29T13:01:21"), peakCurrent = c(-12L, 
-7L, -8L, -8L, -9L, -11L, -13L, 10L, -9L, -15L), geometry = structure(list(
    structure(c(-6.6907, 42.5126), class = c("XY", "POINT", "sfg"
    )), structure(c(-6.5679, 42.4781), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6065, 42.4887), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6177, 42.5162), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6688, 42.5204), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6272, 42.4963), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.616, 42.4903), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6607, 42.5123), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6161, 42.5148), class = c("XY", "POINT", 
    "sfg")), structure(c(-6.6434, 42.4965), class = c("XY", "POINT", 
    "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
    input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), bbox = structure(c(xmin = -6.6907, 
ymin = 42.4781, xmax = -6.5679, ymax = 42.5204), class = "bbox"), class = c("sfc_POINT", 
"sfc"))), row.names = 2:11, sf_column = "geometry", agr = structure(c(fecha = NA_integer_, 
peakCurrent = NA_integer_), class = "factor", levels = c("constant", 
"aggregate", "identity")), class = c("sf", "data.frame")) -> df_sf
library(tidyverse)
library(ggspatial)
library(rosm)
df_sf |> 
  ggplot() +
  annotation_map_tile() +
  geom_sf()
#> Loading required namespace: raster
#> Zoom: 11
#> Fetching 2 missing tiles
#>   |                                                                              |                                                                      |   0%  |                                                                              |===================================                                   |  50%  |                                                                              |======================================================================| 100%
#> ...complete!

Created on 2024-07-29 with reprex v2.0.2

but the second time, reprex hangs, and I wonder if it has to do with the rosm package creating a cache folder somewhere.

You might want to look at this blog. I cover a lot of detail on basemaps and different plotting packages.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.