Use Ggplot2 to draw wind speed rose chart

Source: Internet
Author: User
Tags ggplot

Install.packages ("Ggplot2")

Library (GGPLOT2)

# WINDROSE.R Http://stackoverflow.com/questions/17266780/wind-rose-with-ggplot-r
Require (GGPLOT2)
Require (Rcolorbrewer)

Plot.windrose <-function (data,
Spd
Dir
Spdres = 10,
Dirres = 30,
Spdmin = 0,
Spdmax = 90,
Spdseq = NULL,
palette = "Ylgnbu",
Countmax = NA,
debug = 0) {


# look-to-see-what data is passed in to the function
if (Is.numeric (SPD) & Is.numeric (dir)) {
# Assume that we ' ve been given vectors of the speed and direction vectors
Data <-data.frame (SPD = SPD,
dir = dir)
SPD = "SPD"
dir = "dir"
} else if (exists ("data")) {
# Assume that we ' ve been given a data frame, and the name of the
# and direction columns. This is the format of we want for later use.
}

# Tidy up input data----
N.in <-nrow (data)
Dnu <-(Is.na (data[[spd]) | is.na (Data[[dir]])
DATA[[SPD]][DNU] <-NA
DATA[[DIR]][DNU] <-NA

# figure out the wind speed bins----
if (Missing (spdseq)) {
Spdseq <-seq (spdmin,spdmax,spdres)
} else {
if (Debug >0) {
Cat ("Using custom speed bins \ n")
}
}
# Get some information about the number of bins, etc.
N.spd.seq <-Length (SPDSEQ)
N.colors.in.range <-n.spd.seq-1

# Create the Color map
Spd.colors <-Colorramppalette (Brewer.pal (min (max) (3,
N.colors.in.range),
Min (9,
N.colors.in.range)),
palette)) (N.colors.in.range)

If (max (data[[spd]],na.rm = true) > Spdmax) {
Spd.breaks <-C (spdseq,
max (data[[spd]],na.rm = True)
Spd.labels <-C (Paste (c (spdseq[1:n.spd.seq-1]),
'-',
C (Spdseq[2:n.spd.seq])),
Paste (Spdmax,
"-",
Max (data[[spd]],na.rm = TRUE))
Spd.colors <-C (spd.colors, "grey50")
} else{
Spd.breaks <-C (seq (spdseq))
Spd.labels <-paste (c (Spdseq[1:n.spd.seq-1]),
'-',
C (Spdseq[2:n.spd.seq]))
}
data$spd.binned <-cut (x = data[[spd]],
breaks = Spd.breaks,
labels = spd.labels,
Ordered_result = TRUE)

# figure out the wind direction bins
Dir.breaks <-C (-DIRRES/2,
Seq (DIRRES/2, 360-dirres/2, by = Dirres),
360+DIRRES/2)
Dir.labels <-C (Paste (360-DIRRES/2, "-", DIRRES/2),
Paste (seq (DIRRES/2, 360-3*dirres/2, by = Dirres),
"-",
Seq (3*DIRRES/2, 360-dirres/2, by = Dirres)),
Paste (360-DIRRES/2, "-", DIRRES/2))
# Assign direction to a bin
dir.binned <-Cut (Data[[dir]),
breaks = Dir.breaks,
Ordered_result = TRUE)
Levels (dir.binned) <-dir.labels
data$dir.binned <-dir.binned

# Run Debug if required----
if (debug>0) {
Cat (Dir.breaks, "\ n")
Cat (Dir.labels, "\ n")
Cat (Levels (dir.binned), "\ n")
Cat (speedcuts.colors, "\ n")
}

# Create the plot----
P.windrose <-ggplot (data = data,
AES (x = dir.binned,
Fill = spd.binned)) +
Geom_bar () +
Scale_x_discrete (drop = FALSE,
Labels = waiver ()) +
Coord_polar (start =-((DIRRES/2)/360) * 2*pi) +
Scale_fill_manual (name = "Wind speed (M/s)",
Values = Spd.colors,
drop = FALSE) +
Theme (axis.title.x = Element_blank ())

# Adjust axes if required
if (!is.na (Countmax)) {
P.windrose <-P.windrose +
Ylim (c (0,countmax))
}

# Print the plot
Print (P.windrose)

# return the handle to the wind rose
Return (P.windrose)
}

data.in <-read.csv (file = "H:/1.csv", Col.names = C ("date", "HR", "ws.80", "wd.80"), stringsasfactors = FALSE)
P <-plot.windrose (spd = Data.in$ws.80,dir = Data.in$wd.80,spdseq = C (0,10,20,30,40,50,60,70,80,90))

Use Ggplot2 to draw wind speed rose chart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.