Erin Erin - 2 years ago 171
R Question

R leaflet map: Include multiple rows of data in label popup

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.

Answer Source
# 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download