Change the size layout in ggiraph package

429 Views Asked by At

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. 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!

2

There are 2 best solutions below

0
On

One of the ways you can control the size of the graph is by setting a ratio and using 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;"))

enter image description here

0
On

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. Disclaimer: co-author of ggiraph here.

To answer your question: You can simply use the function patchwork::wrap_plots and supply the preferred relative widths. 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.

Check patchwork documentation to discover several ways to layout two or more plots.