tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="-inferno" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tmap_mode("view") tmap_style("cobalt") tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "Stamen.Watercolor" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "Humanitarian" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "Stamen.Watercolor" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "Stamen.Watercolor" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="-inferno" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "Stamen.Watercolor" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Spectral" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tmap_style("gray") tm_shape( phx2, bbox=bb ) + tm_polygons( col="mhmval12", palette="-inferno" ) tm_basemap( "gray" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Spectral" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "CartoDB.Positron" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Spectral" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "CartoDB.Positron" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=5, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) tm_basemap( "CartoDB.Positron" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=7, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395")) tmap_mode("view") tmap_style("cobalt") tm_basemap( "CartoDB.Positron" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=7, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) "Accent" tmap_mode("view") tmap_style("gray") tm_shape( phx2, bbox=bb ) + tm_polygons( col="mhmval12", palette="-inferno" ) phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395")) renderLeaflet({ tmap_mode("view") # tmap_style("cobalt") tm_basemap( "CartoDB.Positron" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col="pnhwht12", n=7, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) }) # "Accent" # # tmap_mode("view") # tmap_style("gray") # tm_shape( phx2, bbox=bb ) + # tm_polygons( col="mhmval12", palette="-inferno" ) # library( tmap ) phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395")) get_var_name <- reactive({ input$demographics }) renderTmap({ tmap_mode("view") # tmap_style("cobalt") tm_basemap( "CartoDB.Positron" ) + tm_shape( phx2, bbox=bb ) + tm_polygons( col=get_var_name(), n=7, style="quantile", palette="Accent" ) + tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) }) library( tmap ) phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395")) # renderPrint({ class( input$demographics ) }) get_var <- reactive({ input$demographics }) args( radioButtons ) library( shiny ) args( radioButtons ) github.url <- "https://github.com/DS4PS/cpp-529-fall-2020/raw/main/data/phx_dorling.geojson" phx <- geojson_read( x=github.url, what="sp" ) # Load dorling cartogram from YOUR CITY NAME library( geojsonio ) library( sp ) library( tmap ) # load from github # github.url <- "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/phx_dorling.geojson" github.url <- "https://github.com/DS4PS/cpp-529-fall-2020/raw/main/data/phx_dorling.geojson" phx <- geojson_read( x=github.url, what="sp" ) phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395")) # Load dorling cartogram from YOUR CITY NAME library( geojsonio ) library( sp ) library( sf ) library( tmap ) # load from github # github.url <- "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/phx_dorling.geojson" github.url <- "https://github.com/DS4PS/cpp-529-fall-2020/raw/main/data/phx_dorling.geojson" phx <- geojson_read( x=github.url, what="sp" ) phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395")) phx2 <- spTransform( phx, CRS("init=epsg:3395") ) phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("+init=epsg:3395")) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs("init=epsg:3395")) args( st_crs ) help( st_crs ) phx2 <- spTransform( phx, CRS(init="epsg:3395") ) bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, ymax = 3965924, ymin = 3899074 ), crs = st_crs(init="epsg:3395")) +init= library( pal ) library( pals ) ggplot( phx2 ) + geom_sf( aes(fill = pnhwht12), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: ACS 5-year, 2013-2017", fill = "Price-Income Ratio" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) # dashboard layout, widgets, and uploading library( flexdashboard ) library( shiny ) library( rsconnect ) # data wrangling library( dplyr ) # formatting output library( DT ) library( pander ) library( knitr ) # maps library( ggmap ) library( leaflet ) library( viridis ) ggplot( phx2 ) + geom_sf( aes(fill = pnhwht12), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: ACS 5-year, 2013-2017", fill = "Price-Income Ratio" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) names( phx2 ) ggplot( phx2 ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: ACS 5-year, 2013-2017", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) ggplot( phx2 ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: ACS 5-year, 2013-2017", fill = pnhwht12 ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) d <- phx2@data ggplot( d ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) ggplot( phx ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) names( phx ) ggplot( phx ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = pnhwht12 ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) ggplot( phx ) + geom_sf( aes(fill = "pnhwht12" ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) class( phx2 ) d.sf <- st_as_sf( phx2 ) ggplot( d.sf ) + geom_sf( aes(fill = "pnhwht12" ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) ggplot( d.sf ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) ggplot( d.sf ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) + xlim( xmin = -12519146, xmax = -12421368 ) ggplot( d.sf ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) + xlim( xmin = -12519146, xmax = -12421368 )+ ylim( ymax = 3965924, ymin = 3899074 ) ggplot( d.sf ) + geom_sf( aes(fill = pnhwht12 ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = "pnhwht12" ) + scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) + xlim( xmin = -12519146, xmax = -12421368 )+ ylim( ymin = 3899074, ymax = 3965924 ) d.sf <- d.sf %>% mutate( q=mutate( ntile( input$demographics ) ) ggplot( d.sf ) + d.sf <- d.sf %>% mutate( q=ntile( input$demographics ) ) d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics) ) ) d.sf <- d.sf %>% mutate( q=ntile( pnhwht12, 5 ) ) ) args( ntile ) d.sf <- d.sf %>% mutate( q=ntile( pnhwht12, 5 ) ) d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics), 5 ) ) get_data <- reactive({ d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics), 5 ) ) }) names( d.sf ) class( d.sf$d ) class( d.sf$1 ) class( d.sf$q ) d.sf$q get_data <- reactive({ d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics), 5 ) ) }) ggplot( get_data() ) + geom_sf( aes( fill = q ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = q ) + scale_fill_manual( colours=ocean.balance(10), guide = "colourbar" ) + xlim( xmin = -12519146, xmax = -12421368 ) + ylim( ymin = 3899074, ymax = 3965924 ) library( pals ) d.sf <- st_as_sf( phx2 ) renderPlot({ get_data <- reactive({ d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics), 5 ) ) }) ggplot( get_data() ) + geom_sf( aes( fill = q ), color=NA ) + coord_sf( datum=NA ) + labs( title = "House-Price-to-Income Ratio", caption = "Source: Harmonized Census Files", fill = q ) + scale_fill_manual( colours=ocean.balance(10), guide = "colourbar" ) + xlim( xmin = -12519146, xmax = -12421368 ) + ylim( ymin = 3899074, ymax = 3965924 ) }) # ggplot( d.sf ) + # geom_sf( aes( fill = get(input$demographics) ), color=NA ) + # coord_sf( datum=NA ) + # labs( title = "House-Price-to-Income Ratio", # caption = "Source: Harmonized Census Files", # fill = input$demographics ) + # scale_fill_gradientn( colours=ocean.balance(10), guide = "colourbar" ) + # xlim( xmin = -12519146, xmax = -12421368 ) + # ylim( ymin = 3899074, ymax = 3965924 ) # get_var <- reactive({ input$demographics }) # renderPrint({ get_var() }) # # observe ({ # # output$map <- renderTmap({ # # tmap_mode("view") # tm_basemap( "CartoDB.Positron" ) + # tm_shape( phx2, bbox=bb ) + # tm_polygons( col=input$demographics, n=7, style="quantile", palette="Accent" ) + # tm_layout( "PHX Dorling Cartogram", title.position=c("right","top") ) # # }) # # }) # # tmapOutput("map") # # "Accent" # # tmap_mode("view") # tmap_style("gray") # tm_shape( phx2, bbox=bb ) + # tm_polygons( col="mhmval12", palette="-inferno" ) # table( d.sf$ntile ) table( d.sf$q ) d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics), 5 ) ) d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics), 5 ) ) }) d.sf <- d.sf %>% mutate( q=ntile( get(input$demographics), 5 ) ) ocean.balance(10) class( d.sf ) d <- as.data.frame( d.sf ) class( d ) x <- d[ "pnhwht12" ] class( x ) head( x ) x <- as.numeric( d[ "pnhwht12" ] ) x <- d[ "pnhwht12" ] %>% unlist() class( x ) head( x ) cut.points <- quantile( x, seq( 0, 1, 0.1 ) ) hist( x, col="darkgray", border="white" ) abline( v=cut.points, col="darkred", lty=2, lwd=1.5 ) hist( x, breaks=100, col="darkgray", border="white" ) abline( v=cut.points, col="darkred", lty=2, lwd=1.5 ) hist( x, breaks=100, col="darkgray", border="white" ) abline( v=cut.points, col="darkred", lty=1, lwd=2 ) hist( x, breaks=100, col="darkgray", border="white" ) abline( v=cut.points, col="darkred", lty=3, lwd=2 ) hist( x, breaks=100, col="gray", border="white" ) abline( v=cut.points, col="darkred", lty=3, lwd=2 ) hist( x, breaks=50, col="gray", border="white" ) abline( v=cut.points, col="darkred", lty=3, lwd=2 ) names( phx ) names( phx ) names( phx2 ) # dashboard layout, widgets, and uploading library( flexdashboard ) library( shiny ) library( rsconnect ) # data wrangling library( dplyr ) # formatting output library( DT ) library( pander ) library( knitr ) # maps library( ggmap ) library( leaflet ) library( viridis ) library( geojsonio ) library( sp ) library( sf ) library( tmap ) library( pals ) # load dorling cartogram from github # map already contains census data and groups from clustering github.url <- "https://github.com/DS4PS/cpp-529-fall-2020/raw/main/data/phx_dorling.geojson" phx <- geojson_read( x=github.url, what="sp" ) # reproject the map phx2 <- spTransform( phx, CRS("+init=epsg:3395") ) # convert the sp map format to # an sf (simple features) format: # ggmap requires the sf format phx.sf <- st_as_sf( phx2 ) # separate out the data frame from the map d <- as.data.frame( phx.sf ) names( phx ) stay <- TRUE stay stay == TRUE stay == FALSE ! stay ! FALSE sample( c(1,2,3), size=2 ) sample( c(1,2,3), size=2 ) sample( c(1,2,3), size=2 ) sample( c(1,2,3), size=2 ) args( seq ) seq( 1, 3 ) 1:3 sample( c(3), size=1 ) sample( c(3), size=1 ) sample( c(3), size=1 ) sample( c(3), size=1 ) sample( c(3), size=1 ) sample( c(3), size=1 ) sample( c(3), size=1 ) sample( c(3), size=1 ) source( "https://raw.githubusercontent.com/DS4PS/cpp-527-fall-2020/master/lectures/loop-example.R" ) get_sample_slope( d, n=10 ) test_for_null_slope( d, n=10 ) # returns a one-row data frame test_for_null_slope( d, n=10 ) get_sample_slope get_sample_slope( d, n=10 ) get_sample_slope( d, n=10 ) get_sample_slope( d, n=10 ) get_sample_slope( d, n=10 ) slopes <- NULL # collector vector for( i in 1:1000 ) # iterator i { b1 <- get_sample_slope( d, n=10 ) slopes[ i ] <- b1 } # sampling distribution hist( slopes, breaks=25, col="gray20", border="white" ) slopes <- NULL # collector vector for( i in 1:1000 ) # iterator i { b1 <- get_sample_slope( d, n=50 ) slopes[ i ] <- b1 } # sampling distribution hist( slopes, breaks=25, col="gray20", border="white" ) x <- 1:10 c( x, 44 ) c.vector <- 1:10 c.vector[ 44 ] <- 44 c.vector ls() rm( slopes ) ls() slopes <- 2 slopes rm( slopes ) slopes[ 1 ] <- 2 slopes <- numeric(1000) slopes results <- NULL for( i in 1:50 ) { null.slope.test <- test_for_null_slope( d, n=10 ) results <- rbind( results, null.slope.test ) } plot_ci( df=results ) results test_for_null_slope( d, n=10 ) results <- list() for( i in 1:50 ) { null.slope.test <- test_for_null_slope( d, n=10 ) # results <- rbind( results, null.slope.test ) results[[ i ]] <- null.slope.test } results class( results ) as.data.frame( results ) dplyr::bind_rows( results ) help( t.test )