Watts-College / cpp-529-spr-2022

https://watts-college.github.io/cpp-529-spr-2022/
0 stars 2 forks source link

Issue with the Choropleth Map #30

Open jmacost5 opened 2 years ago

jmacost5 commented 2 years ago

@JasonSills @u12345 I think we may have went over this yesterday's lab but it isnt posted yet so I wanted to ask this question. I am having trouble with the Choropleth Map and I am assuming it has to possibly do with the 'input$demographics'. What I am confused/wondering is that is the "x[ input$demographics ] which has to do with the information from the community demographics?

x <- dd.name
names(x) <- value
temp.names <- paste0( dd.name )
radioButtons( inputId="demographics", 
              label = h3("Census Variables"),
              # choices = these.variables, 
              choiceNames=temp.names,
              choiceValues=these.variables,
              selected="pnhwht12")
x[ input$demographics ]
jmacost5 commented 2 years ago

@JasonSills I just added it into the new issue but the error I get its

x[ input$demographics ] Error: object 'input' not found

JasonSills commented 2 years ago

You don't need the line of code in your radiobuttons code chunk:

x[ input$demographics ]

This is the code used to create radio buttons: x <- dd.name

names(x) <- value

temp.names <- paste0( dd.name )

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

And the "demographics" is referenced in the renderPlot function in this line:

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

jmacost5 commented 2 years ago

@JasonSills I guess my question was more about if this is linked to the two maps that are not being displayed. I guess it was more of a conceptual question of is that linked to the choropleth map, or is it something else in the code not being displayed, or is something missing from code from the previous lab

JasonSills commented 2 years ago

The error is returned because the code x[ input$demographics ] does not work here. If I take a fully built out, fully functional dashboard with completed code and run:

x <- dd.name names(x) <- value temp.names <- paste0( dd.name ) radioButtons( inputId="demographics", label = h3("Census Variables"),

choices = these.variables,

          choiceNames=temp.names,
          choiceValues=these.variables,
          selected="pnhwht12")

x[ input$demographics ]

I will receive the same error you received. Remove this line of code and the error will go away.

If you are unable to get your maps to work that is a separate issue. If you remove this line of code what do your maps or the areas where your map should be look like?

jmacost5 commented 2 years ago

@JasonSills I do not have any maps, that is what I was basically trying to understand, that it why I put x[ input$demographics ] in there to see if it will get the map to work. I guess I want to know if this is the right idea for the map, or am i still not close get_data <- reactive({ slc.sf <- slc.sf %>% mutate( q = ntile( get(input$demographics), 10 ) ) mutate(variable=case_when( variable=="mhv.00" ~ "Median Home Value 2000", variable=="mhv.10" ~ "Median Home Value 2010", variable=="mhv.change" ~ "Value Change 2000-2010" variable=="mhv.growth" ~ "Growth in Home Value")) %>% select(-moe) %>%
spread(variable, estimate) #Spread moves rows into columns })

JasonSills commented 2 years ago

@jmacost5

You don't need to manipulate or add anything to this section of code to get the maps to work. I recommend re-watching the lab sessions and the videos posted in the course shell to help clarify how the code build is working.

To update the maps you need to update the sections for the radio buttons. For example, the code below needs to be updated with the correct variable names. The inputId is then referenced in the map (in your example this was "demographics" here it is "home.value":


button.labels <- c("Median Home Value 2000","Median Home Value 2010","Value Change 2000-2010","Growth in Home Value")
button.values <- c("mhv.2000","mhv.2010","mhv.change","mhv.growth")

radioButtons( inputId="home.value", 
              label = h3("Home Values"),
              # choices = these.variables, 
              choiceNames=button.labels,
              choiceValues=button.values,
              selected="mhv.2000")
JasonSills commented 2 years ago

And, to make sure you have it, there is code in the course shell at this location to get you started (all mapping code is available here): https://raw.githubusercontent.com/DS4PS/cpp-529-fall-2020/main/LABS/nhood-change-dashboard-template.rmd

jmacost5 commented 2 years ago

@JasonSills @u12345 I think that where the issue has to do with the 'slc' in the code because in your example that you posted it is sea, which comes from the rdm that makes the geojson but trying to run that in the geojson it does not find the "sea" at the end of the rdm. so the dots that need to be there arent there and thats where I am getting stuck, because I just get a grey box on the other 2 maps.

Choropleth Map


renderPlot({

# split the selected variable into deciles 

get_data <- 
  reactive({
            slc.sf <- 
            slc.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(brewer.ylorbr(10)), guide = "colourbar" ) + 
    xlim( xmin = -12333000, xmax = -12533000 ) + 
    ylim( ymin = 5034000, ymax = 4934000 )

})

#xmin = -12333000, xmax = -12533000, 
#ymax = 4934000, ymin = 5034000

Median Home Values


renderPlot({

# split the selected variable into deciles 

get_data <- 
  reactive({
             slc.sf <- 
             cls.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( "Spatial Distribution of Home Values: ", toupper(input$demographics) ),
          caption = "Source: Harmonized Census Files",
          fill = "Home Value Deciles" ) +
    scale_fill_gradientn( colours=rev(brewer.ylorbr(10)), guide = "colourbar" ) + 
    xlim( xmin = -12333000, xmax = -12533000 ) + 
    ylim( ymax = 4934000, ymin = 5034000 )

})
#xmin = -12333000, xmax = -12533000, 
#ymax = 4934000, ymin = 5034000
JasonSills commented 2 years ago

The slc versus sea is only a naming convention. This should be universal in your code. You have slc because you are analyzing Salt Lake City. I have used code for three different cities as examples: in the lectures we are looking at Jackson, MS so I used jax, in the code I provided it was looking at sea, short for Seattle. In the link provided it was phx, short for Phoenix. This is merely referencing the code created in the earlier code chunks where you create .sf (see code below for that code chunk). We could name this anything, as long as it is consistent throughout the code and is .sf. In the example I provided you would update the "sea" with "slc". Note that one of your "slc"s above is "cls".


# DATA STEPS 

# 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 )
jmacost5 commented 2 years ago

@JasonSills it has always been slc, that was the way I wrote it in the code, but I think that's the issue because looking at your code that is the only discrepancy between the codes is the name that I see, even looking at the comparison for the geojson file that is the only thing I see that is the difference, the sea Screen Shot 2022-04-30 at 12 51 08 PM

JasonSills commented 2 years ago

@jmacost5

Double check your values in ymin and ymax. I see this in your code ( ymax = 4934000, ymin = 5034000 ). I think these are reversed. ymax should be larger than ymin. Double check your placement of xmin and xmax as well. I searched the coordinates of SLC and found this:

image
jmacost5 commented 2 years ago

@JasonSills I cant believe I didnt notice that, but the dots still are not loading onto the map, the map does look better though Screen Shot 2022-04-30 at 3 16 35 PM (2)

Median Home Values


renderPlot({

# split the selected variable into deciles 

get_data <- 
  reactive({
             slc.sf <- 
             slc.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( "Spatial Distribution of Home Values: ", toupper(input$demographics) ),
          caption = "Source: Harmonized Census Files",
          fill = "Home Value Deciles" ) +
    scale_fill_gradientn( colours=rev(brewer.ylorbr(10)), guide = "colourbar" ) + 
    xlim( xmax = -12300000, xmin = -12600000 ) + 
    ylim( ymax = 5050000, ymin = 4900000 )

})

Choropleth Map


renderPlot({

# split the selected variable into deciles 

get_data <- 
  reactive({
            slc.sf <- 
            slc.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(brewer.ylorbr(10)), guide = "colourbar" ) + 
    xlim(  xmax =-12300000, xmin = -12600000 ) + 
    ylim( ymin = 4900000, ymax = 5050000 )

})

#xmin = -12300000, xmax = -12600000
#ymax = 5050000, ymin = 4900000
JasonSills commented 2 years ago

@jmacost5

Try placing them in order so you have xmin then xmax, ymin then ymax like this (you should be able to copy and paste these lines in): xlim( xmin = -12533000, xmax = -12333000 ) + ylim( ymin = 4934000, ymax = 5034000 )

jmacost5 commented 2 years ago

@JasonSills That was the issue, now I have to get the radio buttons to work in median home values and variable distribution

JasonSills commented 2 years ago

@jmacost5 , Great! I will let you tackle that one. Please re-watch the two most recent videos, I cover some important points in there about the radio buttons. If you need additional help please create a new thread.

Happy coding!