6

I have rows of data with duplicate lat/longs and I want to include a label that has all of the grouped column data.

# Make sample dataframe
long <- c(147.5, 147.5, 147.5, 147, 147)
lat <- c(-36.5, -36.5, -36.5, -36, -36)
label <- c(1, 1, 2, 1, 2)

markers <- data.frame(lat,long,label)

# Combine labels based on lat long
markers <- markers %>%
  group_by(lat, long) %>%
  summarize(concat_label = toString(label)) 

# Markers with all of the labels
leaflet() %>%
  addTiles() %>%  
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup= markers$concat_label
  )

Is there a version of toString that uses a line break instead of a comma? I tried to use paste and paste0 but wasn't able to get it to work.

Erin
  • 255
  • 4
  • 14

3 Answers3

4

Your label variable can be an HTML string, so you can make the labels stack on each other with a <br/> tag. For example:

library(leaflet)

long <- c(147.768, 147.768, 147.768,147.768, 147.768, 147.768)
lat <- c(-36.852, -36.852, -36.852,-36.852, -36.852, -36.852)
label <- paste(sep = "<br/>",'long label1', 'long label2', 'long label3','long label4', 'long label5', 'long label6')

markers <- data.frame(lat,long,label)


leaflet() %>%
  addTiles() %>%  
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup=markers$label,
  )

You can also feed variables into the labels using the same approach - concatenating line-by-line.

dshkol
  • 1,208
  • 7
  • 23
  • Is there a way to do this without changing the structure of the underlying dataframe? I have many lat/longs so don't want to manually create the labels for each lat/long. – Erin Sep 26 '17 at 16:52
  • My example used the sample data frame you provided but you don't need to manually create the labels for each lat/long. Leaflet's `addMarkers` adds markers row by row, so its easy to generate individual markers and labels dynamically for each row - just use variables for the strings you are concatenating. For example: say you have for each observation with a lat/long variables that also describe that observation such as `detail1`, `detail2`, `detail3`, etc., you can combine them the same way as above: `label <- paste(sep = "
    ", detail1, detail2, detail3)`.
    – dshkol Sep 26 '17 at 17:21
  • Also, you should probably have a single observation for each lat/long coordinate unless there's a specific reason you want to have duplicate coordinates. I don't really understand that part of your question. – dshkol Sep 26 '17 at 17:22
  • I updated my question based on your feedback, thanks! – Erin Sep 28 '17 at 18:44
3
# Make sample dataframe
long <- c(147.5, 147.5, 147.5, 147, 147)
lat <- c(-36.5, -36.5, -36.5, -36, -36)
label <- c(1, 1, 2, 1, 2)

markers <- data.frame(lat,long,label)

# Aggregate method
markers <- aggregate(label ~ lat + long, markers, paste, collapse = "<br/>")

# Markers with all of the labels
leaflet() %>%
  addTiles() %>%  
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup= markers$label
  )

This question had the answer: Collapse / concatenate / aggregate a column to a single comma separated string within each group

Erin
  • 255
  • 4
  • 14
1

This is essentially @Erin's answer with one change. I think the task can be accomplished with paste0 which offers an advantage over various other possible methods that involve use of some flavor of sep= "<br>". With paste0 one has more control over where <br> tags are placed. Here two items are joined and then broken so that numbers are named in the popup. For additional styling use HTML tags in the paste0 call.

# Make sample dataframe
long <- c(147.5, 147.5, 147.5, 147, 147)
lat <- c(-36.5, -36.5, -36.5, -36, -36)
label <- c(1, 1, 2, 1, 2)

markers <- data.frame(lat,long,label)

# Paste0 Method
# This has the advantage of controlling where the breaks go.
markers$label <- paste0("Longitude: ", long,"<br>",
                         "Latitude: ",lat, "<br>",
                         "Label: ",label)

# Markers with all of the labels
leaflet() %>%
  addTiles() %>%  
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup= markers$label)

enter image description here

ncraig
  • 783
  • 1
  • 10
  • 23