r - 如何着色形状

标签 r ggplot2 ggrough

是否可以使用 ggrough ( https://xvrdm.github.io/ggrough/index.html ) 为 geom_sf 创建的形状着色(首选)或可能 geom_polygon ?请参阅此问题以获取先前的问题,该问题给出了我想到的情节的外观以及 Z.Lin 的随附答案,该答案修改了包以使其与 ggplot2 的当前版本兼容。 :Unable to replicate this ggplot2 plot .
这是使用 geom_sf 创建的 map 的 MWE我想使用 ggrough 遮蔽(每个单独的县) :

library(tidyverse)
library(magrittr)
library(ggplot2)
library(ggrough)
library(RColorBrewer)
library(tidycensus)
library(viridis)
#install.packages("devtools") # if you have not installed "devtools" package
#devtools::install_github("xvrdm/ggrough")
library(hrbrthemes)

#get nevada shapefile
counties <- get_acs(
    geography = "county", year = 2018, geometry = TRUE,
    variables = "B19013_001", keep_geo_vars=TRUE
) %>% filter(STATEFP=="32")
counties$GEOID <- as.integer(counties$GEOID)
#############

a <- ggplot() +
    geom_sf(data = counties, aes(fill = estimate)) + 
    scale_fill_viridis(discrete=FALSE, name="", guide=FALSE) +  
    theme_bw() +
    theme(legend.position = c(0.15, .15)) +
    theme(plot.subtitle = element_text(hjust = 0.8, vjust=-10, size=30)) +  
    theme(panel.background = element_rect(fill = 'white')) +
    theme(panel.grid = element_blank(),axis.title = element_blank(),
          axis.text = element_blank(),axis.ticks = element_blank(),
          panel.border = element_blank())+
    theme(legend.position = c(0.25, .15), legend.key.size = unit(2,"line"),
          legend.title=element_text(size=16), 
          legend.text=element_text(size=14), 
          legend.direction = "vertical", 
          legend.box = "horizontal") +
    labs(caption = "")
a 
这会产生以下结果:
enter image description here
如何使用 ggrough 为该 map 的县着色或者这是不可能的?请注意,我认为 ggrough可以处理geom_col, geom_bar, geom_tile, geom_geom_area, geom_ribbon, geom_violin, geom_point, geom_jitter, geom_dotplot, geom_line, and geom_smooth ,但我不确定 geom_sfgeom_polygon ;如果没有,添加它们会很容易吗?
***更新
这是另一个例子,取自 https://ggplot2.tidyverse.org/reference/ggsf.html :
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
b <- ggplot(nc) +
    geom_sf(aes(fill = AREA))
b
这产生:
enter image description here
***结束更新
(这是一个使用 ggrough 创建的示例,说明我希望县的阴影看起来如何:
enter image description here
)
这是一次失败的尝试(再次依赖 Z.Lin 的回答中的代码: Unable to replicate this ggplot2 plot ):
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"))
    }
}

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))
}

options <- list(GeomSf=list(fill_style="hachure", 
                              angle_noise=0.5,
                              gap_noise=0.2,
                              gap=1.5,
                              fill_weight=1))
get_rough_chart(a, options)
这会产生错误消息:
Error in `*tmp*`[[i]] : subscript out of bounds
***更新
或者用第二个例子:
options <- list(GeomSf=list(fill_style="hachure", 
                          angle_noise=0.5,
                          gap_noise=0.2,
                          gap=1.5,
                          fill_weight=1))
get_rough_chart(b, options)
同样的错误。
***结束更新。
另请注意,可以使用 geom_polygon 创建 map 。 ,所以这也很有趣,不过 geom_sf是首选。

最佳答案

library(magrittr)
library(ggplot2)
library(ggrough)
替换 parse_rough使用 trace
trace(ggrough:::parse_rough, edit=TRUE)
在弹出窗口中,粘贴此内容以便 parse_rough将使用 parse_sfGeomSf .
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("GeomArea", "GeomViolin", "GeomSmooth", 
                  "Background")) {
    rough_els <- append(rough_els, parse_areas(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))
  }
  if (geom %in% c("GeomSf")) {
    rough_els <- append(rough_els, parse_sf(svg))
  }
  purrr::map(rough_els, ~purrr::list_modify(.x, geom = geom))
}
创建函数 parse_sf .
parse_sf <- function (svg) {
  shape <- "path"
  keys <- NULL
  ggrough:::parse_shape(svg, shape, keys) %>% {
    purrr::map(., 
               ~purrr::list_modify(.x, 
                                   points = .x$d, 
                                   shape = "path"
               ))
  }
}
创建所需的绘图
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
b <- ggplot(nc) +
  geom_sf(aes(fill = AREA))
b


options <- list(GeomSf=list(fill_style="hachure", 
                            angle_noise=0.5,
                            gap_noise=0.2,
                            gap=1.5,
                            fill_weight=1))
get_rough_chart(b, options)
enter image description here

关于r - 如何着色形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64031046/

相关文章:

r - 惰性求值和 Promise 数据结构

R:提取关键字后的值和行(文本文件挖掘)

r - 在 R 中用返回而不是打印来抑制消息?

r - 匹配 ODFWeave 和 ggplot2 图像分辨率/大小

r - ggplot2:在同一页面中绘制多个直方图,但一个具有倒置坐标

r - 无法复制此 ggplot2 图

r - 在 R 或 R Studio(服务器)中交互输入密码?

用额外的随机数替换随机生成的矩阵中的值