In this blog, I am going to talk about how R’s tidyverse comes handy especially for Ad-hoc reports. I have been using tidyverse for creating ad-hoc reports where I get the raw data from database and create amazing graphs and aggregation with ggplot2 and dplyr. In this example I am going to show how you can create a simple but yet useful graph after aggregating the results. Most of the time data analysts have to make basic barplots and charts to tell the story to the management.
library(tidyverse)
riskiest_roads <- crash_joined %>%
mutate(`Street` = paste(`ON STREET NAME`, Borough_2, sep=" ---- ")) %>%
filter(!is.na(`ON STREET NAME`)) %>%
group_by(Street) %>%
count() %>%
arrange(desc(n))
ggplot(head(riskiest_roads,25), aes(reorder(Street, n), n))+
geom_bar(stat="identity", fill= "steelblue") + coord_flip() +
labs(title="Highest # of Accidents by Street Name", y="Frequencies", x="Street Name") +
geom_text(aes(label=n), vjust=0.4, hjust= 1.2, size=3, color="white")