1

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!

Meo
  • 140
  • 1
  • 9
  • You tagged this as RMD, but nothing in the code suggests it's RMD. That might be really important—in terms of getting the answer you may really want. For example, RMD output `html_document` has a wicked small `max-width` for the main container. So you could change the size in the widget all day long and it won't look any different when you knit. – Kat Jun 08 '22 at 03:24
  • @Kat Thank you! I've removed the rmakrdown tag and changed it to html instead. Do you have an idea on how i change the size of the map? The whole plot itself is fine, i just want to increase the "vt_map" portion. Thanks. – Meo Jun 08 '22 at 04:04

2 Answers2

2

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

Kat
  • 15,669
  • 3
  • 18
  • 51
0

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.

sigmapi
  • 11
  • 1