Skip to content

Instantly share code, notes, and snippets.

@jobonaf
Created October 3, 2017 06:26
Show Gist options
  • Save jobonaf/d8d60b4c64400dc08f563d370c8ebfd4 to your computer and use it in GitHub Desktop.
Save jobonaf/d8d60b4c64400dc08f563d370c8ebfd4 to your computer and use it in GitHub Desktop.
plotting the sketch: regional background + urban background + hotspots concentration
# libraries
library(ggplot2)
library(dplyr)
# where is the urban area?
urban <- c(rep(F,100),rep(T,100),rep(F,100))
# regional background concentrations
noise <- runif(300,min=5, max = 15)
regional.background <- stats::filter(noise,rep(1/40,40), sides=2, circular = T)
regional.background <- stats::filter(regional.background,rep(1/40,40), sides=2, circular = T)
regional.background <- stats::filter(regional.background,rep(1/40,40), sides=2, circular = T)
# urban background
noise <- runif(300,min=2, max = 10)*urban
urban.background <- stats::filter(noise,rep(1/20,20), sides=2, circular = T)
urban.background <- stats::filter(urban.background,rep(1/20,20), sides=2, circular = T)
# concentration in the local hotspots
noise <- runif(300,min=-10, max = 10)*urban
hotspot <- stats::filter(noise,rep(1/3,3), sides=2, circular = T)
hotspot <- stats::filter(hotspot,rep(1/3,3), sides=2, circular = T)
hotspot <- as.ts(pmax(0,hotspot))
# the data frame
df <- bind_rows(data.frame(concentration=regional.background,
contribution="regional background",
x=1:300),
data.frame(concentration=urban.background,
contribution="urban background",
x=1:300),
data.frame(concentration=hotspot,
contribution="hotspot",
x=1:300))
df$contribution <- ordered(df$contribution, c("hotspot",
"urban background",
"regional background"))
# plotting
p <- ggplot(df, aes(x=x, y=concentration)) +
geom_area(aes(colour = contribution, fill= contribution),
position = 'stack', alpha=0.7) +
scale_color_manual("",values=c("sienna","navyblue","olivedrab")) +
scale_fill_manual("",values=c("sienna","navyblue","olivedrab")) +
theme_classic() +
scale_x_continuous(name="", breaks=c(50*(1:5)),
labels=c("rural","","urban","","rural")) +
theme(axis.ticks.x = element_line(color = c(NA,"black",NA,"black",NA)))
ggsave(p, filename = "reg-urb-loc.pdf", width = 7, height=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment