2013-01-07 77 views
5

我使用R中的'circular'包和rose.diag函數爲位置數據創建了方位角色的玫瑰圖,基本方面爲N ,NE,E等,共8個分箱。然而,垃圾箱並不跨越這些方面。換句話說,第一個bin從0-45,第二個從45到90,依此類推,它以奇怪的方式彙集了方面數據。是否有任何方法來移動垃圾箱,所以0,45,90等是垃圾箱的中心,而不是邊緣?如何使用R中的包'圓'來移動玫瑰圖中的容器R

rose.diag(Degrees$Degrees, bins=8,zero=pi/2, units = 'degrees', rotation='clock') 
+0

我不認爲這是可能沒有黑客的源代碼。我似乎記得在某些時候這樣做,但將不得不挖掘代碼... –

+0

@BenBolker可能沒有黑客的源代碼,但與黑客的陰謀:) – agstudy

回答

4

我覺得本是正確的,它不能很容易地與rose.diag完成的,所以這裏是一個使用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 

enter image description here 這可能不是理想的,因爲不是所有的特徵ggplot2中的rose.diag有簡單的等價物。

+0

看到我的回答'rose.diag'厭惡 – agstudy

+0

@對你的答案進行研究表明了對原始情節元素的巧妙操縱。我個人更喜歡ggplot2渲染的外觀,我發現它更容易修改。 – orizon

+0

不是。我只是回答操作問題來轉移垃圾箱。也許我所做的是巧妙的,但我更喜歡我的渲染:) – agstudy

1

你可以使用gridBase軟件包來做類似的事情。我們繼續使用rose.diag和我們hack的情節,一旦我們在良好的視口空間。

enter image description here

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

對於視覺方面我想補充一些調整,但一些代碼,上面已經回答的問題。

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

爲什麼不能旋轉您的原始數據?注: (cdat-10,bins = 20,col =「darkgrey」,prop = 1.3,axes = FALSE,其中cdat以度數表示(零=π/ 2),而零表示爲2 * pi

rose.diag 附加= TRUE,零=π/ 2-π/ 20)

複製/粘貼的東西我的工作:

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) 

enter image description here

NB下面的代碼給你的舊圖(上圖左):

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

PS爲好奇,我們使用這樣的統計信息如http://www.sciencedirect.com/science/article/pii/S0950329315001068