How to change color of Polyline when it is passing a Polygon in leaflet package using R.

I am using Leaflet package to plot latitude and longitude as a square.
Also, plotting polylines which are passing these squares.

My concern is how can I change the color of the polyline when it passes through any square.

Color change is needed also, additional thickness change is appreciated.

Code somewhat looks like:-


polygon1_data1 = subset(polygon_dataset,polygon_type==1)
polygon1 <- st_as_sf(polygon1_data1 coords = c("LONGITUDE", "LATITUDE"))
polygon1 <- st_buffer(polygon1, dist = 1.25, endCapStyle = "SQUARE")
polygon1 <- st_union(polygon1)

#### Same for polygon 2,3,4,5,6 ######

map1 <- leaflet() %>%
addTiles() %>%

addPolygons(data = polygon1,color="grey",stroke = FALSE,layerId = 1)%>%
addPolygons(data = polygon2,color="yellow",stroke = FALSE,layerId = 2)%>%
addPolygons(data = polygon3,color="#ffbf00",stroke = FALSE,layerId = 3)%>%
addPolygons(data = polygon4,color="red",stroke = FALSE,layerId = 4)%>%
addPolygons(data = polygon5,color="#8B0000",stroke = FALSE,layerId = 5)%>%
addPolygons(data = polygon6,color="purple",stroke = FALSE,layerId = 6)%>%
addPolylines(data = Polyline1,lng = ~as.numeric(Lon),lat = ~as.numeric(Lat),layerId = 7)

Any help is appreciated..

Thank you for your time.


Apart from the code, would you also be able to provide a minimal dataset so we can run the code and actually try and fix it?

You can find help in the article below how to do this:


Your example is not exactly reproducible, as @pieterjanvc mentioned, so allow me to continue with an example using the North Carolina shapefile that ships with {sf} package.

What you want is to calculate the intersection of your line and squares separately, using the st_intersection function from {sf} package.

This way you will have two lines object - the original line, and the cross section of the original line and your polygons (in your case squares, in mine some semi random NC counties).

Just make sure you plot the polyline of intersections after the original line.


# included with sf package
shape <- st_read(system.file("shape/nc.shp", package="sf")) 

# some semi-random counties
shape <- shape %>% 
  filter(NAME %in% c("Wilkes", "Forsyth", "Durham", "Iredell")) %>% 

# a slice of the 36th parallel
parallel36 <- st_linestring(matrix(c(-84, 36, -75, 36), 
                                   nrow = 2, byrow = T), 
                            dim = "XY") %>% 
  st_sfc(crs = 4326)

# this is the key line, the rest is just chaff...
xsection <- st_intersection(parallel36, shape) 

leaflet() %>% 
  addProviderTiles(providers$Stamen.Toner) %>% 
  addPolygons(data = shape, stroke = NA,
              label = ~NAME) %>% 
  addPolylines(data = parallel36, # first the whole line
               weight = 1) %>% 
  addPolylines(data = xsection, # then the intersection
               color = "red",
               weight = 3)



Thank You, this is what I need.:smile:

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