[R-sig-Geo] [Help] Error in spChFIDs(SP, x) : lengths differ
Ignacio Martinez
ignacio82 at gmail.com
Fri Sep 4 18:36:19 CEST 2015
This works :-) :
library(dplyr)
library(leaflet)
library(rgdal)
library(sp)
states <- rgdal::readOGR(dsn = "./cb_2014_us_state_5m.shp",
layer = "cb_2014_us_state_5m", verbose =
FALSE)
statesnames <-
states at data %>% mutate(STUSPS = as.character(STUSPS)) %>%
select(NAME, STUSPS)
counts <-
structure(
list(
STUSPS = c(
"TX", "CA", "FL", "OH", "NY", "CO",
"MI", "IL", "PA", "NJ", "VA", "NC", "GA", "WA", "MN", "MD",
"AZ",
"IA", "MA", "WI", "TN", "LA", "IN", "SC", "KY", "ID", "MO",
"AL",
"OK", "CT", "KS", "OR", "UT", "NE", "ND", "NV", "MS", "AR",
"SD",
"MT", "DC", "WY", "AK", "NM", "HI", "NH", "WV", "ME", "RI",
"DE",
"VT", "PR", "VI"
), count = c(
12031L, 11968L, 7232L, 5113L, 4733L,
4392L, 4251L, 4168L, 3640L, 3374L, 3371L, 3284L, 3084L, 2837L,
2662L, 2650L, 2528L, 2414L, 2378L, 2373L, 2236L, 2158L, 2120L,
1959L, 1758L, 1700L, 1547L, 1466L, 1445L, 1345L, 1305L, 1297L,
1253L, 1234L, 1166L, 1063L, 981L, 963L, 915L, 718L, 673L, 653L,
650L, 637L, 579L, 561L, 510L, 496L, 486L, 396L, 273L, 6L, 5L
)
), .Names = c("STUSPS",
"count"), class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA,-53L)
)
counts <- counts %>% inner_join(statesnames, by = "STUSPS") %>%
mutate(popup = paste0(
'<b>',NAME,'</b>','<br>',
'<b>Number:</b> ', prettyNum(count,big.mark = ",",scientific =
FALSE)
))
USA <- merge(states, counts) # uses sp::merge.Spatial
USA <- USA[-which(USA$STUSPS %in% as.character(USA at data$STUSPS[is.na
(USA at data$count)])),]
pal <- colorNumeric(
palette = "Greens",
domain = USA$count)
leaflet(USA, height = "490px", width = "800px") %>%
addPolygons(
stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
color = ~ pal(count), popup = ~popup
) %>%
setView(lng = -98.579394, lat = 37, zoom = 4) %>%
addLegend(position = "bottomright",
pal = pal,
title = "Number",
opacity = 1, values = USA$count)
Please let me know if you see something that I could be doing better.
Thanks a lot!
Ignacio
On Fri, Sep 4, 2015 at 11:59 AM Ignacio Martinez <ignacio82 at gmail.com>
wrote:
> I'm not sure what I'm missing. This is not working (same error):
>
> counts <-
> structure(
> list(
> STUSPS = c(
> "CA", "NC", "TX", "FL", "VA", "OH",
> "NY", "GA", "IL", "WA", "CO", "AZ", "MD", "LA", "SC", "KS",
> "DC",
> "TN", "MA", "MI", "MN", "NJ", "WI", "PA", "AL", "KY", "OK",
> "MO",
> "ID", "MS", "NM", "IN", "NV", "AR", "OR", "AK", "UT", "IA",
> "MT",
> "HI", "NE", "CT", "WV", "WY", "SD", "VT", "ND", "ME", "RI",
> "NH",
> "DE", "PR", "GU", "VI", "MP", "AS"
> ), count = c(
> 36511L, 27513L,
> 25861L, 21099L, 19415L, 17012L, 15860L, 14362L, 13923L, 13753L,
> 11671L, 10540L, 9777L, 8923L, 8355L, 8219L, 8142L, 8076L,
> 7985L,
> 7770L, 7662L, 7531L, 7273L, 7212L, 7042L, 6708L, 6674L, 6429L,
> 6239L, 5580L, 5395L, 5172L, 5013L, 4933L, 4911L, 4797L, 4694L,
> 4458L, 3873L, 3757L, 3608L, 3111L, 2604L, 2217L, 2156L, 2143L,
> 2030L, 1544L, 1417L, 1168L, 772L, 531L, 148L, 63L, 7L, 2L
> )
> ), .Names = c("STUSPS",
> "count"), class = c("tbl_df", "tbl", "data.frame"),
> row.names = c(NA,-56L)
> )
>
>
> x = c("leaflet", "rgdal", "maptools", "mapproj", "rgeos", "dplyr",
> "sp")
> lapply(x, library, character.only = TRUE)
> # From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
> states <- readOGR(dsn = "./cb_2014_us_state_5m.shp",
> layer = "cb_2014_us_state_5m", verbose = FALSE)
> # Make a copy of the SPDF attribute table, and then work normally, as
> with any data.frame/data.table object
> states.df <- states at data
> # Create an explicit attribute to keep polygons IDs (useful to
> "re-attach" the table to the polygons later)
> states.df <- states.df %>% mutate(rn=row.names(states))
>
> # join them
>
> US = merge(states.df, counts) # uses sp::merge.Spatial
> US = US[-which(US$STUSPS == "PR"),]
>
>
> # Re-attach the attribute table to the SPDF
> states at data <- US
> # Make sure polygons IDs and data.frame row.names match
> states <- spChFIDs(states, states$rn)
>
> Thanks!
>
> On Fri, Sep 4, 2015 at 11:47 AM Edzer Pebesma <
> edzer.pebesma at uni-muenster.de> wrote:
>
>> On 09/04/2015 03:53 PM, Ignacio Martinez wrote:
>> > Thanks Edzer. I think I understand the problem better now (this is all
>> > very new to me). Is there a solution so I can generate the map with my
>> data?
>> >
>>
>> The STUSPS fields of counts and states match, but you seem to filter the
>> attribute table after you merged counts and states, which drops PR, but
>> doesn't take it out of states:
>>
>> > match(c("PR", "na", "MH", "FM", "PW"), counts$STUSPS)
>> [1] 52 NA NA NA NA
>>
>> So, in case you want to deselect PR, a shorter way to get there is
>>
>> > m = merge(states, counts) # uses sp::merge.Spatial
>> > m = m[-which(m$STUSPS == "PR"),]
>> > length(m)
>> [1] 55
>>
>>
>>
>> > Thanks again!
>> >
>> > On Fri, Sep 4, 2015 at 9:51 AM Edzer Pebesma
>> > <edzer.pebesma at uni-muenster.de <mailto:edzer.pebesma at uni-muenster.de>>
>> > wrote:
>> >
>> >
>> >
>> > On 09/04/2015 03:29 PM, Ignacio Martinez wrote:
>> > > I'm trying to create a map using leaflet. I'm basically following
>> this
>> > > <https://github.com/rstudio/leaflet/issues/169>but my data is a
>> bit
>> > > different this time around. Additionally, I'm using dplyr instead
>> of
>> > > data.table.
>> > >
>> > > This is the code i'm trying to run:
>> > >
>> > > counts <-
>> > > structure(
>> > > list(
>> > > STUSPS = c(
>> > > "CA", "NC", "TX", "FL", "VA", "OH",
>> > > "NY", "GA", "IL", "WA", "CO", "AZ", "MD", "LA", "SC",
>> > "KS",
>> > > "DC",
>> > > "TN", "MA", "MI", "MN", "NJ", "WI", "PA", "AL", "KY",
>> > "OK",
>> > > "MO",
>> > > "ID", "MS", "NM", "IN", "NV", "AR", "OR", "AK", "UT",
>> > "IA",
>> > > "MT",
>> > > "HI", "NE", "CT", "WV", "WY", "SD", "VT", "ND", "ME",
>> > "RI",
>> > > "NH",
>> > > "DE", "PR", "GU", "VI", "MP", "AS"
>> > > ), count = c(
>> > > 36511L, 27513L,
>> > > 25861L, 21099L, 19415L, 17012L, 15860L, 14362L,
>> > 13923L, 13753L,
>> > > 11671L, 10540L, 9777L, 8923L, 8355L, 8219L, 8142L,
>> > 8076L, 7985L,
>> > > 7770L, 7662L, 7531L, 7273L, 7212L, 7042L, 6708L,
>> > 6674L, 6429L,
>> > > 6239L, 5580L, 5395L, 5172L, 5013L, 4933L, 4911L,
>> > 4797L, 4694L,
>> > > 4458L, 3873L, 3757L, 3608L, 3111L, 2604L, 2217L,
>> > 2156L, 2143L,
>> > > 2030L, 1544L, 1417L, 1168L, 772L, 531L, 148L, 63L,
>> 7L, 2L
>> > > )
>> > > ), .Names = c("STUSPS",
>> > > "count"), class = c("tbl_df", "tbl",
>> > "data.frame"),
>> > > row.names = c(NA,-56L)
>> > > )
>> > >
>> > >
>> > > x = c("leaflet", "rgdal", "maptools", "mapproj", "rgeos",
>> "dplyr")
>> > > lapply(x, library, character.only = TRUE)
>> > > # From
>> > https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
>> > > states <- readOGR(dsn = "./cb_2014_us_state_5m.shp",
>> > > layer = "cb_2014_us_state_5m", verbose =
>> FALSE)
>> > > # Make a copy of the SPDF attribute table, and then work
>> > normally, as
>> > > with any data.frame/data.table object
>> > > states.df <- states at data
>> > > # Create an explicit attribute to keep polygons IDs (useful to
>> > > "re-attach" the table to the polygons later)
>> > > states.df <- states.df %>% mutate(rn=row.names(states))
>> > >
>> > > # join them
>> > > states.df <- full_join(x = states.df, y = counts,
>> by="STUSPS") %>%
>> > > filter(!(STUSPS%in%c("PR", "na", "MH", "FM", "PW"))) %>%
>> > na.omit() %>%
>> > > mutate(popup=paste0('<b>',NAME,'</b>','<br>',
>> > > '<b>count:</b> ',
>> > > prettyNum(count,big.mark=",",scientific=FALSE)))
>> >
>> > at this stage, you have:
>> >
>> > > dim(states at data)
>> > [1] 56 9
>> > > dim(states.df)
>> > [1] 55 12
>> >
>> > >
>> > > # Re-attach the attribute table to the SPDF
>> > > states at data <- states.df
>> >
>> > now, you created an invalid object:
>> > > length(geometry(states))
>> > [1] 56
>> > > dim(states at data)
>> > [1] 55 12
>> >
>> > this confirms once more what has been said on this list so often,
>> that
>> > instead of using constructor functions such as
>> > SpatialPolygonsDataFrame(), assigning slots directly is dangerous
>> and
>> > better left to those who know what they do (and check sanity):
>> >
>> > > states <- SpatialPolygonsDataFrame(geometry(states), states.df)
>> > Error in SpatialPolygonsDataFrame(geometry(states), states.df) :
>> > Object length mismatch:
>> > geometry(states) has 56 Polygons objects, but states.df has 55
>> rows
>> >
>> >
>> > > # Make sure polygons IDs and data.frame row.names match
>> > > states <- spChFIDs(states, states$rn)
>> > >
>> > > # Create map as usual...
>> > >
>> > > The error I get is:
>> > >
>> > > Error in spChFIDs(SP, x) : lengths differ
>> > >
>> > >
>> > > Thanks for the help!
>> > >
>> > >
>> > > Ignacio
>> > >
>> > > [[alternative HTML version deleted]]
>> > >
>> > > _______________________________________________
>> > > R-sig-Geo mailing list
>> > > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>> > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> > >
>> >
>> > --
>> > Edzer Pebesma
>> > Institute for Geoinformatics (ifgi), University of Münster,
>> > Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
>> > Journal of Statistical Software: http://www.jstatsoft.org/
>> > Computers & Geosciences: http://elsevier.com/locate/cageo/
>> > Spatial Statistics Society http://www.spatialstatistics.info
>> >
>> > _______________________________________________
>> > R-sig-Geo mailing list
>> > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >
>>
>> --
>> Edzer Pebesma
>> Institute for Geoinformatics (ifgi), University of Münster,
>> Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
>> Journal of Statistical Software: http://www.jstatsoft.org/
>> Computers & Geosciences: http://elsevier.com/locate/cageo/
>> Spatial Statistics Society http://www.spatialstatistics.info
>>
>>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list