आकृतियों को कैसे ढालें
क्या इसका उपयोग संभव है ggrough(https://xvrdm.github.io/ggrough/index.html) geom_sf(पसंदीदा) या संभावित रूप से बनाई गई छाया को आकार देने के लिए geom_polygon? इस प्रश्न को एक पूर्व प्रश्न के लिए देखें जो कि मेरे पास मौजूद प्लॉट का लुक देता है और Z.Lin द्वारा दिया गया उत्तर है जो पैकेज को संशोधित करता है ताकि यह वर्तमान संस्करण के साथ संगत हो सके ggplot2: इस ggplot2 प्लॉट को दोहराने में असमर्थ है ।
यहाँ एक मानचित्र का MWE बनाया गया है जिसका उपयोग करके geom_sfमैं (प्रत्येक व्यक्तिगत काउंटी) का उपयोग करना चाहूंगा 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
यह निम्नलिखित उत्पादन करता है:
मैं इस मानचित्र के काउंटियों का उपयोग कैसे कर सकता हूं ggroughया यह संभव नहीं है? ध्यान दें कि मुझे लगता है कि 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_sfया geom_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
यह उत्पादन करता है:
*** अंत अद्यतन
(यहाँ के साथ बनाई गई एक उदाहरण है, ggrough, मैं कैसे लग रहे करने के लिए काउंटी के छायांकन चाहते हैं:
यहां एक असफल प्रयास (फिर से Z.Lin के जवाब से कोड पर निर्भर है: इस ggplotot प्लॉट को दोहराने में असमर्थ है :
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, इसलिए यह रुचि के साथ-साथ geom_sfपसंद किया जाता है।
जवाब
library(magrittr)
library(ggplot2)
library(ggrough)
parse_roughका उपयोग कर बदलेंtrace
trace(ggrough:::parse_rough, edit=TRUE)
पॉपअप विंडो में, इसे पेस्ट करें ताकि parse_roughइसका उपयोग parse_sfहो GeomSf।
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)