Home > OS >  Align diagrams by using ggarrange
Align diagrams by using ggarrange

Time:03-04

How can I align two red lines (y = 0)? I modified the plot of male to scale_y_continuous(breaks = seq(-0.06, 0.02, 0.02). But it didn't work. It still does not align. All of annotate is to construct the 95% confidence interval. Also, every single point represents the coefficient of estimates.

Below is my code.

male <- ggplot()  
      geom_hline(yintercept = 0, color = 'red', lwd = 1)  
      theme_minimal()  
      labs(x = '年資', y = '失業率和年資虛擬變數之係數', title = '男性')  
      theme(text = element_text(family ='黑體-繁 中黑'),
            plot.title = element_text(size = rel(2), face = 'bold', hjust = 0.5),
            axis.title = element_text(size = rel(1.5), face = 'bold',
                                      colour = 'black'),
            axis.text = element_text(size = rel(1.5), face = 'bold',
                                     colour = 'black'),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.background = element_blank())  
      scale_x_continuous(breaks = seq(0, 14))  
      annotate('pointrange', x = 0, y = -.0447358, ymin = -.0497855,
               ymax = -.0396861, size = 0.5)  
      annotate('pointrange', x = 1, y = -.0180776, ymin = -.0230804,
               ymax = -.0130748, size = 0.5)  
      annotate('pointrange', x = 2, y = -.016423, ymin = -.0213736,
               ymax = -.0114724, size = 0.5)  
      annotate('pointrange', x = 3, y = -.0129881, ymin = -.0178787,
               ymax = -.0080975, size = 0.5)  
      annotate('pointrange', x = 4, y = -.0103175, ymin = -.015139,
               ymax = -.0054961, size = 0.5)  
      annotate('pointrange', x = 5, y = -.0082522, ymin = -.0130095,
               ymax = -.0034949, size = 0.5)  
      annotate('pointrange', x = 6, y = -.0071008, ymin = -.0118032,
               ymax = -.0023985, size = 0.5)  
      annotate('pointrange', x = 7, y = -.0070145, ymin = -.0116721,
               ymax = -.0023569, size = 0.5)  
      annotate('pointrange', x = 8, y = -.0056933, ymin = -.0103182,
               ymax = -.0010684, size = 0.5)  
      annotate('pointrange', x = 9, y = -.0047233, ymin = -.0093261,
               ymax = -.0001205, size = 0.5)  
      annotate('pointrange', x = 10, y = -.003273, ymin = -.0078732,
               ymax = .0013272, size = 0.5)  
      annotate('pointrange', x = 11, y = .0006229, ymin = -.0040254,
               ymax = .0052711, size = 0.5)  
      annotate('pointrange', x = 12, y = .0037801, ymin = -.0010039,
               ymax = .0085641, size = 0.5)  
      annotate('pointrange', x = 13, y = .0032255, ymin = -.0017867,
               ymax = .0082376, size = 0.5)  
      annotate('pointrange', x = 14, y = .0029918, ymin = -.0026945, ymax = .008678,
               size = 0.5)
    
    female <- ggplot()  
      geom_hline(yintercept = 0, color = 'red', lwd = 1)  
      theme_minimal()  
      labs(x = '年資', y = '失業率和年資虛擬變數之係數', title = '女性')  
      theme(text = element_text(family ='黑體-繁 中黑'),
            plot.title = element_text(size = rel(2), face = 'bold', hjust = 0.5),
            axis.title = element_text(size = rel(1.5), face = 'bold',
                                      colour = 'black'),
            axis.text = element_text(size = rel(1.5), face = 'bold',
                                     colour = 'black'),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.background = element_blank())  
      scale_x_continuous(breaks = seq(0, 14))  
      annotate('pointrange', x = 0, y = -.0557045, ymin = -.0594948,
               ymax = -.0519143, size = 0.5)  
      annotate('pointrange', x = 1, y = -.0237874, ymin = -.0275535,
               ymax = -.0200212, size = 0.5)  
      annotate('pointrange', x = 2, y = -.0186763, ymin = -.0224115,
               ymax = -.0149412, size = 0.5)  
      annotate('pointrange', x = 3, y = -.0124177, ymin = -.0161171,
               ymax = -.0087183, size = 0.5)  
      annotate('pointrange', x = 4, y = -.0069645, ymin = -.0106195,
               ymax = -.0033096, size = 0.5)  
      annotate('pointrange', x = 5, y = -.0025661, ymin = -.0061792,
               ymax = .0010469, size = 0.5)  
      annotate('pointrange', x = 6, y = .0009979, ymin = -.0025787,
               ymax = .0045745, size = 0.5)  
      annotate('pointrange', x = 7, y = .0044623, ymin = .000915,
               ymax = .0080096, size = 0.5)  
      annotate('pointrange', x = 8, y = .0070437, ymin = .0035184,
               ymax = .010569, size = 0.5)  
      annotate('pointrange', x = 9, y = .0096577, ymin = .0061462,
               ymax = .0131692, size = 0.5)  
      annotate('pointrange', x = 10, y = .0120116, ymin = .0085022,
               ymax = .0155209, size = 0.5)  
      annotate('pointrange', x = 11, y = .0158938, ymin = .0123547,
               ymax = .0194329, size = 0.5)  
      annotate('pointrange', x = 12, y = .0227411, ymin = .0191273,
               ymax = .0263549, size = 0.5)  
      annotate('pointrange', x = 13, y = .0253041, ymin = .0215681,
               ymax = .0290401, size = 0.5)  
      annotate('pointrange', x = 14, y = .0261749, ymin = .0220729, ymax = .0302769,
               size = 0.5)
    
    ggarrange(male, female)

enter image description here

CodePudding user response:

You could try some thing like:

ggarrange(male, female, align = "hv")

As I do not have ggpubr installed on my machine at the moment, I cannot test it. However, if you arrange your data in a right format, your code could be much simpler. Because you have many data points, using annotate to enter each point is a pretty painful task. I extracted the data from your code and put them in a dataframe, df and use facet_wrap to separate two groups:

df %>% ggplot(aes(x = x, y = y) ) 
  geom_hline(yintercept = 0, color = 'red', lwd = 1)  
  theme_minimal()  
  labs(x = '年資', y = '失業率和年資虛擬變數之係數')  
  theme(text = element_text(family ='黑體-繁 中黑'),
        strip.text = element_text(family ='黑體-繁 中黑', 
                                  size = rel(1.5), face = 'bold'
                                  ),
        plot.title = element_text(size = rel(2), face = 'bold', hjust = 0.5),
        axis.title = element_text(size = rel(1.5), face = 'bold',
                                  colour = 'black'),
        axis.text = element_text(size = rel(1.5), face = 'bold',
                                 colour = 'black'),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank())  
  scale_x_continuous(breaks = seq(0, 14))  
  geom_pointrange(aes(ymin = ymin, ymax = ymax)) 
  facet_wrap(vars(gender))

enter image description here

Extracted data: 
 male <- data.frame(x = 0, y = -.0447358, ymin = -.0497855,  ymax = -.0396861, gender = '男性') %>% 
  add_row(x = 1, y = -.0180776, ymin = -.0230804, ymax = -.0130748, gender = '男性') %>% 
  add_row(x = 2, y = -.016423, ymin = -.0213736,   ymax = -.0114724, gender = '男性') %>% 
   add_row(x = 3, y = -.0129881, ymin = -.0178787,  ymax = -.0080975, gender = '男性')%>%
   add_row(x = 4, y = -.0103175, ymin = -.015139,   ymax = -.0054961, gender = '男性')%>%
   add_row(x = 5, y = -.0082522, ymin = -.0130095, ymax = -.0034949, gender = '男性')%>%
   add_row(x = 6, y = -.0071008, ymin = -.0118032, ymax = -.0023985, gender = '男性')%>%
   add_row(x = 7, y = -.0070145, ymin = -.0116721, ymax = -.0023569, gender = '男性')%>%
   add_row(x = 8, y = -.0056933, ymin = -.0103182, ymax = -.0010684, gender = '男性')%>%
   add_row(x = 9, y = -.0047233, ymin = -.0093261, ymax = -.0001205, gender = '男性')%>%
   add_row(x = 10, y = -.003273, ymin = -.0078732, ymax = .0013272, gender = '男性')%>%
   add_row(x = 11, y = .0006229, ymin = -.0040254, ymax = .0052711, gender = '男性')%>%
   add_row(x = 12, y = .0037801, ymin = -.0010039, ymax = .0085641, gender = '男性')%>%
   add_row(x = 13, y = .0032255, ymin = -.0017867, ymax = .0082376, gender = '男性')%>%
   add_row(x = 14, y = .0029918, ymin = -.0026945, ymax = .008678,  gender = '男性')

female <-   data.frame(x = 0, y = -.0557045, ymin = -.0594948, ymax = -.0519143, gender = '女性')%>%
   add_row(x = 1, y = -.0237874, ymin = -.0275535, ymax = -.0200212, gender = '女性')%>%
   add_row(x = 2, y = -.0186763, ymin = -.0224115, ymax = -.0149412, gender = '女性')%>%
   add_row(x = 3, y = -.0124177, ymin = -.0161171, ymax = -.0087183, gender = '女性')%>%
   add_row(x = 4, y = -.0069645, ymin = -.0106195, ymax = -.0033096, gender = '女性')%>%
   add_row(x = 5, y = -.0025661, ymin = -.0061792, ymax = .0010469, gender = '女性')%>%
   add_row(x = 6, y = .0009979, ymin = -.0025787, ymax = .0045745, gender = '女性')%>%
   add_row(x = 7, y = .0044623, ymin = .000915, ymax = .0080096, gender = '女性')%>%
   add_row(x = 8, y = .0070437, ymin = .0035184, ymax = .010569, gender = '女性')%>%
   add_row(x = 9, y = .0096577, ymin = .0061462, ymax = .0131692, gender = '女性')%>%
   add_row(x = 10, y = .0120116, ymin = .0085022, ymax = .0155209, gender = '女性')%>%
   add_row(x = 11, y = .0158938, ymin = .0123547,  ymax = .0194329, gender = '女性')%>%
   add_row(x = 12, y = .0227411, ymin = .0191273, ymax = .0263549, gender = '女性')%>%
   add_row(x = 13, y = .0253041, ymin = .0215681, ymax = .0290401, gender = '女性')%>%
   add_row(x = 14, y = .0261749, ymin = .0220729, ymax = .0302769, gender = '女性')
df<- male %>% rbind(female)
  • Related