我正在寻找使用ggplot2创建风玫瑰的良好R代码(或包),以显示风的频率,大小和方向.
我对ggplot2特别感兴趣,因为这样可以让我有机会利用其中的其他功能.
从National Wind Technology的"M2"塔上下载80米级的一年天气数据.此链接将创建自动下载的.csv文件.你需要找到该文件(它被称为"20130101.csv"),并将其读入.
# read in a data file
data.in <- read.csv(file = "A:/drive/somehwere/20130101.csv",
col.names = c("date","hr","ws.80","wd.80"),
stringsAsFactors = FALSE))
Run Code Online (Sandbox Code Playgroud)
这适用于任何.csv文件,并将覆盖列名称.
如果您不想下载该数据,我们将使用以下10个数据点来演示该过程:
data.in <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
Run Code Online (Sandbox Code Playgroud)
1L,1L),. Label ="1/1/2013",class ="factor"),hr = 1:9,ws.80 = c(5,7,7,51.9,11,12,9,11 ,17),wd.80 = c(30,30,30,180,180,180,269,270,271))..Name = c("date","hr","ws.80"," wd.80"),row.names = c(NA,-9L),class ="data.frame")
在该示例中,颜色与每个条的半径相关.如何在这个情节中添加一个颜色条?

我的代码模仿了"玫瑰图"投影,它基本上是极坐标投影的条形图.
这是它的一部分:
angle = radians(10.)
patches = radians(360.)/angle
theta = np.arange(0,radians(360.),angle)
count = [0]*patches
for i, item in enumerate(some_array_of_azimuth_directions):
temp = int((item - item%angle)/angle)
count[temp] += 1
width = angle * np.ones(patches)
# force square figure and square axes looks better for polar, IMO
fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
rmax = max(count) + 1
ax.set_rlim(0,rmax)
ax.set_theta_offset(np.pi/2)
ax.set_thetagrids(np.arange(0,360,10))
ax.set_theta_direction(-1)
# project strike distribution as histogram bars
bars = ax.bar(theta, count, width=width)
r_values = …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个玫瑰图,显示每个细胞子集的平均轨迹角度和距离.我希望玫瑰图周围的角度是轨迹角度,图中条形的长度是总位移.
这是每组平均角度和位移的测试数据集.
testsum<-data.frame(Group=c(1,2,3),
angle=c(0.78,1.04,2.094),
displacement=c(1.5,2,1))
Run Code Online (Sandbox Code Playgroud)
当我尝试用循环方法绘制它时,我的图表看起来非常错误.
p1<-ggplot(testsum, aes(x=angle,y=displacement))+
coord_polar(theta="x",start=0)+
geom_bar(stat="identity",aes(color=Group,fill=Group),width=.01)+
scale_x_continuous(breaks=seq(0,360,60))
Run Code Online (Sandbox Code Playgroud)
它给了我这个输出图.
根据数据所说的内容,它看起来应该更像这样(绘制预期的输出).

它似乎是错误地放置角度?知道我做错了什么吗?
我使用R中的'圆形'包和rose.diag函数创建了位置数据的方面的玫瑰图,其中包括N,NE,E等的基本方面,总共8个箱.但是,垃圾箱并没有跨越这些方面.换句话说,第一个bin从0到45,第二个从45到90,依此类推,它以奇怪的方式汇集了方面数据.有没有办法移动垃圾箱,所以0,45,90等是垃圾箱的中心,而不是边缘?
rose.diag(Degrees$Degrees, bins=8,zero=pi/2, units = 'degrees', rotation='clock')
Run Code Online (Sandbox Code Playgroud) 我试图在地图上的特定坐标上绘制玫瑰图/圆形直方图,类似于在地图上绘制饼图,就像在包中一样mapplots.
下面是一个生成的示例mapplots(参见下面的代码),我想用玫瑰图替换饼图

该软件包circular可以让我绘制玫瑰图,但我无法将其与mapplots软件包集成.有关替代包或代码的任何建议吗?
回答代码制作地图的问题.这一切都基于mapplots包.我为地图下载了一个shapefile(我想来自http://www.freegisdata.org/)
library(mapplots)
library(shapefiles)
xlim = c(-180, 180)
ylim = c(-90, 90)
#load shapefile
wmap = read.shapefile ("xxx")
# define x,y,z for pies
x <- c(-100, 100)
y <- c(50, -50)
z1 <- c(0.25, 0.25, 0.5)
z2 <- c(0.5, 0.2, 0.3)
z <- rbind(z1,z2)
# define radii of the pies
r <- c(5, 10)
# it's easier to have all data in a single df
plot(NA, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用来自Andy Clifton的梦幻风玫瑰R代码,网址为/sf/answers/1208674701/,用于绘制从气象模型中提取的数据.从模型中我得到你和风速分量m/s.然后我计算风速和风向并尝试风玫瑰代码.但我不确定它是否按预期工作,或者我不明白该功能正在做什么.
我尝试使用Andy的函数和下面的代码来绘制4朵风玫瑰:
flist<-c("03050","12010","46070","46050")
for (i in 1:4 ) {
windfile=paste("viento-",flist[i],".csv",sep="")
viento=read.table(windfile,sep=";",header=F)
colnames(viento) <- c("u","v")
fecha=read.table("ffhh.txt",header=F,sep=";")
colnames(fecha) <- c("date","hr")
wind<-cbind.data.frame(fecha,viento)
wind$speed=sqrt(wind$u*wind$u+wind$v*wind$v)
wind$dir=windDir(wind$u,wind$v)
graf=paste("RAMS-wind-rose-",flist[i],".png",sep="")
png(graf,res=240,width=3000, height=2400)
plot.windrose(data = wind,spd = "speed",dir = "dir")
dev.off()
}
Run Code Online (Sandbox Code Playgroud)
图形输出是

因此,最大风速在18-20米/秒的范围内.这样对吗?但如果我寻找最大风速,我会得到不同的价值
max(wind$speed)
[1] 10.24317
Run Code Online (Sandbox Code Playgroud)
也许我误解了wind.rose函数是如何运行的,或者在调用函数时可能存在问题.任何建议将不胜感激,我现在卡在这里.
风数据:
dput(wind)
structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用下面的代码在 R 中绘制月度玫瑰图,但遇到错误:“错误:分面变量必须至少有一个值”
这是我使用的 数据链接到数据
此代码来自上一篇文章:使用 ggplot 的玫瑰图
require(ggplot2)
require(RColorBrewer)
require(scales)
plot.windrose <- function(data,
spd,
dir,
spdres = 2,
dirres = 22.5,
spdmin = 2,
spdmax = 20,
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 = …Run Code Online (Sandbox Code Playgroud)