Skip to content

Instantly share code, notes, and snippets.

@tomhopper
Last active August 29, 2015 14:06
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 tomhopper/a113881cfaacfc8eba89 to your computer and use it in GitHub Desktop.
Save tomhopper/a113881cfaacfc8eba89 to your computer and use it in GitHub Desktop.
Custom labels for ggplot2 facets.
#' Data frame column names are rarely human-readable, concise and clear, but are usually meaningful. Rather
#' than trying to modify the data, we can provide custom labels for facets.
library(data.table)
library(lubridate)
library(reshape2)
library(ggplot2)
#' Download raw data from "Weather Data" at \link{http://datamonitoring.marec.gvsu.edu/DataDownload.aspx},
#' rename the file to "Marec_weather.csv" and save it to /data/ in the current working directory.
#' Load the data
weather <- fread("data/Marec_weather.csv", colClasses = c("Date","Date","numeric","numeric","numeric","numeric","numeric","numeric","numeric"))
#' Tidy the data
weather <- weather %>%
mutate(DateTime = mdy(Date) + hms(Time)) %>%
select(-Date, -Time)
#setnames(weather, colnames(weather), c("Pressure_inHg","Temperature_F","Wind_Speed_ms","Wind_Direction_deg","Humidity_perc","Solar_wm2","Daily_Rain_in","DateTime"))
weather[which(weather$Barometric_Pressure_in_Hg < 1)] <- NA
weather$Temperature_F[which(weather$Temperature_F > 140)] <- NA
weather$Wind_Speed_meter_per_second[which(weather$Wind_Speed_meter_per_second > 90)] <- NA
weather$Wind_Direction_deg[which(weather$Wind_Direction_deg > 30000)] <- NA
weather$Humidity_Percent[which(weather$Humidity_Percent > 100.5)] <- NA
weather$Solar_Radiation_W_per_m2[which(weather$Solar_Radiation_W_per_m2 > 30000)] <- NA
weather$Daily_Rain_in[which(weather$Daily_Rain_in > 30000)] <- NA
weather_m <- weather %>%
melt(., id.vars = "DateTime") %>%
na.omit()
#' Map between column names and desired facet labels
facet_names <- list(
"Barometric_Pressure_in_mg" = "Pressure",
"Temperature_F" = "Temperature",
"Wind_Speed_meter_per_second" = "Wind Speed",
"Wind_Direction_deg" = "Direction",
"Humidity_Percent" = "Humidity",
"Solar_Radiation_W_per_m2" = "Solar",
"Daily_Rain_in" = "Rainfall")
#' Create a custom facet labeller function
facet_labeller <- function(variable, value) {
return(facet_names[value])
}
#' Plot the data with facets
weather_m[which(weather_m$variable != "Wind_Direction_deg")] %>%
ggplot() +
geom_point(aes(x = DateTime, y = value), size = 0.5, alpha = 0.2) +
facet_grid(variable ~ ., scales = "free_y", labeller = facet_labeller) +
theme_minimal() +
theme(axis.title.x = element_blank(),
strip.background = element_rect(fill = "grey80", colour = "grey50", size = 0.2),
panel.margin = unit(2, "lines"))
#' Note that this is actually a bad example of the use of facets; see my Gist `plot_aligned_series.R` for a better method
#' for plotting this data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment