我正在使用传单htmlwidget实现来使用R绘制基于Web的地图。我正在寻找一个特定的标记,无法找到它,并意识到它根本没有被显示。但是,当我将我的数据集分组到只是该条目时,标记显示得很漂亮。
以下是标记的屏幕截图,代码在将数据子集化为此标记后运行(使用简单的R脚本thecounted <- thecounted[thecounted$age==6,]
):
将整个数据集作为标记放下时,这是相同的位置。
有谁知道发生了什么事?我对浏览器/传单将放置的标记数量有任何限制吗?这不是特定于此条目的故障,因为许多其他标记都没有显示......
以下是我的全部代码。
#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", ""))))))
)
答案 0 :(得分:4)
传单中缺少的点通常是由NA
数据引起的,其中传单不会在其中包含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)