R基础图形循环直方图

时间:2017-01-13 03:52:23

标签: r graphics

在R中绘制圆形直方图的最佳方法是什么?

我的数据有以下形式:

    Dir      N
 1: 360  56564
 2:   0     NA
 3: 180 149374
 4: 210  82219
 5: 240  23315
 6: 300  11436
 7: 330  30648
 8:  30  32198
 9:  60  15266
10:  90  14596
11: 120  26267
12: 150  81782
13: 270  10100

我尝试使用循环包中的windrose函数,但它需要另一种格式的输入数据。

我已经研究了图形::函数和星星看起来很有希望,但到目前为止还没有具体的内容。

由于

2 个答案:

答案 0 :(得分:1)

您可以使用getParam尝试以下操作,但由于您的数据量很小,因此情节不是很花哨:

circular

enter image description here

使用另一个库library(circular) df <- read.table(text='Dir N 1: 360 56564 2: 0 NA 3: 180 149374 4: 210 82219 5: 240 23315 6: 300 11436 7: 330 30648 8: 30 32198 9: 60 15266 10: 90 14596 11: 120 26267 12: 150 81782 13: 270 10100', header=TRUE) rownames(df) <- NULL names(df) <- c('dir', 'mag') df$dir <- circular(as.numeric(df$dir), units='degrees') df$mag <- df$mag / 10000 # scale magnitude windrose(df, breaks=circular(seq(0, 2 * pi, by = pi/4)), increment=5) ,它看起来如下:

openair

enter image description here

library(openair) df <- read.table(text='Dir N 1: 360 56564 2: 0 NA 3: 180 149374 4: 210 82219 5: 240 23315 6: 300 11436 7: 330 30648 8: 30 32198 9: 60 15266 10: 90 14596 11: 120 26267 12: 150 81782 13: 270 10100', header=TRUE) names(df) <- c('wd', 'ws') df$ws <- df$ws / 10000 # scale speed windRose(df, angle=45) 的极坐标图看起来不同(仅将ggplot2转换为极坐标)

geom_bar

enter image description here

我在library(ggplot2) ggplot(df, aes(x=dir, y=mag)) + geom_bar(stat='identity') + coord_polar() 从头开始尝试的一些实现(仅仅是为了这个想法,实现效率不高,你总是可以提高实现效率,例如使用base R而不是{{1填充弧),我们可以在polygon中使用类似的实现来模仿segments中的实现:

base R

enter image description here

答案 1 :(得分:0)

你可以使用ggplot2:

library(ggplot2)
data(mpg)
g <- ggplot(mpg, aes(class)) + geom_bar() + coord_polar()
g

enter image description here

对于您的数据,假设上面的数据框名为df,并且您想要绘制N,那么您可以这样做:

library(ggplot2)
g <- ggplot(df, aes(N)) + geom_bar() + coord_polar()
g