How to shift bins in a rose diagram using package 'circular' in R

99封情书 提交于 2019-12-05 10:19:18

I think Ben is right that it cannot be done easily with rose.diag, so here is a solution using ggplot2:

library(ggplot2)
Degrees <- runif(100, 0, 360)
rose <- ggplot(mapping = aes(x = Degrees)) +
  stat_bin(breaks = (0:8 - 0.5)/8 * 360) +
  scale_x_continuous(
    breaks = 0:7/8*360, 
    labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW")
    ) +
  coord_polar(start=-pi/8)
rose

This may not be ideal because not all of the features in rose.diag have easy equivalents in ggplot2.

You can have something like this using gridBase package. We keep using rose.diag and we hack the plot, once we are in the space of the good viewport.

require(grid)
#grid.newpage()
##generate some data 
x <- circular(runif(50, 0, 2*pi))
bins <- 8
rotation <- 'clock'
##tcl =0(no ticks), tcl.text=-2 to write away the ticks marks
rose.diag(x, bins=bins,zero=0,  rotation='clock',
          tcl=0,tcl.text=-2,col='#80FF00FF')
library(gridBase)
## I use the plot viewport
vp <- baseViewports()$plot
pushViewport(vp)           ## here we go!
## radial transformation 
at <- (0:bins - 0.5)/bins * 2 * pi

## ticks
grid.segments( x0 =  .95*sin(at),  y0 = 0.95*cos(at),
               x1 = 1.05*sin(at),  y1 = 1.05*cos(at),
               default.units = "native")
## ticks labels
grid.text(x = 1.1*sin(at),   default.units = "native",
          y = 1.1*cos(at),   gp=gpar(col='red'),
          label = c("N", "NE", "E", "SE", "S", "SW", "W", "NW"))

For visual aspect I add some tuning , but the some code above answer already to the question.

## dashed lines from the center for visual aspect 
grid.segments( x0 =  .95*sin(at),  y0 = 0.95*cos(at),
               x1 = 0,  0,
               gp = gpar(lty="dashed"),
               default.units = "native")

## circle just to get the same color of text
grid.circle(r=1,x=0,y=0,gp=gpar(col='red',fill=NA,lwd=2), default.units = "native")
## remove the viewport
popViewport(1)

Why not rotate your original data? N.b. below cdat is in degrees (zero = pi / 2), whilst zero is in 2*pi

rose.diag(cdat - 10, bins = 20, col="darkgrey", prop=1.3, axes=FALSE, add=TRUE, zero = pi/2-pi/20)

Copy/pasting something I am working on:

library(circular)

raw <-read.csv("C:\\Users\\Andy\\Desktop\\business\\research\\Oxford\\MichelDish\\r.csv", header=T)
raw <-na.omit(raw)


cdat <- circular(raw [, c ("kandUnknown")],type="angles",units="degrees", rotation="clock", zero=pi/2)


plot(cdat, cex=1.1, bin=720, stack=TRUE, sep=0.035, shrink=1.8, tcl.text=.2)


ticks.circular(circular(seq(0,2*pi,pi/8)), zero=pi/2, rotation='clock', tcl=0.075)



rose.diag(cdat - 10, bins = 20, col="darkgrey", prop=1.3, axes=FALSE, add=TRUE, zero = pi/2 - pi/20)

lines(density.circular(cdat, bw=40), lwd=2, lty=1)

n.b. the code below gives you the old figure (above left):

rose.diag(cdat, bins = 20, col="darkgrey", prop=1.3, axes=FALSE, add=TRUE)

ps for the curious, we are using such stats for eg http://www.sciencedirect.com/science/article/pii/S0950329315001068

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!