0

Here is my code:

m <- leaflet() %>%
  addProviderTiles(providers$Stamen.Toner) %>%
  setView(lng = -107.9917071, lat = 59.5, zoom = 3.5) %>%
  addPolygons(data = plant,
              color = "#660000",
              weight = 1,
              smoothFactor = 0.5) %>%
  addCircleMarkers(lng = plant$lon, lat = plant$lat)
m

No matter what I try I get the following error message:

Error in polygonData.default(data) : Don't know how to get path data from object of class spec_tbl_df

My data frame from which my data comes is a simple 5 row by 3 columns of coordinates and the name of the place.

Thoughts?

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Roman10
  • 115
  • 1
  • 6
  • Can you show your dataset? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 Jan 22 '21 at 11:24
  • Name lat lon University 43.5339923 -80.2244647 University 49.8091536 -97.1330418 University 3.52682 -113.5244937 University 49.2519564 -123.2465285 University 45.5069177 -73.5791163 – Roman10 Jan 22 '21 at 12:30
  • Can you put it in the question as `dput()` or `reprex()` like in the article? – william3031 Jan 23 '21 at 21:04

1 Answers1

0

You had point data, not polygons.

library(leaflet)

plant <- data.frame(
  stringsAsFactors = FALSE,
              Name = c("University","University",
                       "University","University","University"),
               lat = c(43.5339923, 49.8091536, 3.52682, 49.2519564, 45.5069177),
               lon = c(-80.2244647,-97.1330418,
                       -113.5244937,-123.2465285,-73.5791163)
)


leaflet() %>%
  addProviderTiles(providers$Stamen.Toner) %>%
  setView(lng = -107.9917071, lat = 59.5, zoom = 3.5) %>%
  # I removed the addPolygons(), you are adding points, not polygons
  addCircleMarkers(lng = plant$lon, lat = plant$lat)

enter image description here

william3031
  • 1,653
  • 1
  • 18
  • 39
  • 1
    Thank you for your comment! You are absolutely right! It took me hours to figure this out. My code now is: m <- leaflet() %>% addProviderTiles(providers$Stamen.Toner) %>% setView(lng = -94, lat = 52, zoom = 3.5) %>% addCircleMarkers(m, lng = Canada$x2, lat = Canada$x1, radius = 1, color = "#5F04B4", weight = 10) m – Roman10 Jan 30 '21 at 14:15