簡體   English   中英

圓形圖與R中的向量

[英]Circular plot with vectors in R

我正在嘗試創建一個圓形圖,其中各種角度的矢量來自不同角度的原點:類似下圖,盡管它不必相同。 circstatscircularcircular ,並學到了很多關於圓形圖的知識,但沒有找到任何與我正在尋找的東西相似的東西。 我認為如果必須的話,我可以手工制作一些東西,但似乎有可能比我更有經驗的人已經寫了一些代碼來做這件事。


該圖來自Schmidt,2007,Ecology 88(11):2793-2802 ,圖2C。

網格包非常強大,可用於組合和排列圖形元素。

library(grid)

結果如下: 在此輸入圖像描述 有些數據,我認為剩下的就是你的價值<1

polar <- read.table(text ='
degree value
1    120  0.50
2     30  0.20
3   160  0.20
4     35  0.50
5    150  0.40
6    90  0.14
7    70  0.50
8      20  0.60',header=T)


## function to create axis label
axis.text <- function(col,row,text,angle){
  pushViewport(viewport(layout.pos.col=col,layout.pos.row=row,just=c('top')))
  grid.text(angle,vjust=0) 
  grid.text(text,vjust=2)         
  popViewport()
}

## function to create the arrows, Here I use the data
arrow.custom <- function(polar){
  pushViewport(viewport(layout.pos.col=2,layout.pos.row=2))
  apply(polar,1,function(x){
       pushViewport(viewport(angle=x['degree']))  
          grid.segments(x0=0.5,y0=0.5,x1=0.5+x['value']*0.8,y1=0.5,
          arrow=arrow(type='closed'),gp=gpar(fill='grey'))
       popViewport()
  })
  popViewport()
}


## The global layout 3*3 matrix
lyt=grid.layout(3, 3, 
                   widths= unit(c(4,15,4), "lines"),
                   heights=unit(c(4,15,4), "lines"),
                   just='center')
pushViewport(viewport(layout=lyt,xscale=2*extendrange(polar$value)))
 ## the central part : circles , arrows and axes
pushViewport(viewport(layout.pos.col=2,layout.pos.row=2))
grid.circle(r=c(0.5,0.3),gp = gpar(ltw=c(3,2),col=c('black','grey')))
arrow.custom(polar)
grid.segments(x0=0.5,y0=0,x1=0.5,y=1,gp=gpar(col='grey'))
grid.segments(x0=0,y0=0.5,x1=1,y=0.5,gp=gpar(col='grey'))
popViewport()

## the axis labels 
axis.text(1,2,'Phragmites',expression(270 * degree))
axis.text(3,2,'Spartina',expression(90 * degree))
axis.text(2,1,'Increasing tropic position',expression(0 * degree))
axis.text(2,3,'Decreasing tropic position',expression(180 * degree))

plotrix包中有一個polar.plot函數,似乎可以做你想要的。 但是,我還沒有弄清楚如何沿着其中一條邊的圓弧添加虛線。

例:

library(plotrix)

testlen<-c(rnorm(36)*2+5)
testpos<-seq(0,350,by=10)
polar.plot(testlen,testpos,main="Test Polar Plot",lwd=3,line.col=4)

#rotate degree
oldpar<-polar.plot(testlen,testpos,main="Test Clockwise Polar Plot",
start=180,clockwise=TRUE,lwd=3,line.col=4)

# reset everything
par(oldpar)

在此輸入圖像描述

我最終選擇了radial.plot中的plotrix (盡管事實證明polar.plot具有所有相同的功能 - 當我弄清楚它時,它的記錄並不是很好)。 我無法弄清楚如何做箭頭,或沿着圓周的部分虛線但是對我的目的都不重要。 由於某種原因,我無法得到第一個數據點,所以我插入了一個虛擬點。 感謝大家的幫助!

library(plotrix)

magnitude <- c(2.1, 2.3, 2.5, 1.5, 2.8, 2.7)
angle <- c(2.1, 2.6, -0.1, -2.6, 0.1, 0.4)
directionlabels <- c("more\nbenthic", "higher trophic", 
                     "more\npelagic", "lower trophic")
colors <- c("black", "red", "green", "blue", "orange", "purple", "pink")
par(cex.axis=0.7)
par(cex.lab=0.5)

radial.plot(c(0, magnitude), 
            c(0, angle), 
            lwd=4, line.col=,
            labels=directionlabels,
            radial.lim = c(0,3), #range of grid circle
            main="circular diagram!",
            show.grid.label=1, #put the concentric circle labels going down
            show.radial.grid=TRUE
)

圓圖!

以下是使用my.symbols以及TeachingDemos包中的ms.polygonms.arrows的方法:

plot(c(-2,2),c(-2,2), axes=FALSE, xlab='', ylab='', type='n', asp=1)
abline(v=0, col='lightgrey')
abline(h=0, col='lightgrey')
my.symbols(c(0,0),c(0,0),ms.polygon, xsize=c(2,4), lwd=c(1,2), n=360)

theta <- seq(pi/4, 3*pi/4, length=250)
lines( 2.03*cos(theta), 2.03*sin(theta), lwd=2, lty='dashed' )
lines( c(0,0), c(0,2), lty='dashed', lwd=2 )

a <- c(300,305,355,0,5,45,65)
l <- c(1.1, .5, .4,1,.6,.7,1.25)

my.symbols( rep(0,7), rep(0,7), ms.arrows, xsize=2, r=l, adj=0, 
        angle=pi/2 - pi/180*a )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM