2016-07-05 71 views
2

我正在使用傳單htmlwidget實現來繪製使用R的基於Web的地圖。我正在查找特定標記,無法找到它,並且意識到它沒有顯示在所有。但是,當我將我的數據集子集合到只是那個條目時,標記顯示得很漂亮。傳單標記在某些情況下不顯示

這裏是標記的屏幕截圖,與所述數據子集劃分只是這個標記(,使用R腳本thecounted <- thecounted[thecounted$age==6,]的簡單線)之後已經運行代碼:

enter image description here

這裏是相同的位置將整個數據集放置爲標記時。

enter image description here

有誰知道這是怎麼回事?我對瀏覽器/傳單將放置的標記數量有任意的限制嗎?這不是特定於此條目的小故障,因爲其他許多標記都未顯示出來......

這是我的完整代碼。在單張

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

回答

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) 

enter image description here

+0

是的!謝謝你太棒了。 雖然,由於某種原因,該人的緯度/經度值並沒有在我的機器上讀作NA,所以我不得不將它們與 進行比較,然後計算出[thecounted $ lat!=「」,] – peopletrees

相關問題