DS4PS / cpp-529-spr-2021

Course shell for CPP 529 Community Analytics
http://ds4ps.org/cpp-529-spr-2021/
2 stars 0 forks source link

Error: File does not exist. Create it, or fix the path. #34

Open qaltimee opened 7 months ago

qaltimee commented 7 months ago

@AntJam-Howell Hi, I have my dorling cartogram created and saved.

library( geojsonio )

seattle.sp <- spTransform( seattle.sp_dorling, CRS("+proj=longlat +datum=WGS84") )
geojson_write( seattle.sp_dorling, file="seattle.sp_dorling.geojson", geometry="polygon" )

The file path is created as shown below:

<geojson-file>
  Path:       seattle.sp_dorling.geojson
  From class: SpatialPolygonsDataFrame

I try to pull the path in the dashboard like this:

# DATA STEPS 

library( geojsonio )
library( sp )
library( tmap ) 

seattle.sp <- geojson_read("data/seattle.sp_dorling.geojson", what="sp" )

plot( seattle.sp )

I keep getting the error : Error: File does not exist. Create it, or fix the path.

I'm not sure what I need to do to pull in the path. I changed the work directory to file path and that didn't seem to do it either. Is there something I'm missing?

qaltimee commented 7 months ago

I figured out, I was not using the right path. thank you!

qaltimee commented 7 months ago

I am having an issue with the map showing up in the Choropleth Tab, similar to the Choropleth Map issue. I checked the bounding box and it matches with Seattle. here's my code:

renderPlot({

# split the selected variable into deciles 

get_data <- 
  reactive({
             seattle.sf <- 
             seattle.sf %>% 
             mutate( q = ntile( get(input$demographics), 10 ) )  
          })

ggplot( get_data() ) +
    geom_sf( aes( fill = q ), color=NA ) +
    coord_sf( datum=NA ) +
    labs( title = paste0( "Choropleth of Select Demographics: ", toupper(input$demographics) ),
          caption = "Source: Harmonized Census Files",
          fill = "Population Deciles" ) +
    scale_fill_gradientn( colours=rev(ocean.phase(10)), guide = "colourbar" ) + 
    xlim( xmin = -122.48, xmax = -122.16 ) + 
    ylim( ymin = 47.41, ymax = 47.79 )

})

When I run the document, I get Error: [Object Object] Screenshot 2024-04-21 220004

AntJam-Howell commented 7 months ago

Hi can you send me your rmd file? Also are you doing San Diego or Seattle?

-- Anthony Howell Director, Center for Technology, Data, and Society, Associate Professor, Public Policy Arizona State University

On Sun, Apr 21, 2024 at 7:01 PM qaltimee @.***> wrote:

I am having an issue with the map showing up in the Choropleth Tab, similar to the Choropleth Map https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-spr-2021/issues/34l__;!!IKRxdwAv5BmarQ!ZBA_5dkGRx6ksEvtxFjokXunwo_-34DjPX62XGZLqJc3np0mzLRM-crXIYSnFmfwvGwgOffRa6w4HqavPP3bydgvP7WCbw$ issue. I checked the bounding box and it matches with Seattle. here's my code:

renderPlot({

split the selected variable into deciles

get_data <- reactive({ seattle.sf <- seattle.sf %>% mutate( q = ntile( get(input$demographics), 10 ) ) })

ggplot( get_data() ) + geom_sf( aes( fill = q ), color=NA ) + coord_sf( datum=NA ) + labs( title = paste0( "Choropleth of Select Demographics: ", toupper(input$demographics) ), caption = "Source: Harmonized Census Files", fill = "Population Deciles" ) + scale_fill_gradientn( colours=rev(ocean.phase(10)), guide = "colourbar" ) + xlim( xmin = -122.48, xmax = -122.16 ) + ylim( ymin = 47.41, ymax = 47.79 )

})

When I run the document, I get Error: [Object Object] Screenshot.2024-04-21.220004.png (view on web) https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-spr-2021/assets/167607966/699b9928-8d50-46b3-9591-7ed8e2a296c8__;!!IKRxdwAv5BmarQ!ZBA_5dkGRx6ksEvtxFjokXunwo_-34DjPX62XGZLqJc3np0mzLRM-crXIYSnFmfwvGwgOffRa6w4HqavPP3bydiL6qMPeA$

— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-spr-2021/issues/34*issuecomment-2068493397__;Iw!!IKRxdwAv5BmarQ!ZBA_5dkGRx6ksEvtxFjokXunwo_-34DjPX62XGZLqJc3np0mzLRM-crXIYSnFmfwvGwgOffRa6w4HqavPP3bydgXzGSsJw$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AMK2Y7ZXKBPCRZCXRRK5FPDY6SKTJAVCNFSM6AAAAABGQJKLSOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRYGQ4TGMZZG4__;!!IKRxdwAv5BmarQ!ZBA_5dkGRx6ksEvtxFjokXunwo_-34DjPX62XGZLqJc3np0mzLRM-crXIYSnFmfwvGwgOffRa6w4HqavPP3bydjqtzZfow$ . You are receiving this because you were mentioned.Message ID: @.***>

qaltimee commented 7 months ago

Hi Professor, I just emailed you my RMD files. I'm doing Seattle.

AntJam-Howell commented 7 months ago

Hi, so i wont be able to help specifics with Seattle as mentioned in canvas, since we are focusing on San Diego. But it appears that some of your coordinates are incorrect:

bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395"))

xlim( xmin = -122.48, xmax = -122.16 ) +
ylim( ymin = 47.41, ymax = 47.79 )

First, i would eliminate the bb form the following code (and let R automatically place the map):

tmap_mode("view") tm_basemap( "CartoDB.Positron" ) tm_shape( seattle.sf, bbox=bb ) + tm_polygons( col="cluster", palette="Accent", title="Community Types" )

Make sure that the other coordinates (pasted again below) point to Seattle and not somewhere else using either locator function() for PC or website if using a Mac (link http://bboxfinder.com/#0.000000,0.000000,0.000000,0.000000)

xlim( xmin = -122.48, xmax = -122.16 ) +
ylim( ymin = 47.41, ymax = 47.79 )

best

On Mon, Apr 22, 2024 at 4:14 PM qaltimee @.***> wrote:

Hi Professor, I just emailed you my RMD files. I'm doing Seattle.

— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-spr-2021/issues/34*issuecomment-2071104894__;Iw!!IKRxdwAv5BmarQ!ZMpo9wDqpEJEj03zmhdHIKdUwtZyXh9p3kcFkbEppI6YBX5_8iAauWrVjTngJFoADMRbZ9H_gAEzqahPWQPszwPqpqfmrg$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AMK2Y73LSQTPNKDNESCF2J3Y6WKWZAVCNFSM6AAAAABGQJKLSOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZRGEYDIOBZGQ__;!!IKRxdwAv5BmarQ!ZMpo9wDqpEJEj03zmhdHIKdUwtZyXh9p3kcFkbEppI6YBX5_8iAauWrVjTngJFoADMRbZ9H_gAEzqahPWQPszwP0XXfcpA$ . You are receiving this because you were mentioned.Message ID: @.***>

-- Anthony Howell (Website https://anthonyhowell.netlify.app/) Director, Center on Technology, Data, and Society, Associate Professor, Public Policy Arizona State University

qaltimee commented 7 months ago

Hi Professor, thank you for getting back to me. To limit the confusion, I went ahead and used San Diego. I am having issues with transferring San Diego cartogram to the dashboard. Here's my code for the cartogram:


library( geojsonio )   # read shapefiles
library( sp )          # work with shapefiles
library( sf )          # work with shapefiles - simple features format
library( mclust )      # cluster analysis 
library( tmap )        # theme maps
library( ggplot2 )     # graphing 
library( ggthemes )    # nice formats for ggplots
library( dplyr )       # data wrangling 
library( pander )      # formatting RMD tables
library( tidycensus )
library( cartogram )  # spatial maps w/ tract size bias reduction
#library( maptools )   # spatial object manipulation 
library( corrplot )   # correlation plots 

# clear the workspace
rm( list = ls() )

Part 1

Step 1: Identifying San Diego data


crosswalk <- read.csv( "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv",  stringsAsFactors=F, colClasses="character" )

grep( "^SAN DIEGO", crosswalk$msaname, value=TRUE ) 

these.msp <- 
  crosswalk$msaname == grep( "^SAN DIEGO", crosswalk$msaname, value=TRUE ) 

these.fips <- crosswalk$fipscounty[ these.msp ]
these.fips <- na.omit( these.fips )

Step 2: Download a Shapefile with Population Data


# set the api key
census_api_key( "1bddc538fabf1ff114f21a562749825df2b7f7fd" )

state.fips <- substr( these.fips, 1, 2 )
county.fips <- substr( these.fips, 3, 5 )

cbind( these.fips, state.fips, county.fips ) %>% pander()

# get the san diego data
sd.pop <- get_acs( 
  geography = "tract", 
  variables = "B01003_001", 
  state = state.fips, 
  county = county.fips,
  geometry = TRUE ) %>% 
  select( GEOID, estimate ) %>%
  rename( POP=estimate )

# recode the GEIOD variable to conform with the census data
# remove the leading zero
sd.pop$GEOID<-sub( ".","", sd.pop$GEOID )

Step 3: Add Census Data


URL <- "https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds"
census.dat <- readRDS( gzcon( url( URL ) ) )

# merge the pop data for san diego with the census data
sdd <- merge( sd.pop, census.dat, by.x="GEOID", by.y="tractid" )

# make sure there are no empty polygons
sdd <- sdd[ ! st_is_empty( sdd ) , ]

Step 4: Transform the Shapefile into a Dorling Cartogram


# convert sf map object to an sp version
sdd.sp <- as_Spatial( sdd )

class( sdd.sp )

plot( sdd.sp )

# project map and remove empty tracts
sdd.sp <- spTransform( sdd.sp, CRS( "+init=epsg:3395" ) )
sdd.sp <- sdd.sp[ sdd.sp$POP != 0 & (! is.na( sdd.sp$POP ) ) , ]

# Save Dorling To File 
library( geojsonio )
#geojson_write( sdd.sp, file="San_Diego.geojson", geometry="polygon" )

sdd.sp$pop.w <- sdd.sp$POP / 10000   # standardizes it to max of 1.5
sd_dorling <- cartogram_dorling( x=sdd.sp, weight="pop.w", k=0.03 )
plot( sd_dorling )
tm_shape( sd_dorling ) + 
  tm_polygons( 
    size="POP", col="hinc12", n=7, style="quantile", palette="Spectral" ) + 
   tm_layout( "Dorling Cartogram \nof Household Income \nfor San Diego", 
              title.position=c( "right","top" ) )
library( geojsonio )

sdd.sp <- spTransform( sd_dorling, CRS("+proj=longlat +datum=WGS84") )
geojson_write( sd_dorling, file="sd_dorling.geojson", geometry="polygon" )

Once I upload the link to the dashboard, the map shows up gray. I'm not sure if I'm missing anything on my cartogram. I tried uploading it to Github but all I get is the full map.

AntJam-Howell commented 7 months ago

Hi, I will sent you an email with RMD file. best

On Wed, Apr 24, 2024 at 12:18 AM qaltimee @.***> wrote:

Hi Professor, thank you for getting back to me. To limit the confusion, I went ahead and used San Diego. I am having issues with transferring San Diego cartogram to the dashboard. Here's my code for the cartogram:

library( geojsonio ) # read shapefiles library( sp ) # work with shapefiles library( sf ) # work with shapefiles - simple features format library( mclust ) # cluster analysis library( tmap ) # theme maps library( ggplot2 ) # graphing library( ggthemes ) # nice formats for ggplots library( dplyr ) # data wrangling library( pander ) # formatting RMD tables library( tidycensus ) library( cartogram ) # spatial maps w/ tract size bias reduction

library( maptools ) # spatial object manipulation

library( corrplot ) # correlation plots


```{r}

# clear the workspace
rm( list = ls() )

Part 1

Step 1: Identifying San Diego data


crosswalk <- read.csv( "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv <https://urldefense.com/v3/__https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv__;!!IKRxdwAv5BmarQ!ZWBYKdk_DHpFv_93QCdvMKZ6VKCKTStA_41tZH_tCNFg2mzidtUnO3UbKve6z7TxWxMaWZo4diGGP9sNzYgNfzGFKpaE8Q$>",  stringsAsFactors=F, colClasses="character" )

grep( "^SAN DIEGO", crosswalk$msaname, value=TRUE )

these.msp <-
  crosswalk$msaname == grep( "^SAN DIEGO", crosswalk$msaname, value=TRUE )

these.fips <- crosswalk$fipscounty[ these.msp ]
these.fips <- na.omit( these.fips )

Step 2: Download a Shapefile with Population Data


# set the api key
census_api_key( "1bddc538fabf1ff114f21a562749825df2b7f7fd" )

state.fips <- substr( these.fips, 1, 2 )
county.fips <- substr( these.fips, 3, 5 )

cbind( these.fips, state.fips, county.fips ) %>% pander()

# get the san diego data
sd.pop <- get_acs(
  geography = "tract",
  variables = "B01003_001",
  state = state.fips,
  county = county.fips,
  geometry = TRUE ) %>%
  select( GEOID, estimate ) %>%
  rename( POP=estimate )

# recode the GEIOD variable to conform with the census data
# remove the leading zero
sd.pop$GEOID<-sub( ".","", sd.pop$GEOID )

Step 3: Add Census Data


URL <- "https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds <https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds__;!!IKRxdwAv5BmarQ!ZWBYKdk_DHpFv_93QCdvMKZ6VKCKTStA_41tZH_tCNFg2mzidtUnO3UbKve6z7TxWxMaWZo4diGGP9sNzYgNfzG0h3zVuA$>"
census.dat <- readRDS( gzcon( url( URL ) ) )

# merge the pop data for san diego with the census data
sdd <- merge( sd.pop, census.dat, by.x="GEOID", by.y="tractid" )

# make sure there are no empty polygons
sdd <- sdd[ ! st_is_empty( sdd ) , ]

Step 4: Transform the Shapefile into a Dorling Cartogram


# convert sf map object to an sp version
sdd.sp <- as_Spatial( sdd )

class( sdd.sp )

plot( sdd.sp )

# project map and remove empty tracts
sdd.sp <- spTransform( sdd.sp, CRS( "+init=epsg:3395" ) )
sdd.sp <- sdd.sp[ sdd.sp$POP != 0 & (! is.na( sdd.sp$POP ) ) , ]

# Save Dorling To File
library( geojsonio )
#geojson_write( sdd.sp, file="San_Diego.geojson", geometry="polygon" )

sdd.sp$pop.w <- sdd.sp$POP / 10000   # standardizes it to max of 1.5
sd_dorling <- cartogram_dorling( x=sdd.sp, weight="pop.w", k=0.03 )
plot( sd_dorling )
tm_shape( sd_dorling ) +
  tm_polygons(
    size="POP", col="hinc12", n=7, style="quantile", palette="Spectral" ) +
   tm_layout( "Dorling Cartogram \nof Household Income \nfor San Diego",
              title.position=c( "right","top" ) )

library( geojsonio )

sdd.sp <- spTransform( sd_dorling, CRS("+proj=longlat +datum=WGS84") )
geojson_write( sd_dorling, file="sd_dorling.geojson", geometry="polygon" )

Once I upload the link to the dashboard, the map shows up gray. I'm not
sure if I'm missing anything on my cartogram. I tried uploading it to
Github but all I get is the full map.

—
Reply to this email directly, view it on GitHub
<https://urldefense.com/v3/__https://github.com/DS4PS/cpp-529-spr-2021/issues/34*issuecomment-2074244124__;Iw!!IKRxdwAv5BmarQ!ZWBYKdk_DHpFv_93QCdvMKZ6VKCKTStA_41tZH_tCNFg2mzidtUnO3UbKve6z7TxWxMaWZo4diGGP9sNzYgNfzF9B9tMPQ$>,
or unsubscribe
<https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AMK2Y77HD5MX4FMJNHT4JZTY65MDPAVCNFSM6AAAAABGQJKLSOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZUGI2DIMJSGQ__;!!IKRxdwAv5BmarQ!ZWBYKdk_DHpFv_93QCdvMKZ6VKCKTStA_41tZH_tCNFg2mzidtUnO3UbKve6z7TxWxMaWZo4diGGP9sNzYgNfzGzUDiS1A$>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>

-- Anthony Howell (Website https://anthonyhowell.netlify.app/) Director, Center on Technology, Data, and Society, Associate Professor, Public Policy Arizona State University

qaltimee commented 7 months ago

thank you, Professor, it helped get things back in line.

I am running into an issue with the home. value input on the medium home values tab. I'm not sure what I'm missing but when I run the document I get a Error:[Object Object]. I believe there was another question posted asking the same thing but when I switch out home.value for demographics it works, so I'm not sure if home.value is causing an issue.

`renderPlot({

  # split the selected variable into deciles 

get_data <- 
  reactive({
             sd.sf <- 
             sd.sf %>% 
             mutate( q = ntile( get(input$home.value), 10 ) )  
          })

ggplot( get_data() ) +
    geom_sf( aes( fill = q ), color=NA ) +
    coord_sf( datum=NA ) +
    labs( title = paste0( "Spatial Distribution of Home Values: ", toupper(input$demographics) ),
          caption = "Source: Harmonized Census Files",
          fill = "Home Value Deciles" ) +
    scale_fill_gradientn( colours=rev(ocean.balance(10)), guide = "colourbar" ) + 
    xlim( xmin = -117.611081, xmax = -116.08094 ) + 
    ylim( ymin = 32.528832, ymax = 33.505025 )

})
`
AntJam-Howell commented 7 months ago

Hi, you should have the following code that mutates the demographics, not home.value.

radioButtons( inputId="demographics", 
              label = h3("Census Variables"),
              choiceNames=var.names,
              choiceValues=these.variables,
              selected="pnhwht12")

renderPlot({

# split the selected variable into deciles 

get_data <- 
  reactive({
             phx.sf <- 
             phx.sf %>% 
             mutate( q = ntile( get(input$demographics), 10 ) )  
          })

ggplot( get_data() ) +
    geom_sf( aes( fill = q ), color=NA ) +
    coord_sf( datum=NA ) +
    labs( title = paste0( "Choropleth of Select Demographics: ", 
                          toupper( input$demographics ) ),
          caption = "Source: Harmonized Census Files",
          fill = "Population Deciles" ) +
    scale_fill_gradientn( 
      colours=rev( ocean.balance( 10 ) ), guide = "colourbar" ) + 
    xlim( xmin = -13071745, xmax = -12980948 ) + 
    ylim( ymin = 3811797, ymax = 3931462 )

})