0

I am trying to make a combined plot with the same y-limits. In here there are two facetwrapped boxplots. I would normally set the y-limits manually at the same level, but this messed up the medians and thus the direction of the line in the left plot (see image). Therefore the scales in this plot are set scale = "free_y"

My question: Is there a way to copy the y-axis from a plot, and use this exact scale on another plot? / What are the alternatives to find the exact y-limits a boxplot uses?

For now I have tried obtain the y-limits of the left facetwrapped plot (see image), and use this information to manually set the same y-limit on the right facetwrapped plot(see image) (This plot I am able to set y-limits manually without messing up the plot).

The combined plot with needed simular y-axis (name left plot = AbundancePlotVH , name rightplot = boxplotVHAbundance. I would like to make the y-axis on the rightplot the same as the one of the leftplot

To obtain the exact Y-limits I have tried:

  • par("usr") but this gave me 0 1 0 1 values
  • Find the min and max value of the data from the leftplot (see image) through the code below
Range <- range(c(totalVH$MeanAbundance)) 
Range

This helped, but did not make the axis exactly the same. I've been searching for alternative solutions, but cant seem to find one that works for me..

Here the code I use for my plot. It combines 2 individual facetwrapped boxplots (AbundancePlotVH & boxplotVHAbundance)

prow<- plot_grid(AbundancePlotVH + theme(legend.position="none"), boxplotVHAbundance +  theme(legend.position="none", axis.text.y = element_blank(), axis.ticks.y = element_blank(), 
VH<- plot_grid(title, prow, legend, ncol =  1, rel_heights = c(1, 10), axis = "l", align = "v")
VH
stefan
  • 90,330
  • 6
  • 25
  • 51
Thimo
  • 1
  • 1
  • 1
    Using the same limits (and using range) for both plots is the way to go. IMHO the issue with your plots is the alignment. Perosnally that's why I use `patchwork` to combine plots as from my experience it does a fairly good job to align seperate plots. – stefan Dec 08 '22 at 15:06
  • 1
    If you had a ggplot saved in an object called `g`, you could then do `b <- ggplot_build(g)` and then `b$layout$panel_params[[1]]$y.range` would give you the range of the y-axis in the first panel. the usual `par()` calls don't return the appropriate values for a ggplot. I found [this answer](https://stackoverflow.com/questions/7705345/how-can-i-extract-plot-axes-ranges-for-a-ggplot2-object/57427773#57427773) helpful. – DaveArmstrong Dec 08 '22 at 16:01

0 Answers0