无法复制此ggplot2图 [英] Unable to replicate this ggplot2 plot

查看:104
本文介绍了无法复制此ggplot2图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am unable to replicate an example from the ggrough library (https://xvrdm.github.io/ggrough/articles/Customize%20chart.html). In particular, I am trying to replicate the following plot (minus the font aspects):

该代码来自上方幼儿园"下方的同一链接.标头.

The code is from the same link above under the "Kindergarten" header.

我正在使用以下代码:

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)

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

However, I am unable to replicate the above. Here is what I get:

同样,我不担心字体,但确实希望使阴影的geom_area正常工作.当前完全不渲染.作为参考,以下是p对象产生的结果(即,在经过ggrough处理之前的绘图):

Again, I am not worried about the fonts, but do want to get the shaded geom_area to work. It currently doesn't render at all. For reference, here is what the p object yields (i.e., the plot before it goes through the ggrough processing):

还要注意,我能够复制蓝图"例如,使用geom_col.因此,看来ggrough处理geom_area时出了点问题,但不确定.

Also note that I am able to replicate the "Blueprint" example, which uses geom_col. So it appears that something is going wrong with ggrough processing the geom_area, but not sure.

推荐答案

ggrough软件包不能与geom_area的当前版本的ggplot2一起很好地使用,因为它会在xml节点中查找名称为"; polyline".在旧版本的ggplot2中,这可能工作得很好,因为每个区域都由多义线包围.但是,在最新版本中,情况不再如此(请参见在3.3.0下进行重大更改).

The ggrough package doesn't work well with the current version of ggplot2 for geom_area, because it looks for the target area in xml nodes bearing the name "polyline". In older versions of ggplot2, this probably worked fine, because each area was enclosed by a polyline. In more recent versions, however, this is no longer the case (see breaking changes under 3.3.0).

查看以下各项是否对您有用:

See if the following works for you:

  1. 定义parse_*函数的版本,该版本查找"polygon"而不是"polyline".
  1. Define a version of the parse_* function that look for "polygon", rather than "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图层.
  1. Change the corresponding un-exported function in ggrough to use the newly defined parse_polygons for GeomArea layers.

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

In addition, I've added in GeomRibbon (which appeared to be missing from the original but is really a more general case of GeomArea) & moved GeomViolin over from parse_areas to parse_polygons, because it faces the same issue.

(注意:GeomSmooth也可能会损坏,但与GeomRibbon/GeomViolin相比,我认为它的解析功能将需要更多的调整,而且我看不到有用例...)

(Note: GeomSmooth will probably break too, but I think its parse function will take a bit more tweaking, compared to GeomRibbon / GeomViolin, & I'm not seeing a use case for it...)

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     

这篇关于无法复制此ggplot2图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆