Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

ggplot2 - How to set adequate space for facet wrap in R

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...