在某些情况下不显示传单标记

leaflet marker not displaying in certain contexts

我正在使用 leaflet htmlwidget 实现来使用 R 绘制基于网络的地图。我正在寻找一个特定的标记,找不到它,然后意识到它根本没有显示。但是,当我将我的数据集子集化为 just 该条目时,标记显示得很漂亮。

这里是标记的屏幕截图,在将数据子集化到这个标记之后代码已经 运行(使用简单的 R 脚本行 thecounted <- thecounted[thecounted$age==6,]):

这是将整个数据集作为标记放置时的相同位置。

有人知道这是怎么回事吗?我是否达到了 browsers/leaflet 将放置的标记数量的任意限制?这不是此条目特有的故障,因为许多其他标记也没有显示...

这是我的全部代码。

#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", ""))))))
                                )

leaflet 中的缺失点通常是由 NA 数据引起的,其中 leaflet 不会在包含 NA 的行之后绘制任何内容。

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

去掉这个人你就笑了

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)