无法复制此 ggplot2 绘图

7

我无法复制 ggrough 库中的一个示例 (https://xvrdm.github.io/ggrough/articles/Customize%20chart.html)。特别是,我正在尝试复制以下图表(除了字体方面):

enter image description here

这段代码取自上文所述"Kindergarten"标题下的同一链接。
我正在使用以下代码:
library(hrbrthemes)
library(tidyverse)
library(gcookbook)
library(ggplot2)
library(ggrough)
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + 
    geom_area(alpha=0.8) +
    scale_fill_ipsum() +
    scale_x_continuous(expand=c(0,0)) +
    scale_y_comma() -> p 

options <- list(GeomArea=list(fill_style="hachure", 
                              angle_noise=0.5,
                              gap_noise=0.2,
                              gap=1.5,
                              fill_weight=1))
get_rough_chart(p, options)

然而,我无法复制上述内容。这是我的结果:

enter image description here

再次强调,我不担心字体问题,但是希望能够让阴影 geom_area 起作用。目前它根本没有渲染出来。为了参考,这里是 p 对象产生的结果(即在经过 ggrough 处理之前的图):

enter image description here

请注意,我能够复制使用geom_col的“Blueprint”示例。因此,似乎ggrough在处理geom_area时出了问题,但不确定。

包 'ggrough' 不可用(适用于 R 版本 4.0.1),似乎不在 CRAN 上。 - Steffen Moritz
仅限于从GitHub获取ah。 - Steffen Moritz
2
尝试过了,但也没有成功。从软件包描述中我猜测,在当前的Github版本中它就是无法工作 - ggrough仍然在开发中,存在着很大的bug。欢迎贡献! - Steffen Moritz
我得到了和你一样的虚假图表。只是一个想法,这个程序依赖于tidyverse,并且已经有2年没有更新了,我预计会出现一些相当显著的故障... - Ben Bolker
1
做了另一个检查(将其发布为答案给你,因为它太长了)。但我基本上得出了与你相同的结论 - 这是一个与geom_area有关的问题。甚至快速查看了包源代码,但乍一看没有为你找到快速解决方法。 - Steffen Moritz
显示剩余2条评论
2个回答

9

由于ggrough包寻找名为“polyline”的xml节点来查找目标区域,因此该包无法与当前版本的ggplot2很好地配合使用geom_area。在较旧的ggplot2版本中,这可能效果很好,因为每个区域都被折线包围。然而,在更近期的版本中,情况却不再如此(请参见3.3.0版本下的重大变更)。

如果下面的方法适用于您,请尝试:

  1. 定义一个版本的parse_*函数,该函数寻找“polygon”而不是“polyline”。
parse_polygons <- function (svg) {
  shape <- "polygon" # was "polyline" in ggrough:::parse_areas
  keys <- NULL
  ggrough:::parse_shape(svg, shape, keys) %>% {
    purrr::map(., 
               ~purrr::list_modify(.x, 
                                   points = stringr::str_squish(.x$points) %>% 
                                     {stringr::str_glue("M{.}Z")}, 
                                   shape = "path"))
  }
}
  1. 在ggrough中更改相应的未导出函数,以使用新定义的parse_polygons用于GeomArea层。

此外,我添加了GeomRibbon(原始版本似乎缺少,但实际上是GeomArea的更一般情况),并将GeomViolin从parse_areas移至parse_polygons,因为它面临相同的问题。

(注意:GeomSmooth也可能会失败,但我认为与GeomRibbon / GeomViolin相比,它的解析函数需要进行更多调整,而且我没有看到其使用案例...)

trace(ggrough:::parse_rough, edit = TRUE)

# paste the following function into the pop-up window
function (svg, geom) {
  rough_els <- list()
  if (geom %in% c("GeomCol", "GeomBar", "GeomTile", "Background")) {
    rough_els <- append(rough_els, parse_rects(svg))
  }
  if (geom %in% c("GeomSmooth", "Background")) {   # removed GeomArea / GeomViolin from here
    rough_els <- append(rough_els, parse_areas(svg))
  }
  if (geom %in% c("GeomArea", "GeomRibbon", "GeomViolin")) {  # new condition here
    rough_els <- append(rough_els, parse_polygons(svg))
  }
  if (geom %in% c("GeomPoint", "GeomJitter", "GeomDotPlot", "Background")) {
    rough_els <- append(rough_els, parse_circles(svg))
  }
  if (geom %in% c("GeomLine", "GeomSmooth", "Background")) {
    rough_els <- append(rough_els, parse_lines(svg))
  }
  if (geom %in% c("Background")) {
    rough_els <- append(rough_els, parse_texts(svg))
  }
  purrr::map(rough_els, ~purrr::list_modify(.x, geom = geom))
}

测试:

library(ggplot2)
library(ggrough)

uspopage <- gcookbook::uspopage
p <- ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + 
  geom_area(alpha=0.8) +
  scale_x_continuous(expand=c(0,0)); p
options <- list(GeomArea=list(fill_style="hachure", 
                              angle_noise=0.5,
                              gap_noise=0.2,
                              gap=1.5,
                              fill_weight=1))
get_rough_chart(p, options)

geom_area plot

geom_ribbon 的附加测试:

# using example from geom_ribbon help page
pp <- data.frame(year = 1875:1972, level = as.vector(LakeHuron)) %>%
  ggplot(aes(year)) +
  geom_ribbon(aes(ymin = level - 1, ymax = level + 1), 
              fill = "grey70")
options <- list(GeomRibbon=list(fill_style="hachure", 
                                angle_noise=0.5,
                                gap_noise=0.2,
                                gap=1.5,
                                fill_weight=1))
get_rough_chart(pp, options)

geom_ribbon 绘图

会话信息:

R version 4.0.1 (2020-06-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 15063)

Matrix products: default

locale:
[1] LC_COLLATE=English_Singapore.1252  LC_CTYPE=English_Singapore.1252   
[3] LC_MONETARY=English_Singapore.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Singapore.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gdtools_0.2.2 dplyr_1.0.0   ggrough_0.1.0 ggplot2_3.3.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5        cpp11_0.2.1       pillar_1.4.6      compiler_4.0.1   
 [5] plyr_1.8.6        Rmisc_1.5         forcats_0.5.0     tools_4.0.1      
 [9] boot_1.3-25       digest_0.6.25     jsonlite_1.7.1    lifecycle_0.2.0  
[13] tibble_3.0.3      gtable_0.3.0      lattice_0.20-41   pkgconfig_2.0.3  
[17] rlang_0.4.7       rstudioapi_0.11   yaml_2.2.1        xml2_1.3.2       
[21] withr_2.2.0       stringr_1.4.0     htmlwidgets_1.5.1 systemfonts_0.3.1
[25] generics_0.0.2    vctrs_0.3.4       grid_4.0.1        tidyselect_1.1.0 
[29] data.table_1.12.8 svglite_1.2.3.2   glue_1.4.2        R6_2.4.1         
[33] gcookbook_2.0     tidyr_1.1.0       reshape2_1.4.4    purrr_0.3.4      
[37] farver_2.0.3      magrittr_1.5      htmltools_0.5.0   scales_1.1.1     
[41] ellipsis_0.3.1    fortunes_1.5-4    colorspace_1.4-1  labeling_0.3     
[45] stringi_1.5.3     munsell_0.5.0     crayon_1.3.4     

哇,非常感谢你找出了这个问题——现在它运行得非常好! - bill999

5

我写这篇作为答案,因为在评论中写太长了。

我也为你尝试了 ggrough 包:

他们 Github 说明中的以下示例完美运行:

library(ggplot2)
count(mtcars, carb) %>%
  ggplot(aes(carb, n)) +
  geom_col() + 
  labs(title="Number of cars by carburator count") + 
  theme_grey(base_size = 16) -> p 
p

library(ggrough)
options <- list(
  Background=list(roughness=8),
  GeomCol=list(fill_style="zigzag", angle_noise=0.5, fill_weight=2))
get_rough_chart(p, options)

enter image description here

然而,当我尝试您的示例时:

library(hrbrthemes)
library(tidyverse)
library(gcookbook)
library(ggplot2)
library(ggrough)
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + 
    geom_area(alpha=0.8) +
    scale_fill_ipsum() +
    scale_x_continuous(expand=c(0,0)) +
    scale_y_comma() -> p 
p

到这里为止没问题,这将得到以下图像:

enter image description here

现在我们需要添加ggrough部分。

options <- list(GeomArea=list(fill_style="hachure", 
                              angle_noise=0.5,
                              gap_noise=0.2,
                              gap=1.5,
                              fill_weight=1))
get_rough_chart(p, options)

我得到了与你几乎相同的结果:

enter image description here

我得出了和你一样的结论:

因此,似乎ggrough在处理geom_area时出现了问题

甚至可以成功地使用相同的ggrough设置(唯一的区别是GeomCol)来处理上述汽车数量示例。

正如Github页面所说:

ggrough仍在开发中,仍存在重大错误。欢迎贡献!"

他们最后一次Git提交是2年前。

现在,由于您想创建的图表来自他们自己的示例,我非常确定它曾经起作用过。(我的意思是,为什么他们会上传虚假的图表)

因此,如果您真的绝望了,可以尝试:

  • 安装2年前的ggplot2版本(和其他必需的软件包)

  • 安装旧的ggrough包提交版本。也许他们提交了一个破坏了曾经工作的示例的提交。

或者您可以尝试修复他们的软件包 ;)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接