Skip to content

Instantly share code, notes, and snippets.

@MatthewSchumwinger
Created February 7, 2014 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewSchumwinger/8870864 to your computer and use it in GitHub Desktop.
Save MatthewSchumwinger/8870864 to your computer and use it in GitHub Desktop.
R snippet for color histogram is used in Milwaukee building ages map
library(ggplot2)
library(grid)
## create bins for color legend
s.data$YR_BUILT <- as.numeric(s.data$YR_BUILT)
bin1 <- s.data$TAXKEY %in% subset(s.data, YR_BUILT < 1897)$TAXKEY
s.data$bin[bin1] <- "< 1897"
bin2 <- s.data$TAXKEY %in% subset(s.data, YR_BUILT >= 1897 & YR_BUILT < 1935)$TAXKEY
s.data$bin[bin2] <- "1897-1935"
bin3 <- s.data$TAXKEY %in% subset(s.data, YR_BUILT >= 1935 & YR_BUILT < 1945)$TAXKEY
s.data$bin[bin3] <- "1935-1945"
bin4 <- s.data$TAXKEY %in% subset(s.data, YR_BUILT >= 1945 & YR_BUILT < 1965)$TAXKEY
s.data$bin[bin4] <- "1945-1965"
bin5 <- s.data$TAXKEY %in% subset(s.data, YR_BUILT >= 1965 & YR_BUILT < 1995)$TAXKEY
s.data$bin[bin5] <- "1965-1995"
bin6 <- s.data$TAXKEY %in% subset(s.data, YR_BUILT >= 1995)$TAXKEY
s.data$bin[bin6] <- "1995+"
table(s.data$bin)
s.data$bin <- as.factor(s.data$bin)
names(s.data)
names(s.data)[13] <- "era"
## plot historgram
p <- qplot(YR_BUILT,data=s.data,geom="histogram",fill=era, binwidth = 1,
xlab = "",ylab="# parcels", main="Milwaukee's building stock, colored by construction date")
p + scale_fill_manual(values = c("#8E0325", "#F26B43", "#ffcc87", "#add4ff", "#45b3ff","#4A5AD9")) +
theme(
panel.background = element_rect(fill = "black",colour = NA),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
plot.background = element_rect(fill = "black",colour = NA),
plot.title = element_text(size=14, vjust=1.5),
text=element_text(colour="white"),
axis.text.x = element_text(colour = 'white'),
axis.text.y = element_text(colour = 'white'),
axis.ticks = element_line(colour = 'white'),
legend.position = "none",
axis.title.y = element_text(angle=360, vjust=1.025, hjust=5,size=11),
plot.margin = unit(c(.5,0,0,-1.5),"cm")
) +
scale_x_continuous(breaks=c(1875, 1897, 1935, 1945, 1965,1995,2014), limits=c(1875,2015))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment