0

I am asking this question which sounds exactly like this post because it did not solve the problem. Here is my question. I have 2 plots that I want to put together with their scale perfectly in line. Here is what I got to : enter image description here

As you can see the x axis is not exactly in line. Here is the code I used

bp_horiz<-ggplot(df,aes(y=Corg_rate))+
  geom_boxplot()+
  theme_classic()+
  labs(y="Corg annual rate")+
  coord_flip()

histo<-ggplot(data=df, aes(Corg_rate))+
  geom_histogram(fill="grey",col="black")+
  labs(x="Corg annual rate",y="Counts")+
  theme_classic()+
  labs(x=NULL,y="Counts")

plot_grid(histo,boxplot_horiz,ncol=1,align="v",rel_heights=c(4,1),axis = 'lr')

I tried to change the align parameters from v to h to hv without success. I tried using egg with the same problem on non alignment. I tried using ggMarginal but it does not work when the center plot is a histogram.

How would you solve this issue?

I do't know how to add data but to make things clearer here is the beginning of it:

   Corg_rate
 1  -0.0147 
 2   0.0106 
 3   0.114  
 4  -0.00230
 5   0.0105 
 6  -0.0574
 7   0.0102 
 8  -0.00472
 9   0.0335 
10  -0.00803
Xav64
  • 139
  • 1
  • 8

1 Answers1

0

I finally found a way: writing the scale for each plot. I have therefore added to the code

  • for the boxplot (before coord_flip)
+scale_y_continuous(limits=c(min(df$Corg_rate),max(df$Corg_rate)))

  • and for the histogram
+scale_x_continuous(limits=c(min(df$Corg_rate),max(df$Corg_rate)))

The function plot_grid then works just fine and the two plots have their scales aligned

Xav64
  • 139
  • 1
  • 8