1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 使用ggplot2绘制风向风速玫瑰图

使用ggplot2绘制风向风速玫瑰图

时间:2023-12-04 09:05:12

相关推荐

使用ggplot2绘制风向风速玫瑰图

偶然的机会,试着使用了一次ggplot2,绘出的玫瑰图的确非常美,将使用过程记录下来。

ggplot2

ggplot2是用于绘图的R语言扩展包,其理念根植于《Grammar of Graphics》一书。它将绘图视为一种映射,即从数学空间映射到图形元素空间。

例如将不同的数值映射到不同的色彩或透明度。该绘图包的特点在于并不去定义具体的图形(如直方图,散点图),而是定义各种底层组件(如线条、方块)来合成复杂的图形,

这使它能以非常简洁的函数构建各类图形,而且默认条件下的绘图品质就能达到出版要求。

//05/11/7264.html

安装ggplot2

R语言环境下安装ggplot2

install.packages("ggplot2")

绘制风向风速玫瑰图

载入相关依赖包

#library与require功能类似,区别是require()返回一个logic之,library返回一个类似地址的值

#library(ggplot2)

#library(RColorBrewer)

require(ggplot2)

require(RColorBrewer)

绘制函数来自/questions/17266780/wind-rose-with-ggplot-r,针对个人需求做了相关修改

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 was 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 speed

# and direction columns. This is the format 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 each wind 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

#修改上述函数,改用英文缩写方位标志

#dir.breaks <- c(-1, 11.25 + (22.5*0:16))

#dir.binned <- cut(data[[dir]],breaks = dir.breaks,labels = c("N", "NNE", "NE", "ENE", "E", "ESE","SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N2"))

#levels(dir.binned)[17] = "N"

# 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)

#调用方法,代入风速风向数据,生成图像(data.in$ws.80指代入上述读取数据中的ws.80行,spdseq为风速分割区间)

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))

#保存图像

ggsave("H:/test.png")

使用的数据文件

生成的风向风速玫瑰图

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。