R基本图形圆形直方图 [英] R base graphics circular histogram

查看:143
本文介绍了R基本图形圆形直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What would be the best way to draw a circular histogram in R?

我的数据具有以下形式:

My data has this form:

    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函数,但是它需要使用另一种格式的输入数据.

I have tried using windrose function from the circular package but it requires input data in another format.

我研究了以下图形::函数和星形看上去很有希望,但到目前为止还没有具体的内容.

I have looked into the graphics:: functions and stars looked promising, but nothing concrete so far.

谢谢

推荐答案

您可以尝试使用circular进行以下操作,但是由于您的数据量很小,因此绘图不太理想:

You can try the following with circular, but since your data size is small, the plot is not very fancy:

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相似,如下所示:

With another library openair it looks like the following:

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转换为极坐标)

The polar coord plot with ggplot2 looks different (which just converts geom_bar into polar coordinates)

library(ggplot2)
ggplot(df, aes(x=dir, y=mag)) + geom_bar(stat='identity') + coord_polar()

我从头开始尝试在base R中执行的某些实现(仅出于想法,该实现不是非常有效,您可以始终提高实现效率,例如,使用polygon而不是segments来填补弧线),我们可以在base R中使用类似的实现来模仿ggplot中的实现:

Some implementation I tried in base R from scratch (just for the idea, the implementation is not very efficient, you can always improve the efficiency of implementation, for example use polygon instead of segments to fill the arcs), we can use a similar implementation in base R to mimic the one in ggplot:

add.filled.arc <- function(center.x, center.y, radius, angle.start, angle.end, col='black') {
  theta <- seq(angle.start, angle.end, .0001)
  segments(0, 0, radius*cos(theta), radius*sin(theta), col)
  segments(0, 0, cos(angle.start), sin(angle.start), col='gray')
  segments(0, 0, cos(angle.end), sin(angle.end), col='gray')
}

plot.coord.polar <- function(df) {
  df <- df[complete.cases(df),]
  df <- df[order(df[,1]),]
  df[,1] <- df[,1]*(pi/180) # convert dir to radian
  df[,2] <- df[,2] / max(df[,2]) # normalize magnitude within [0-1]
  plot(-1:1, -1:1, type= 'n', xlab='', ylab='', xaxt='n', yaxt='n')
  sapply(1:(nrow(df)-1), function(i) add.filled.arc(0, 0, df[i,2], df[i,1], df[i+1,1], rainbow(nrow(df))[i]))
  theta <- seq(0, 2*pi, 0.0001)
  lines(cos(theta), sin(theta), col='gray')
}

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)
plot.coord.polar(df)

这篇关于R基本图形圆形直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆