简体   繁体   English

更改 ggiraph package 中的尺寸布局

[英]Change the size layout in ggiraph package

In this example below from (from https://walker-data.com/census-r/mapping-census-data-with-r.html#linking-maps-and-charts ), i would like to increase the width of the map.在下面的这个例子中(来自https://walker-data.com/census-r/mapping-census-data-with-r.html#linking-maps-and-charts ),我想增加map。 How can i do this?我怎样才能做到这一点?

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;"))

Thank you!谢谢!

One of the ways you can control the size of the graph is by setting a ratio and using cowplot::plot_grid .控制图形大小的方法之一是设置比率并使用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;"))

在此处输入图像描述

I see that you are using patchwork to layout the two sub-plots.我看到您正在使用拼凑来布置两个子图。 As I understand it, this is a patchwork related question, it has nothing to do with ggiraph or tidycensus or html.据我了解,这是一个拼凑相关的问题,它与 ggiraph 或 tidycensus 或 html 无关。 Disclaimer: co-author of ggiraph here.免责声明:这里是 ggiraph 的合著者。

To answer your question: You can simply use the function patchwork::wrap_plots and supply the preferred relative widths.要回答您的问题:您可以简单地使用 function patchwork::wrap_plots 并提供首选的相对宽度。 So, the final line in your code should be:因此,代码中的最后一行应该是:

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;"))

That will make the map plot 1.5 wider than the other plot.这将使 map plot 比其他 plot 宽 1.5。

Check patchwork documentation to discover several ways to layout two or more plots.检查拼凑文档以发现布置两个或更多地块的几种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM