DS4PS / ddmp-uw-class-spring-2019

Data-Driven Management & Policy Course at UW, Spring 2019
https://ds4ps.github.io/ddmp-uw-class-spring-2019/
4 stars 3 forks source link

Error generating map #9

Open taylynners04 opened 5 years ago

taylynners04 commented 5 years ago

Hi there (@MAGALLANESJoseManuel),

I am trying to generate the map for part 1 of lab 6. I created the merged layer by zipcode, and then created categorical map layers that indicate zipcodes where there are more cash donations or more in kind donations (I generated dummy variables for both cash and in kind donations). Here is the code I am used to generate the cash and in kind layers:

mapCash=layerMoreCash[layerMoreCash$cash_or_in_kind_Cash==1,]

mapInKind=layerMoreCash[layerMoreCash$cash_or_in_kind_InKind==1,]

And here is the code that I used to try and make the final map:

base= tm_shape(baseMap) + tm_polygons()

layer_1 = base + tm_shape(mapCash) + tm_polygons(col = 'darkgreen',border.col = NULL)

layer_1_2 = layer_1 + tm_shape(mapInKind) + tm_polygons(col = 'darkolivegreen1',border.col = NULL)

ContTypeMap = layer_1_2 + tm_compass(position = c('left','TOP'),type = 'arrow') + tm_scale_bar(position=c("RIGHT", "BOTTOM"),width = 0.2)

ContTypeMap

When I run this final chunk to generate the map, I get the error message:

Error in is.list(x) && all(vapply(x, is.list, TRUE)) : could not allocate memory (0 Mb) in C function 'R_AllocStringBuffer'

Is this error telling me that the map is too large for my computer memory space? Is there something wrong in my code, or can you provide a work around for this error?

Thanks! Taylor Bailey

MAGALLANESJoseManuel commented 5 years ago

Can you please send the code used to create 'layerMoreCash'?

RJKB2019 commented 5 years ago

Hello, I tried to use:

contriWA_2016$CashAmt <- ifelse(contriWA_2016$cash_or_in_kind == 'Cash', contriWA_2016$amount, 0) contriWA_2016$IKAmt <- ifelse(contriWA_2016$cash_or_in_kind == 'In kind', contriWA_2016$amount, 0)

WA_zip_contri= contriWA_2016 %>%
group_by(contributor_zip) %>%
summarize('Total Cash'=sum(CashAmt))

But I can only summarize either Total Cash or Total In Kind - how to summarize them both?

projectsUW commented 5 years ago

There might be several ways to get that. One option would be:

  1. Compute the sum of money 'inkind' per zip code. Save that as a dataframe
  2. Compute the sum of money 'incash' per zip code. Merge that to the the previous one
  3. Create a column, with a binary variable (0 / 1) , just showing which is higher.
  4. plot the 0 and plot the 1.
taylynners04 commented 5 years ago

@MAGALLANESJoseManuel I don't have the original code anymore that I was using to create "layerMoreCash." However, I think the problem was that I was not aggregating the data by zipcode. I was just trying to group by zipcode, without computing an average or sum of the data, so this was creating a very large spatial layer (around 8GB). Everything worked correctly when I aggregated the data by zipcode and created a much smaller spatial layer. Thanks!

wainman19 commented 5 years ago

Hi, I keep getting this error when i go to knit my rmd file:

Quitting from lines 17-74 (Lab_6_v3.Rmd) Error in crs(wazipMap) : could not find function "crs" Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval

Execution halted

Any suggestions? Thanks

projectsUW commented 5 years ago

Make sure to call:

library(raster)

before using crs()

wainman19 commented 5 years ago

Yup I did, still the same message.

MAGALLANESJoseManuel commented 5 years ago

Make sure the R chunk is set to:

eval=TRUE

wainman19 commented 5 years ago

Okay, I found the issue about not being unable to find the function. CRS was not capitalized. But doing that created a completely different error...

is.na() applied to non-(list or vector) of type 'S4'Error in CRS(baseMap) : no method for coercing this S4 class to a vector

projectsUW commented 5 years ago

I can't help you with the information you shared, I would need to see the code you used from the start.

ejvanholm commented 5 years ago

I second what Jose said, but one other general piece of advice from years of making mistakes. You can often figure out the problem by googling the error message R gives you. They are't often written in clear English, but someone else has hit that same snag, and you'll often find a message board of other people trying to help them. I'm not sure that will help here, but just a broader point that'll probably be unsatisfactory.

wainman19 commented 5 years ago

Thanks all. Here is the link to the markdown file code:

https://drive.google.com/file/d/1L6-LRzIeFyYfFWFBUCeRc5I6n_YLOOU6/view?usp=sharing


title: "Lab 6 v3" output: html_document

knitr::opts_chunk$set(echo = TRUE)

Part 1

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:


library(tmap)

load("~/Desktop/503 Group/Session6/contriWA_2016.RData")

str(contriWA_2016)

zippedSHP= "https://github.com/EvansDataScience/data/raw/master/WAzips.zip"

library(utils)
library(rgdal)

temp=tempfile()
download.file(zippedSHP, temp)
unzip(temp)

(maps=list.files(pattern = 'shp'))

library(rgdal)
library(sp)
library(tmap)

wazipMap <- readOGR("SAEP_ZIP_Code_Tabulation_Areas.shp",stringsAsFactors=F)

waZips = tm_shape(wazipMap) + tm_polygons(border.col = 'firebrick',
                                 col = 'dodgerblue3')
waZips

library(rmapshaper)
# This will make just a border of the state
baseMap <- ms_dissolve(wazipMap)

waBorder = tm_shape(baseMap) + tm_polygons(col = 'white',
                                           lwd = 1)
waBorder

library(dplyr)
library(ggplot2)
library(sf)

(CashZip=table(contriWA_2016$contributor_zip,contriWA_2016$cash_or_in_kind))

mapCRS=CRS(wazipMap)

contriWA_geo <- SpatialPointsDataFrame(contriWA_2016[,c(10:9)], # Lon/Lat
                    contriWA_2016,    #the original data frame
                    proj4string = mapCRS)   # assign a CRS of map 

names(contriWA_geo)

class(contriWA_geo)

waDots1 = waBorder + 
          tm_shape(contriWA_geo) + 
          tm_dots(size = 0.1,col = 'mediumvioletred',alpha=0.5,shape = 20) 

waDots1

Interactive


library(leaflet)

leaflet(contriWA_geo) %>%
  addTiles()%>%
  addCircleMarkers(clusterOptions = markerClusterOptions())

Next

library(dplyr)

WA_zip_contri = contriWA_2016 %>%
  group_by(contributor_zip) %>%
  summarize('Ave_Amount'=mean(amount))

head(WA_zip_contri)

names(wazipMap)
str(wazipMap$ZCTA5CE10)

Let's turn contributor zip into a character...

WA_zip_contri$contributor_zip=as.character(WA_zip_contri$contributor_zip)

Merge the map and the data... *layerContrib is like a new map

layerContrib=merge(wazipMap,WA_zip_contri,
                   by.x ='ZCTA5CE10',
                   by.y ='contributor_zip', all.x=F)

Now I am going to plot... Define the variable to plot, define the pallet and then you come here start with boarder map, add layerContri map, then add polygons for colors..the result will be layer 1.

library(tmap)
library(RColorBrewer)
library(classInt)

varToPLot=layerContrib$AVE_Amount
colorForPalette='Pinks'

layer1= waBorder +  
        tm_shape(layerContrib) +
                tm_polygons("Cash", 
                            style="quantile", 
                            n=5, 
                            title="Cash Contributions") 

fullMap= layer1 + tm_compass(position = c('left','TOP'), type = 'arrow') +
                  tm_scale_bar(position=c("RIGHT", "BOTTOM"),width = 0.2)

fullMap

Final Project

I have finished collecting data for my project. I actually collected the data earlier this year as part of an exercise app I regularly use on my phone.