【问题标题】:Unable to replicate this ggplot2 plot无法复制此 ggplot2 图
【发布时间】:2020-09-06 00:05:22
【问题描述】:

我无法从 ggrough 库 (https://xvrdm.github.io/ggrough/articles/Customize%20chart.html) 复制示例。特别是,我试图复制以下情节(减去字体方面):

代码来自上面“幼儿园”标题下的同一链接。

我正在使用以下代码:

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)

但是,我无法复制上述内容。这是我得到的:

再一次,我并不担心字体,但确实想让阴影 geom_area 工作。它目前根本不渲染。作为参考,这里是 p 对象产生的结果(即,在经过 ggrough 处理之前的图):

还请注意,我可以复制“蓝图”示例,该示例使用 geom_col。因此,ggrough 处理 geom_area 似乎出了点问题,但不确定。

【问题讨论】:

  • “ggrough”包不可用(对于 R 版本 4.0.1)-似乎不在 CRAN 上
  • 啊,仅来自 github
  • 试过也没有成功。从我假设的包描述来看,它在当前的 Github 版本中不起作用 - ggrough 是一项正在进行的工作,仍然有很大的错误。欢迎投稿!”
  • 好点 - 请参阅此页面以获取安装说明:github.com/xvrdm/ggrough
  • 又做了一次检查(把它作为你的答案,因为它太长了)。但我几乎得出了和你一样的结论——这是 geom_area 的问题。甚至快速查看了包源,但乍一看并没有找到适合您的快速解决方案。

标签: r ggplot2 ggrough


【解决方案1】:

ggrough 包不适用于geom_area 的当前版本的 ggplot2,因为它会在名为“polyline”的 xml 节点中查找目标区域。在旧版本的 ggplot2 中,这可能工作得很好,因为每个区域都被折线包围。然而,在最近的版本中,情况不再如此(请参阅breaking changes under 3.3.0)。

看看以下是否适合你:

  1. 定义查找“多边形”而不是“折线”的 parse_* 函数版本。
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_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)

会话信息:

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     

【讨论】:

  • 哇,非常感谢您解决这个问题 - 现在效果很好!
【解决方案2】:

我写这个是为了回答,因为对于 cmets 来说太长了。

我还为您尝试了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)

然而,当我尝试你的例子时:

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

到这里为止没关系,这将给出以下图像:

现在我们需要添加 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)

我得到的结果和你差不多:

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

所以 ggrough 处理 geom_area 似乎出了点问题

对于上面的汽车数量示例,甚至可以成功地为 ggrough 部分使用所有相同的设置(唯一的区别是 GeomCol)。

正如 Github 页面所说:

ggrough 是一项正在进行中的工作,仍然存在很大的错误。欢迎投稿!”

他们在 2 年前进行了最后一次 Git 提交。

现在,由于您要创建的情节来自他们自己的示例,我很确定它曾经有效。 (我的意思是他们为什么要上传假图)

所以如果你真的很绝望,你可以试试:

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

  • ggrough 包安装较旧的提交版本。也许他们做了一个提交,打破了曾经有效的例子。

或者你尝试修复他们的包;)

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 2019-08-11
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多