R Grid 包解决方案,用于在圆上绘制角度

问题描述 投票:0回答:1

我想在圆的周长上画角。这个带有网格包的解决方案看起来很有前途在 R 中带有向量的圆形图用于我需要的定制,但我在绘制圆边缘的角度时遇到了麻烦。

这接近我需要的圆形包装。它具有以特定角度与圆边缘相交的箭头,并且更多角度绘制为周长上的点

angles <- read.table(text ='
degree Year
    120  2000
     30  2001
  160  2002
    35 2003
   150  2004    
   90  2005
    70  2006
      20  2007',header=T)
angles = circular(angles$degree, units = "degrees", 
                  template = "geographics") 
plot(angles, col = "black",stack=F) 
arrows.circular(limits, col = c("red","red","blue"))

但是,我在使用网格包来表示线段和点时遇到了麻烦。我认为解决方案可能是使用此处的函数将角度转换为笛卡尔坐标

a=57
b=75
c=42
limits<-data.frame(lims=c(a,b,c))

angles <- read.table(text ='
degree Year
    120  2000
     30  2001
  160  2002
    35 2003
   150  2004    
   90  2005
    70  2006
      20  2007',header=T)

limits.custom <- function(limits){
  pushViewport(viewport(layout.pos.col=2,layout.pos.row=2))
  apply(limits,1,function(x){
    pushViewport(viewport(angle=x['lims']))  
    grid.segments(x0=0.5,y0=0.5,x1=0.8,y1=0.5,gp=gpar(lty=5,lwd=2))
    popViewport()
  })
  popViewport()
}

circ_coords <- function(r, t, h, k){
  t <- t * pi/180
  x <- r * cos(t) + h
  y <- r * sin(t) + k
  z <- c(x, y)
  names(z) <- c('X', 'Y')
  return(z)
}

angles.custom <- function(angles){
  pushViewport(viewport(layout.pos.col=2,layout.pos.row=2))
  apply(angles,1,function(x){
    coord<-circ_coords(r=0.5,t=x['degree'],h=0.5,k=0.5)
    grid.points(x=coord["X"],y=coord["Y"],gp=gpar(pch=16))
  })
  popViewport()
}

pushViewport(viewport(layout.pos.col=2,layout.pos.row=2))
grid.circle(x=0.5,y=0.5,r=0.5,gp = gpar(ltw=c(3),col=c('black')))
limits.custom(limits)
angles.custom(angles)
pushViewport(viewport(layout.pos.col=2,layout.pos.row=2))
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()
r grid circular-dependency
1个回答
0
投票

这里有一个使用

ggplot2
的快速破解方法。

我选择以两种方式改变

limits

  • 在所有数据源中将“x”和“y”变量命名为相同名称通常会更方便,因此我将
    limits$lims
    更改为
    limits$degree
    以匹配
    angles
    ;
  • 我添加了一个 nominal 变量
    type
    ,以演示 ggplot 处理事物的分类方式。虽然当然可以包含文字颜色,但允许 ggplot 使用您拥有的“真实类别名称”是一件好事(特别是如果您想要图例),然后如果需要,可以手动控制分配给中每个级别的颜色。分类
library(ggplot2)
angles <- read.table(text ='
degree Year
    120  2000
     30  2001
  160  2002
    35 2003
   150  2004    
   90  2005
    70  2006
      20  2007',header=T)
a=57
b=75
c=42
limits <- data.frame(degree=c(a,b,c), type=c("R","R","B"))
ggplot(angles, aes(x=degree)) +
  geom_point(y = 1) + 
  geom_segment(
    y = 0, yend = 1,
    aes(xend = degree, color = type), 
    arrow = grid::arrow(),
    data = limits) +
  coord_polar(theta = "x") +
  scale_x_continuous(
    name = NULL,
    limits = c(0, 360),
    breaks = seq(0, 360, length.out = 5)[-1],
    labels = c("E", "S", "W", "N")
    # minor_breaks = seq(0, 360, length.out = 9)[-1] # if you want to specify semi-cardinals or more
    ) +
  scale_color_manual(
    values = c(R="red", B="blue")
  ) +
  guides(color = "none") +
  theme_minimal() +
  theme(
    panel.grid.minor = element_blank() # suppress minor_breaks
  )

您可以通过阅读

?grid::arrow
并更改其选项来控制大部分箭头,我使用的是默认值。

此外,我对 x 限制进行了硬编码,需要将其保持在 360 度圆(因为这实际上只是一个具有未定义的圆形限制的极坐标图)。

© www.soinside.com 2019 - 2024. All rights reserved.