【问题标题】:Plotting order of circles in ggforce在ggforce中绘制圆圈的顺序
【发布时间】:2021-02-28 11:07:48
【问题描述】:

我正在修改我之前发布的问题 Limited number of geoms in ggforce?。我当时以为我错了,但我现在可以更清楚地重建它: 我想在另一个之上绘制 N 个圆圈。无论我要绘制多少个圆圈,圆圈#11-N 都绘制在前 10 个圆圈的下方。这说明了问题:

library(tidyverse)
library(ggforce)

circles <- data.frame(
  x0 = seq(1, 30),
  y0 = seq(1, 30),
  r = 2
)

ggplot() +
  geom_circle(aes(x0 = x0, y0 = y0, r = r), fill = "grey", data = circles) +
  coord_fixed()

因此,当我想绘制同心圆时,前 10 个圆会隐藏所有其他圆。 我可以通过首先绘制 11-N 个圆圈然后绘制前十个圆圈来编写解决方法,但它并不优雅

【问题讨论】:

    标签: r ggplot2 ggforce


    【解决方案1】:

    这是ggforce 中的一个错误:它将圆圈中的点用与原始圆圈排序不同的字符串进行分组。

    要获得修复错误的版本,请使用

    remotes::install_github("dmurdoch/ggforce@sortorder")
    

    这将要求您安装包构建工具。如果您没有这些,您可以查看网站 https://github.com/thomasp85/ggforce/issues/224 以查看修复程序何时合并到包的 CRAN 构建中。

    【讨论】:

      【解决方案2】:

      我只能提供一个稍微笨拙的解决方法,它对于大型数据集可能效果不佳。循环遍历每一行,创建一个圆圈,然后将它们全部加在一起以制作一个要绘制的对象。

      library(tidyverse)
      library(ggforce)
      
      circles <- data.frame(
          x0 = seq(1, 30),
          y0 = seq(1, 30),
          r = 2
      ) 
      
      mygeom_circle <- function(onerow) {
          geom_circle(aes(x0 = x0, y0 = y0, r = r), fill = "grey", data = onerow) 
      }
      
      gs <- sapply(1:nrow(circles), function(r) mygeom_circle(circles[r,])) # loop through one by one making single circles
      gg <- 
          ggplot() +
          gs +            # add the single circles in the order they were made
          coord_fixed()
      
      gg
      

      【讨论】:

      • 谢谢,但由于它是一个错误,我更喜欢使用修复
      猜你喜欢
      • 1970-01-01
      • 2014-04-11
      • 2012-09-19
      • 1970-01-01
      • 2015-01-31
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多