リーフレットの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", ""))))))
)
はいを笑っています!これは素晴らしいです。 しかし、その人の緯度/緯度の値は私のマシン上ではNAとして読み込まれていなかったので、私はそれらを除外しなければならなかった thecounted < - thecounted $ lat!= ""、 – peopletrees