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 ) add_two <- function( x ) { x.plus.2 <- x + 2 return( x.plus.2 ) } add_two( 3 ) add_two( 10 ) add_two( ) add_two <- function( x=5 ) { x.plus.2 <- x + 2 return( x.plus.2 ) } add_two( ) add_two( x=3 ) add_two( x=10 ) 5 -> x x x = 7 x calcMortgage <- function( principal, years=30, APR=0.05 ) { months <- years * 12 # covert years to months int.rate <- APR / 12 # convert annual rate to monthly # amortization formula monthly.payment <- ( principal * int.rate ) / (1 - (1 + int.rate)^(-months) ) monthly.payment <- round( monthly.payment, 2 ) return( monthly.payment ) } plot( rnorm(10), rnorm(10) ) ls() 1:10 > "5" add_two <- function( x=5 ) { x.plus.2 <- x + 2 x.plus.2 } add_two( x=3 ) add_two <- function( x=5 ) { x + 2 } add_two( x=3 ) add_two <- function( x=5 ) { x.plus.2 <- x + 2 } add_two( x=3 ) x.plus.2 x add_two <- function( x=5 ) { x.plus.2 <- x + 2 return( NULL ) } add_two( x=3 ) calcMortgage <- function( principal, years=30, APR=0.05 ) { months <- years * 12 # covert years to months int.rate <- APR / 12 # convert annual rate to monthly # amortization formula monthly.payment <- ( principal * int.rate ) / (1 - (1 + int.rate)^(-months) ) monthly.payment <- round( monthly.payment, 2 ) return( monthly.payment ) } ls() x class( x ) class( add_two ) arguments( add_two ) argument( add_two ) add_two calcMortgage( principal=100000, years=15, APR=0.03 ) calcMortgage( principal=100000 ) calcMortgage( 100000, 15, 0.03 ) calcMortgage( 100000, 0.03, 15 ) calcMortgage( principal=100000, 15, 0.03 ) calcMortgage( 15, 100000, 0.03 ) calcMortgage( 15, principal=100000, 0.03 ) calcMortgage( y=15, princ=100000, A=0.03 ) calcMortgage( y=15, princ=100000, a=0.03 ) calcMortgage( y=15, principle=100000, A=0.03 ) args( calcMortgage ) add_two <- function( x=5 ) { x <- x + 2 return( x ) } add_two( ) x <- 12 add_two <- function( x=5 ) { x <- x + 2 return( x ) } add_two( ) x ls() x x x <- add_two( ) x x ls() args( function_name )