Last active
September 10, 2017 15:08
-
-
Save eliocamp/20cc47b2a4b39af602a57f1e2cdd437f to your computer and use it in GitHub Desktop.
Example of a windrose on ggplot2
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
library(ggplot2) | |
library(data.table) | |
# Datos de este link: http://midcdmz.nrel.gov/apps/plot.pl?site=NWTC&start=20010824&edy=26&emo=3&eyr=2062&year=2013&month=1&day=1&endyear=2013&endmonth=12&endday=31&time=0&inst=21&inst=39&type=data&wrlevel=2&preset=0&first=3&math=0&second=-1&value=0.0&user=0&axis=1 | |
wind <- fread("~/Downloads/20130101.csv") | |
# La función cut me da los intervalos en orden decresciente, pero | |
# los necesito en orden cresciente. | |
ReverseCut <- function(x, ...) { | |
f <- cut(x, ...) | |
n <- length(levels(f)) | |
levels(f) <- levels(f)[n:1] | |
f | |
} | |
# Truquito: mover el dominio de la dirección a (-22.5/2, 360 - 22.5/2). | |
wind[, new.dir := ifelse(direction > 360 - 22.5/2, direction - 360, direction)] | |
# Me quedo con 1000 al azar para que no tarde tanto en plotear. | |
set.seed(1) | |
wind <- wind[sample(1:.N, 1000)] | |
ggplot(wind, aes(new.dir, fill = ReverseCut(speed, breaks = 5))) + | |
geom_histogram(binwidth = 22.5, center = 0, aes(y = ..density..*100)) + | |
coord_polar(start = -22.5/2*pi/180) + # para que el norte quede arriba | |
scale_fill_viridis_d(name = "Velocidad", direction = -1) + | |
scale_y_continuous(name = "Frecuencia", | |
limits = c(-0.50, NA), labels = NULL) + | |
annotate(geom = "rect", ymin = -0.5, ymax = 0, xmin = 0-22.5/2, xmax =360-22.5/2, | |
fill = "white") + # círculo blanco del centro | |
scale_x_continuous(name = "", limits = c(0 - 22.5/2, 360 - 22.5/2), | |
breaks = seq(0, 360 - 22.5, by = 22.5), | |
minor_breaks = NULL, | |
labels = c("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", | |
"S", "SSO", "SO", "OSO", "O", "ONO", "NO", "NNO")) + | |
geom_text(data = data.frame(x = 0, y = seq(0, 12, by = 3)), aes(x = x, y = y, label = y), | |
inherit.aes = F) + | |
theme_minimal() | |
# Resultado: https://i.imgur.com/rzmb64w.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment