1

I use the following code

library(ggplot2)

N = 30
M = 16

dat <- stack(as.data.frame(matrix( rnorm(N*M,mean=0,sd=2), N, M)))
             
p <- ggplot(dat,aes(x = ind, y = values, fill=ind)) + 
  geom_boxplot() +
  theme(legend.position="none")

p + coord_flip()

to produce something that looks like this enter image description here

But in fact I have 160 variables to plot (so M = 160, not M =16), and I would like to "stack" the above plots with a similar "look and feel", i.e. a plot with a "height" about 10 times the one above (or 10 times the width) and not the one below.

enter image description here

Portland
  • 185
  • 7
  • Could you please define the term "ratio"? Do you mean the 'inter-quartile range'? So the boxplots with the largest inter-quartile range are at the top and the smallest inter-quartile range at the bottom of the graph? – Peter Jun 21 '20 at 13:24
  • I think OP means "produce a graphic with an aspect ratio of 10:1 (height:width). – Limey Jun 21 '20 at 13:26
  • I modified, to make it clearer. It was the ration of height and width of the plot. – Portland Jun 21 '20 at 13:26
  • In which case `... theme(aspect.ratio=10)` – Limey Jun 21 '20 at 13:28
  • @Limey I tried that with theme(aspect.ratio=10) but the resolution is lost and the output horrible. – Portland Jun 21 '20 at 13:31

1 Answers1

1

Finish with

 p <- p + theme(aspect.ratio=10)

Or whatever value meets your needs.

A markdown solution will produce a long thin plot that is perfectly legible. Unfortunately, posting the raw code in here is problematic: the chunk headers are processed by SO's markdown engine and I don't know how to escape them. So I'll have to describe it rather than paste it.

fig.height and fig.width specify dimensions in inches so use the following chunk header:

{r, fig.height=36, fig.width=3.2}

Then your chunk code is

library(ggplot2)

N = 30
M = 160

dat <- stack(as.data.frame(matrix( rnorm(N*M,mean=0,sd=2), N, M)))
             
ggplot(dat,aes(x = ind, y = values, fill=ind)) + 
  geom_boxplot() +
  theme(legend.position="none") + 
  coord_flip() +
  theme(aspect.ratio=16)

I've not posted the resultant image for obvious reasons!

Play around with the values of fig.height, fig.width and aspect.ratio until you get the look you like.

Limey
  • 10,234
  • 2
  • 12
  • 32
  • Thanks. But try the example code with that and see the output, it is not pretty. – Portland Jun 21 '20 at 13:31
  • Your fundamental problem is that you have 160 groups. That's going to produce a very tall, thin graph whatever you do. For example, if each box and whisker plot occupies half an inch of vertical space, you're going to get an image 80 inches high. If each b&w occupies 1/32 of an inch, your plot is 5 inches high will fit on an A4 (or Letter) page but the boxes will be so small they can't be read or they will overlap. Something has to give. If you cannot compromse, then I don't see a solution. – Limey Jun 21 '20 at 13:43
  • Thanks. I don't want to fit on A4 or letter page but on an HTML page where you can scroll as much as you want. In other words, I would like 10 graphs like the first one, stacked above each other. – Portland Jun 21 '20 at 13:49
  • Then just play with `aspect.ratio` til you get the look you want. You may need to play with the pixel dimensions of the plot to get what you want. And remember that RStudio previews scale to the size of the window. I'll update my answer. – Limey Jun 21 '20 at 13:51
  • If you could check the output of your solution (and maybe paste it) that would be super useful ,thanks. – Portland Jun 21 '20 at 13:52
  • Sadly, your post has been closed as a duplicate. Which, strictly speaking, it is. – Limey Jun 21 '20 at 13:54