1

I have a dataset of 4 rows and a few columns which are country, location, hits, lat, lon. Reproducible is:

structure(list(country = c("France", "France", "France", "France")
, location("Ile-de-France, Paris", "Ile-de-France, Villebon-sur-yvette", "Nord-Pas-de-Calais, Hérin", "Nord-Pas-de-Calais, Lille")
, Hits(1, 1, 3, 5)
, lat = c(46.227638, 46.227638, 46.227638, 46.227638)
, Ion = c(-2.213749, 2.213749, 2.213749, 2.213749)
)
, .Names = c("country", "location", "Hits", "lat", "Ion")
, class = "data.frame")

I want to use this in popup and show all the location and hits as 4 seperate lines.

current code i am using is:

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addCircles(lng=area$longitude, lat=area$latitude, popup=paste("Country:", area$Country, "<br>"
                                                            , "Location:", area$Location, "-", area$Hits, "<br>"))

If u have questions feel free to ask.

enter image description here

4
  • what specific problem are you having? what is your question?
    – pcantalupo
    Commented Sep 11, 2015 at 12:40
  • the output is as follows on the web link. fsaiyed.files.wordpress.com/2015/06/capture1.png i want to be able to have the 4 locations with their hit instead of the same being repeated Commented Sep 11, 2015 at 13:00
  • If you have enough reputation point, please post the image in your question. Web links have a tendency to disappear.
    – pcantalupo
    Commented Sep 11, 2015 at 13:03
  • sorry didnt knew if i had enough points or not, but looks like i do. Commented Sep 11, 2015 at 13:09

2 Answers 2

2

There were some mistakes in your example. Try this

library(leaflet)
area <- data.frame(country = c("France", "France", "France", "France")
           , location= c("Ile-de-France, Paris", "Ile-de-France, Villebon-sur-yvette", "Nord-Pas-de-Calais, Hérin", "Nord-Pas-de-Calais, Lille")
           , Hits= c(1, 1, 3, 5)
           , lat = c(46.234638, 46.456638, 46.288638, 46.900638)
           , lon = c(2.313749, 2.413749, 2.513749, 2.613749)
)


m <- leaflet() %>%
addTiles() %>%  # Add default OpenStreetMap map tiles
addCircles(lng=area$lon, lat=area$lat, 
popup=paste("Country:", area$country, "<br>", "Location:", area$location, "-", area$Hits, "<br>"))

The main issue with your example is in the provided coordinates. You assigned the same coordinates to the four points. This results in the display of the last point only.

0

If you want to plot multiple versions of the same point, you'll have to adjust the coordinates so that they are not all the same, or reshape your data so that each additional information is a column in your data set. I call this 'jittering' and have a function to do it below. It provides a slight offset without being too far off.

I use the following code to add white noise to the latitude and longitude so that they are close, but do not overlap.

This would solve your issue of repeated values for longitude and latitude. Another use case is that if your data is at zip code resolution, and you have the coordinates for the zip code centroid, then all locations within the zip would be plotted on top of each other and you would only see the last one.

# Add Jittering to the Zips so that they don't stack
l <- nrow(last)
jitterFactor <- function(l){
  jF <- runif(l, min = -1, max = 1)
  jF <- jF/100

  return(jF)
}

# use randomness so that 0's don't overlap
last$JitterLat <- jitterFactor(l)
last$JitterLon <- jitterFactor(l)

# apply the jitter
last$Lat <- last$latitude + last$JitterLat
last$Lon <- last$longitude + last$JitterLon

In the map below, the three green circles are all the same location, but represent different month's data. The jitter function spread them out a little.

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.