簡體   English   中英

更改 ggiraph package 中的尺寸布局

[英]Change the size layout in ggiraph package

在下面的這個例子中(來自https://walker-data.com/census-r/mapping-census-data-with-r.html#linking-maps-and-charts ),我想增加map。 我怎樣才能做到這一點?

library(tidycensus)
library(ggiraph)
library(tidyverse)
library(patchwork)
library(scales)

vt_income <- get_acs(
  geography = "county",
  variables = "B19013_001",
  state = "VT",
  year = 2020,
  geometry = TRUE
) %>%
  mutate(NAME = str_remove(NAME, " County, Vermont"))

vt_map <- ggplot(vt_income, aes(fill = estimate)) + 
  geom_sf_interactive(aes(data_id = GEOID)) + 
  scale_fill_distiller(palette = "Greens",
                       direction = 1, 
                       guide = "none") + 
  theme_void()

vt_plot <- ggplot(vt_income, aes(x = estimate, y = reorder(NAME, estimate), 
                                 fill = estimate)) +
  geom_errorbar(aes(xmin = estimate - moe, xmax = estimate + moe)) +
  geom_point_interactive(color = "black", size = 4, shape = 21,
                         aes(data_id = GEOID)) +
  scale_fill_distiller(palette = "Greens", direction = 1,
                       labels = label_dollar()) + 
  scale_x_continuous(labels = label_dollar()) + 
  labs(title = "Household income by county in Vermont",
       subtitle = "2016-2020 American Community Survey",
       y = "",
       x = "ACS estimate (bars represent margin of error)",
       fill = "ACS estimate") + 
  theme_minimal(base_size = 14)

girafe(ggobj = vt_map + vt_plot, width_svg = 10, height_svg = 5) %>%
  girafe_options(opts_hover(css = "fill:cyan;"))

謝謝!

控制圖形大小的方法之一是設置比率並使用cowplot::plot_grid

library(cowplot)
girafe(ggobj = plot_grid(vt_map, vt_plot, ncol = 2, rel_widths = c(2, 3)), 
       width_svg = 10, height_svg = 6) %>%
  girafe_options(opts_hover(css = "fill:cyan;"))

在此處輸入圖像描述

我看到您正在使用拼湊來布置兩個子圖。 據我了解,這是一個拼湊相關的問題,它與 ggiraph 或 tidycensus 或 html 無關。 免責聲明:這里是 ggiraph 的合著者。

要回答您的問題:您可以簡單地使用 function patchwork::wrap_plots 並提供首選的相對寬度。 因此,代碼中的最后一行應該是:

girafe(ggobj = wrap_plots(vt_map, vt_plot, widths = c(1.5, 1)), width_svg = 10, height_svg = 5) %>%
  girafe_options(opts_hover(css = "fill:cyan;"))

這將使 map plot 比其他 plot 寬 1.5。

檢查拼湊文檔以發現布置兩個或更多地塊的幾種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM