ไม่สามารถจำลองพล็อต ggplot2 นี้ได้

Sep 06 2020

ฉันไม่สามารถจำลองตัวอย่างจาก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แต่ไม่แน่ใจ

คำตอบ

8 Z.Lin Sep 10 2020 at 15:04

แพ็กเกจ ggrough ทำงานได้ไม่ดีกับ ggplot2 เวอร์ชันปัจจุบันgeom_areaเนื่องจากมองหาพื้นที่เป้าหมายในโหนด xml ที่มีชื่อ "polyline" ใน ggplot2 เวอร์ชันเก่าสิ่งนี้อาจใช้งานได้ดีเนื่องจากแต่ละพื้นที่ถูกล้อมรอบด้วยโพลีไลน์ อย่างไรก็ตามในเวอร์ชันล่าสุดจะไม่เป็นเช่นนั้นอีกต่อไป (ดูการเปลี่ยนแปลงที่ไม่สมบูรณ์ภายใต้ 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     
5 SteffenMoritz Sep 08 2020 at 10:05

ฉันเขียนสิ่งนี้เป็นคำตอบเพราะมันจะยาวเกินไปสำหรับความคิดเห็น

ฉันลองใช้ggrough แพ็คเกจสำหรับคุณด้วย:

ตัวอย่างต่อไปนี้จาก github readme ทำงานได้อย่างสมบูรณ์:

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 เป็นงานระหว่างดำเนินการและยังมีข้อบกพร่องใหญ่ ยินดีต้อนรับการมีส่วนร่วม! "

พวกเขาทำ Git ครั้งสุดท้ายเมื่อ 2 ปีที่แล้ว

ตอนนี้เนื่องจากพล็อตที่คุณต้องการสร้างนั้นมาจากตัวอย่างของพวกเขาเองฉันค่อนข้างแน่ใจว่ามันได้ผล (ฉันหมายความว่าทำไมพวกเขาถึงอัปโหลดพล็อตปลอม)

ดังนั้นหากคุณหมดหวังจริงๆคุณสามารถลอง:

  • ติดตั้ง ggplot2 เวอร์ชัน (และแพ็คเกจอื่น ๆ ที่จำเป็น) จาก 2 ปีที่แล้ว

  • ติดตั้งคอมมิตเวอร์ชันเก่าจากggroughแพ็คเกจ บางทีพวกเขาอาจทำสิ่งที่ผิดตัวอย่างที่เคยใช้งานได้

หรือคุณพยายามแก้ไขแพ็คเกจของพวกเขา;)