【问题标题】:not centred concentric circles plot不居中的同心圆图
【发布时间】:2018-05-12 21:42:59
【问题描述】:

我有时会这样做,但我无法找到解决方案。我有这段代码可以使用 R 中的 ggplot2 在 25 x 25 网格上绘制同心圆。我不知道如何能够操纵同心圆的中心不在原点(0,0),而是在中心网格 (5,5)。我也想将网格的比例保持在 25 到 25 之间。非常感谢您提前

 require(ggplot2)
 require(grid)

 x <- rep(seq(25), 25)
 y <- rep(seq(25), each=25)
 circ_rads <- seq(1,5,2)

qplot(x, y) +
  lapply(circ_rads, FUN = function(x)
      annotation_custom(circleGrob(gp = gpar(fill = "transparent", color = "black")), 
                        -x, x, -x, x)) +
  geom_text(aes(x = 0, y = circ_rads + 0.1, label = circ_rads)) +
  coord_fixed(ratio = 1) 

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我们可以像这样使用ggforce::geom_circle

    library(ggplot2)
    library(ggforce)
    
    x <- rep(seq(25), 25)
    y <- rep(seq(25), each=25)
    circ_rads <- seq(1,5,2)
    
    xy <- data.frame(x=x, y=y)
    
    circles <- data.frame(
      x0 = 5, # You say circles should be a 'centre of the grid' and 5, 5
      y0 = 5, # not sure what you really mean, so going with 5, 5 here
      r = circ_rads
    )
    
    ggplot() +
      geom_point(data = xy, 
                 aes(x, 
                     y)) +
      geom_circle(data = circles, 
                  aes(x0 = x0,
                      y0 = y0,
                       r = r)) +
      coord_fixed()
    

    【讨论】:

    • 非常感谢您的帮助。这正是我一直试图弄清楚的。再次感谢您。
    • 完全不用担心。如果这回答了您的问题,您应该accept the answer 向其他人表明这一点
    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多