DS4PS / cpp-529-fall-2020

http://ds4ps.org/cpp-529-fall-2020/
0 stars 0 forks source link

Final Project #29

Open malmufre opened 3 years ago

malmufre commented 3 years ago

Hello,

I am going through the steps to create a Dorling cartogram for the city that I chose, and I have reached this step.

msp_dorling <- merge( msp_dorling, d, by.x="GEOID", by.y="tractid", all.x=T ) class(msp_dorling) and up to this step where I merge my Dorling cartogram with object d (which contains the census data) , the class of the msp_dorling is an sp object.

However when I try to add these variables to my Dorling cartogram the msp_dorling changes to an sf object.

hstn.pop <-
get_acs( geography = "tract", variables = "B01003_001",
         state = "48", county = "201", geometry = FALSE ) %>% 
         select( GEOID, estimate ) %>%
         rename( POP=estimate )

hstn.mhhi <- 
  get_acs( geography = "tract", variables = "B19013_001",
           state = "48", county = "201", geometry = FALSE ) %>% 
  select( GEOID, estimate ) %>% 
  rename( MHHI=estimate )

library( tigris )
library( pander )

msp_dorling <- tracts( state="48", county="201", cb=TRUE, year=2015 )

Then when I try this

msp_dorling <- merge( msp_dorling, hstn.pop, by.x="GEOID", by.y="GEOID" )
msp_dorling <- merge( msp_dorling, hstn.mhhi, by.x="GEOID", by.y="GEOID" )

head( msp_dorling@data ) %>% pander()

I get this error . Error in head(msp_dorling@data) : trying to get slot "data" from an object (class "sf") that is not an S4 object.

I have loaded the libraries sf spand rgdalbut It still did not seem to work. Where am I going wrong?

Thanks

lecy commented 3 years ago

The merge() function is able to combine an sp object with a date.frame object.

It cannot user merge() with an sf object. You could have to cast the sf object as an sp object first.

What is the class of this object here?

msp_dorling <- tracts( state="48", county="201", cb=TRUE, year=2015 )
class( msp_dorling )

I suspect it's an sf object. It's also a regular polygon shapefile, not a dorling shapefile, but that is just a variable name issue that should cause an error.

lecy commented 3 years ago

If you go back through the labs there are examples of conversions from sf to sp, or sp to sf:

# convert sf map object to an sp version
msp.sp <- as_Spatial( msp )
malmufre commented 3 years ago

The class of the object

msp_dorling <- tracts( state="48", county="201", cb=TRUE, year=2015 )
class( msp_dorling ) 

is an sf object.

I suspect that I am doing something wrong in the steps of creating my Dorling cartogram . Up to this step my msp.sp is an sp object.

msp.sp <- as_Spatial( msp )

class( msp.sp )

I went through the steps we did in lab 4 , however when I saw the steps in the link provided in the course schedule https://ds4ps.org/cpp-529-fall-2020/LABS/save-dorling-cartogram.html I saw that there was the mhhi variable added so I am guessing this is where I went wrong.

lecy commented 3 years ago

Ok, I think I see the issue here. If you are trying to preview data you need to this syntax for the sp objects (you reference the embedded data frame using the @data slot):

head( msp.sp@data ) 

But you can use the basic version for sf objects since they embed the polygons inside the dataframe itself:

head( msp.sf )

So it wasn't an issue with your merge step, it was just the preview step.