问题
I am using the leaflet htmlwidget implementation to draw a web-based map using R. I was looking for a specific marker, couldn't find it, and realized it wasn't being displayed at all. However, when I subset down my dataset to just that entry, the marker displays beautifully.
Here is a screenshot of the marker, with code having been run after subsetting the data to just this marker (using the simple line of R script thecounted <- thecounted[thecounted$age==6,]
):
Here is the same location when placing the whole dataset down as markers.
Does anyone know what is going on? Am I hitting some arbitrary limit on the number of markers that browsers/leaflet will lay down? This is not a glitch specific to this entry as plenty of other markers are not showing either...
Here is the entirety of my code.
#download needed packages you don't have
wants <- c("magrittr", "leaflet", "jsonlite", "curl")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
require(jsonlite)
require(curl)
require(leaflet)
require(magrittr)
#pull data from json file embedded in the Guardian's The Counted website: http://www.theguardian.com/thecounted
thecounted <- fromJSON("https://interactive.guim.co.uk/2015/the-counted/v/1455138961531/files/skeleton.json")
#Color-code for whether the victim was armed
# Red = Unarmed
unarmedC <-"#ff0000"
# Teal = armed
armedC <- "#008080"
# Black = Don't know or ambiguous category like "Non-lethal firearm" or "vehicle"
idkC <- "#000000"
pal <- colorFactor(c(idkC, rep(armedC,2), unarmedC, rep(idkC,4)), domain= c("Disputed",
"Firearm",
"Knife",
"No",
"Non-lethal firearm",
"Other",
"Unknown",
"Vehicle"))
# automatically set date range for pulled data
today <- Sys.Date()
today <- format(today, format="%b %d %Y")
dateRange <- paste0("(Jan 01 2015"," - ", today,")")
#Use the leaflet htmlwidget to create an interactive online visualization of data
leaflet(data = thecounted) %>% #data from the counted
#add default open source map tiles from OpenStreetMap
addTiles() %>%
#fit bounds around the USA
fitBounds(-125,25, -67,49) %>%
#add a map legend
addLegend(
title=paste(sep="<br/>","People killed by police",dateRange),
position = 'bottomright',
colors = c(unarmedC,armedC, idkC),
labels = c("Unarmed", "Armed", "Unknown / non-lethal / vehicle / other")) %>%
#dynamically add markers for people who were killed
addCircleMarkers(~long, ~lat, stroke=FALSE,
color = ~pal(armed), #color defined above
fillOpacity = ifelse(thecounted$armed=="No",0.75,0.35), #make unarmed dots more visible
#create pop-up windows with some information for each marker
popup = ~ paste(name, "<br/>",
"Age",age,"<br/>",
#include race if available
ifelse(race == "B", "Black",
ifelse(race == "W" , "White",
ifelse(race =="H", "Hispanic",
ifelse(race == "A", "Asian",
ifelse(race == "N", "Native American",
ifelse(race == "U", "Race unknown", "")))))),"<br/>",
#tell us whether they were unarmed or if unknown, else leave blank
#because the categories for being armed are convoluted
ifelse(armed=="No", "Unarmed<br/>",
ifelse(armed=="Unknown", "Unknown if armed<br/>",
ifelse(armed=="Vehicle", "Armed with 'vehicle'<br/>",
ifelse(armed=="Knife", "Had a knife<br/>",
ifelse(armed=="Disputed", "Disputed if armed<br/>", ""))))),
#include cause of death
ifelse(classification == "Gunshot", "Killed by gunshot",
ifelse(classification == "Death in custody", "Died in custody",
ifelse(classification == "Other", "",
ifelse(classification == "Taser", "Killed by taser",
ifelse(classification == "Struck by vehicle", "Struck by vehicle", ""))))))
)
回答1:
Missing points in leaflet is usually caused by NA
data, where leaflet doesn't plot anything after the row with the NA
in it.
thecounted[rowSums(is.na(thecounted)) > 0, ]
# uid armed name slug age gender race date state classification large lat long hasimage
# 738 738 Firearm Jason Hale jason-hale-738 29 M W 2015/8/19 WA Gunshot FALSE NA NA FALSE
Remove this guy and you're laughing
leaflet(data = thecounted[rowSums(is.na(thecounted)) == 0, ]) %>%
addTiles() %>%
addCircleMarkers(lat = ~lat, lng = ~long,
stroke = FALSE,
color = ~pal(armed),
fillOpacity = ifelse(thecounted$armed=="No",0.75,0.35),
popup = ~name)
来源:https://stackoverflow.com/questions/38194512/leaflet-marker-not-displaying-in-certain-contexts