Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Homogenize ggplot abscissa size for 2 plots

I want to compare two variables against the X axis by displaying them on the same column.

For some reasons (not technical question), I can’t display both variables on the same graph.

When I display these two variables with different unit ranges with ggarrange, the one with the largest numbers appears to have more space for y labels and less space for the graph (lower abscissa size).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Do you know how I could display both graphs having the same greyzone size (same abscissa size)?

Thank you

### Import library
library(ggplot2)
library(ggpubr)

### Initiating data
dfTemp <- 
  data.frame(
    Xaxis=1:100,
    Variable1=rnorm(100, 100, 10),
    Variable2=rnorm(100, 1000000, 100000)
      )

### Initiating plots
scatterPlot1 <- 
  ggplot(data=dfTemp, aes(x=Xaxis, y=Variable1)) + 
  geom_line()
scatterPlot2 <- 
  ggplot(data=dfTemp, aes(x=Xaxis, y=Variable2)) + 
  geom_line()

### Display plots
ggarrange(scatterPlot1,
          scatterPlot2,
          nrow=2, ncol=1)

enter image description here

>Solution :

Use the align argument

dfTemp <- 
  data.frame(
    Xaxis=1:100,
    Variable1=rnorm(100, 100, 10),
    Variable2=rnorm(100, 1000000, 100000)
  )

### Initiating plots
scatterPlot1 <- 
  ggplot(data=dfTemp, aes(x=Xaxis, y=Variable1)) + 
  geom_line()
scatterPlot2 <- 
  ggplot(data=dfTemp, aes(x=Xaxis, y=Variable2)) + 
  geom_line()

### Display plots
ggarrange(scatterPlot1,
          scatterPlot2,
          nrow=2, ncol=1, align = "v")

plot

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading