*Photo by Ahnaf Piash from Pexels*
Recently, I was asked to work with some data on Kingston, Ontario neighbourhoods from the Open Data Kingston portal. Specifically, the Neighbourhood Census Profiles - Income, Occupation, & Education dataset. I’ve linked both the portal and dataset in case you feel like checking them out.
In doing a basic descriptive analysis I put together a rudimentary Shiny app that showed some counts for information like methods of commuting, fields of study, and education levels for the various Kingston neighbourhoods. Since then, I’ve played with the app a bit and gotten it more to my liking.
There were a few things that I wanted to do with this app. First, Kingston has approximately 40 neighbourhoods and I wanted to display information for each one. So for each of the various topics that would be 40-odd charts, which would be a bit of a slog to present and sit through. The app made presenting this information easier as it allows for each of the neighbourhoods to be selectable while only presenting one at a time. Second, I wanted to be able to display each of the categories. Third, at the last minute I decided I wanted to be able to compare neighbourhoods.
In making this Shiny app I came across some new things. First, is the plotly
package. This is a great package that creates interactive plots and provides a wrapper (ggplotly()
) for ggplot2
plots to make them interactive. I’ll hopefully get around to doing a write up on the package this holiday season, but I also recommend checking it out and playing around with it. There’s plenty of documentation out there. Second, is using selectizeInput
vs. selectInput
. The latter required having two individual dropdown menus, whereas the former allowed me to eliminate the second dropdown menu and keep the selection options just to one menu. Lastly, I had to figure out how to ensure the colours on the plot stayed consistent. For instance, if I had only the Sydenham neighbourhood selected the points would be in blue. If I then added Williamsville, then Williamsville would show up in red. However, if I selected a neighbourhood that was alphabetically before Sydenham, like Alwington, then the colours would reorder such that Sydenham would become red and Alwington would be blue. This is because ggplot
would automatically reorder the neighbourhoods alphabetically. I couldn’t set a factor order for the values in the dataset as the order was dependent on the selection and not the data. So, the solution was to pass the selectizeInput
as a vector to the scale_colour_manual()
aesthetic function in the ggplot
chart.
There’s still a few things that don’t play well, for instance I can’t get the plotly
legend to stop reordering alphabetically but the colours stay consistent atleast, but I’m happy with the progress. Check out the app below and explore some information about Kingston, ON neighbourhoods.
Some points on the app: hovering over a point will provide information on that point, and to change the selected neighbourhoods just delete one of the set neighbourhoods and a list of neighbourhoods will pop up.