Last active
October 31, 2016 01:10
-
-
Save yisongtao/79631752cadf3d1c864f9b09a72f3cef to your computer and use it in GitHub Desktop.
plot 1 code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_311 <- readRDS("all_data.RDS") | |
all_complaints <- data_311 %>% select(2, 6, 25) #Select "Complaint Type" and "Borough" columns | |
all_complaints$`Created Date` <- as.POSIXct(strptime(all_complaints$`Created Date`, "%m/%d/%Y %I:%M:%S %p")) | |
all_complaints$Year <- format(all_complaints$`Created Date`, "%Y") | |
all_complaints <- filter(all_complaints, Year != "2016") | |
all_complaints <- filter(all_complaints, Year != "2015") | |
all_complaints <- filter(all_complaints, Year != "2014") | |
all_complaints$`Complaint Type`[grepl("^Noise.*", all_complaints$`Complaint Type`)] <- "Noise" | |
all_complaints <- all_complaints %>% | |
group_by(Borough, `Complaint Type`) %>% summarise(Count = n()) | |
all_complaints_NY <- all_complaints %>% | |
group_by(`Complaint Type`) %>% | |
summarise(Count = sum(Count)) %>% | |
arrange(desc(Count)) | |
top10_complaints_NY <- top_n(all_complaints_NY, 10, Count) | |
#### function below to fix all capitalized words | |
capwords <- function(s, strict = FALSE) { | |
cap <- function(s) paste(toupper(substring(s, 1, 1)), | |
{s <- substring(s, 2); if(strict) tolower(s) else s}, | |
sep = "", collapse = " " ) | |
sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s))) | |
} | |
##### function below to make subplots | |
figure1 <- function(df){ | |
df <- df[order(df$Count),] | |
order <- df$`Complaint Type` | |
order <- sapply(order, capwords, T) | |
df$`Complaint Type` <- order | |
df$`Complaint Type` <- factor(df$`Complaint Type`, levels = order) | |
return(ggplot(df) + geom_bar(aes(x=`Complaint Type` , y = Count | |
), fill = "Orange", | |
stat = "identity") + theme_minimal() + | |
xlab("") + ylab("Number of Complaints") + coord_flip() ) | |
} | |
# subplot 1 | |
p_top10_complaints_NY <- figure1(top10_complaints_NY) + ggtitle("New York City \nTop 10 Complaints 2010-2013") | |
top10_complaints_borough <- top_n(group_by(all_complaints, Borough), 10, Count) | |
top10_complaints_Man <- filter(top10_complaints_borough, Borough == "MANHATTAN") | |
# subplot 2 | |
p_top10_complaints_Man <- figure1(top10_complaints_Man) + ggtitle("Manhattan \nTop 10 Complaints 2010-2013") | |
top10_complaints_Qns <- filter(top10_complaints_borough, Borough == "QUEENS") | |
# subplot 3 | |
p_top10_complaints_Qns <- figure1(top10_complaints_Qns) + ggtitle("Queens \nTop 10 Complaints 2010-2013") | |
top10_complaints_Bn <- filter(top10_complaints_borough, Borough == "BROOKLYN") | |
# subplot 4 | |
p_top10_complaints_Bn <- figure1(top10_complaints_Bn) + ggtitle("Brooklyn \nTop 10 Complaints 2010-2013") | |
top10_complaints_Brx <- filter(top10_complaints_borough, Borough == "BRONX") | |
# subplot 5 | |
p_top10_complaints_Brx <- figure1(top10_complaints_Brx) + ggtitle("Bronx \nTop 10 Complaints 2010-2013") | |
top10_complaints_SI <- filter(top10_complaints_borough, Borough == "STATEN ISLAND") | |
top10_complaints_SI$`Complaint Type`[4] <- "Missed Collection" | |
# subplot 6 | |
p_top10_complaints_SI <- figure1(top10_complaints_SI) + ggtitle("Staten Island \nTop 10 Complaints 2010-2013") | |
#### The following function allos multiple graphs in one plot, the following multiplot | |
#### function is from http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ | |
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {...} | |
################################## | |
################# final plot | |
multiplot(p_top10_complaints_NY, p_top10_complaints_Man, p_top10_complaints_Bn, | |
p_top10_complaints_Qns, p_top10_complaints_Brx, p_top10_complaints_SI, | |
cols=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment