Nate
Nate

Reputation: 565

Expand margins of ggplot

Apologies for the simplistic question, but I'm having trouble adjusting the size (width) of this plot to include all the data so that it doesn't look so squished. I've tried adjusting the margins and the width in png(), but nothing seems to work.

png("file_name.png",  units = "in", width = 10, height = 5,  res = 300)

ggplot(pred, aes(x = Longitude, y = Latitude)) +
  geom_raster(aes(fill = Fitted)) + 
  facet_wrap(~ CYR) +
  scale_fill_viridis(option = 'plasma',
                     na.value = 'transparent') +
  coord_quickmap() +
  theme(legend.position = 'top')
  # theme(plot.margin=grid::unit(c(0,20,0,20), "mm"))

dev.off()

enter image description here

Upvotes: 1

Views: 103

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26495

Do you need to use coord_quickmap() for some reason? Removing it 'fixes' the plot dimensions, e.g. using the palmerpenguins dataset:

library(ggplot2)
library(palmerpenguins)

p1 <- ggplot(penguins, aes(x = sex,
                     y = bill_length_mm,
                     fill = bill_depth_mm)) +
  geom_raster() +
  scale_fill_viridis_c(option = 'plasma',
                       na.value = 'transparent') +
  facet_wrap(~interaction(island, species, year)) +
  theme(legend.position = 'top') +
  coord_quickmap()

p1
#> Warning: Raster pixels are placed at uneven horizontal intervals and will be shifted
#> ℹ Consider using `geom_tile()` instead.
#> Warning: Removed 2 rows containing missing values (`geom_raster()`).

p2 <- ggplot(penguins, aes(x = sex,
                     y = bill_length_mm,
                     fill = bill_depth_mm)) +
  geom_raster() +
  scale_fill_viridis_c(option = 'plasma',
                       na.value = 'transparent') +
  facet_wrap(~interaction(island, species, year)) +
  theme(legend.position = 'top') #+
#  coord_quickmap()

p2
#> Warning: Raster pixels are placed at uneven horizontal intervals and will be shifted
#> ℹ Consider using `geom_tile()` instead.
#> Removed 2 rows containing missing values (`geom_raster()`).

Created on 2023-02-13 with reprex v2.0.2

Upvotes: 1

Related Questions