0
df_clip2_types = left_join(df_clip2, types) %>% na.omit()

fname <- dps[i]
fname1 <- paste0(fname, ".pdf")
pdf(file = fname1)

p_ag_cover <- ggplot() +
   geom_raster(data = df_clip2_types, aes(x = x, y = y, fill = cropgroup, group = cropgroup)) +
   scale_fill_manual(
     limits = c( "nuts", "smgrains", "fruit", "hay", "potatoes", "rice", "cotton", "fallow", "grapes", "veg", "corn", "othercrops", "Open Water", "Pasture", "other", "N/A"),
     labels = c( "Nuts", "Small grains", "Fruit", "Hay", "Potatoes", "Rice", "Cotton", "Fallow cropland", "Grapes", "Vegetables", "Corn", "Other crops", "Open Water", "Pasture", "other", "N/A"),
     values = c( "darkgoldenrod", "goldenrod", "darkred", "darkgreen", "black",  "violet","pink", "grey", "purple", "lightgreen", "gold", "lightblue", "darkblue", "springgreen3", "antiquewhite1", "dark grey")) + labs(title = fname) + background_grid() + theme(
       panel.background = element_rect(fill = "grey")
     ) 

plot(p_ag_cover, asp = 2)

dev.off()

Hello, I am plotting a raster using geom_raster() .

However, when I create a plot/export to PDF, I notice that the dimensions of the raster are distorted, and that instead of looking like it should (dimension-wise) on the left, the image looks like the right (too wide and short.

I am new to R and would appreciate any help. I've also attached the code here. I've tried changing asp values.

Raster plot that I am trying to replicate

enter image description here

aster plotting output that I get with my code, it's too short and wide

enter image description here

Scale that I want to achieve with raster plotting

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
  • Use `ggsave()` to save plots to disk, you may find that will solve your problem. – Phil Jul 16 '22 at 00:50
  • It does not appear that `pdf()` respects your `asp=` argument. It only allows you to specify width= and height= with a default value of 7 for each so you are getting a square plot (counting the space for the legend on the right). Changing the width= value to something smaller, e.g. 5 should work, but it will take a bit of trial and error to get it right. – dcarlson Jul 16 '22 at 16:01

0 Answers0