Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automatically align the axis limits for different ggplots

Tags:

r

ggplot2

I am running regression and presenting the results using ggplots. Generally, the intention is to see the dynamic effect of a policy that happened in period 2.

I am plotting the regression results, the dataframe I created from the regression functions looks like

dfplot1 <- data.frame(coef=c(0.05,0,0.1,0.15,0.2), 
                      se=c(0.1,0,0.1,0.1,0.1), 
                      period=1:5)
dfplot2 <- data.frame(coef=c(0.05,0,0.12,0.18,0.24), 
                      se=c(0.12,0,0.12,0.12,0.12), 
                      period=1:5)

And I plot the coefficients with standard errors using

library(ggplot2)
library(patchwork)

p1 <- ggplot(dfplot1)+
  geom_point(aes(x=period, y=coef))+
  geom_segment(aes(x=period, xend=period, y=coef-1.96*se, yend=coef+1.96*se))+
  geom_segment(aes(x=period, xend=period, y=coef-1.96*se, yend=coef+1.96*se))+ 
  labs(x='Year', y='Coefficients')+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme(panel.background = element_blank())
p2 <- ggplot(dfplot2)+
  geom_point(aes(x=period, y=coef))+
  geom_segment(aes(x=period, xend=period, y=coef-1.96*se, yend=coef+1.96*se))+
  geom_segment(aes(x=period, xend=period, y=coef-1.96*se, yend=coef+1.96*se))+ 
  labs(x='Year', y='Coefficients')+
  geom_hline(yintercept = 0, linetype = "dashed")+
  theme(panel.background = element_blank())
p1|p2

The figure looks like, enter image description here

I can use ylim in each ggplot to set the y axis limit and make them the same like

p1+ylim(-0.5,0.5)
p2+ylim(-0.5,0.5)

enter image description here

To better visualize and compare the coefficients in these two regressions with different covariates, is there a way to automatically find the best fit and align the axes of the two ggplots?

Thank you very much!

like image 972
xxx Avatar asked Jan 22 '26 21:01

xxx


2 Answers

You could facet_wrap:

library(tidyverse)

dfplot1 <- data.frame(coef=c(0.05,0,0.1,0.15,0.2), 
                      se=c(0.1,0,0.1,0.1,0.1), 
                      period=1:5)
dfplot2 <- data.frame(coef=c(0.05,0,0.12,0.18,0.24), 
                      se=c(0.12,0,0.12,0.12,0.12), 
                      period=1:5) 

df <- bind_rows(dfplot1, dfplot2, .id = "id")

df |> ggplot(aes(period, coef)) +
  geom_point() +
  geom_segment(aes(xend = period, y = coef - 1.96 * se, yend = coef + 1.96 * se)) +
  labs(x = "Year", y = "Coefficients") +
  geom_hline(yintercept = 0, linetype = "dashed") +
  facet_wrap(~id) +
  theme(panel.background = element_blank())

Created on 2022-05-27 by the reprex package (v2.0.1)

like image 73
Carl Avatar answered Jan 25 '26 17:01

Carl


You can harvest the ranges from both plots using layer_scales, then get the overall range of the concatenated result to use in ylim. This avoids the need for guessing.

lim <- range(c(layer_scales(p1)$y$range$range, layer_scales(p2)$y$range$range))

p1 + ylim(lim) | p2 + ylim(lim)

enter image description here

like image 23
Allan Cameron Avatar answered Jan 25 '26 17:01

Allan Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!