-1

I'm trying to plot multiple columns on one plot, using the following code:

df.m <- melt(stkPres, "date")

ggplot(df.m, aes(date, value)) + 
  geom_line() + 
  facet_wrap(~variable, scales = "free")

Which returns a graph like this:

enter image description here

How can I make the plots sized more appropriately?

mk2080
  • 872
  • 1
  • 8
  • 21
  • 1
    Try making your graphics device larger? I'm not sure what you really want. That's a lot of data for such a small space. – MrFlick Jan 20 '21 at 20:21
  • Plot over multiple pages, see function `facet_multiple` in package `ggplus` or `facet_wrap_paginate` in package `ggforce`. See also several SO questions, say, [this one](https://stackoverflow.com/questions/45475249/save-facet-wrap-on-multiple-pages-using-ggforce-ggplus). – Rui Barradas Jan 20 '21 at 20:22

1 Answers1

1

Here are some options:

  • Don't let scales be free, plot the data on common axes. This will remove the labels between panels.
  • Remove the strip background.
  • Reduce the size and margin of the strip text.
  • Reduce the spacing between panels.

Example below:

library(ggplot2)

ggplot(diamonds, aes(carat, price)) +
  geom_point() +
  facet_wrap(~ interaction(clarity, color)) +
  theme(strip.background = element_blank(),
        strip.text = element_text(size = rel(0.8), margin = margin()),
        panel.spacing = unit(3, "pt"))

Created on 2021-01-20 by the reprex package (v0.3.0)

It seems your x-axis doesn't need to be free if the dates are common. If not having a free y-axis skews your data in weird ways, considering calculating an index instead of the plain data.

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • this is super helpful! Is it possible to make the entire graph longer so that there is more space for each plot too? – mk2080 Jan 20 '21 at 20:57
  • The only thing that would help is to rescale the graphics device. If you are in RStudio you can click the zoom button. If you're exporting the plot via `ggsave()` you can make the size larger. – teunbrand Jan 20 '21 at 21:05