Difficulty in arranging plots

Viewed 294

I have total 7 plots. Six of them are line charts which are to be aligned and arranged one below each other such that there is no space between them - to make one composite plot.

Here is the data and ggplot2 code and I am using the same line chart 6 times just to explain my problem

x<- 1:10

y<- rnorm(10)

data <- data.frame(x,y)


library(ggplot2)
k<- ggplot(data, aes(x= x, y= y)) + geom_line() +  theme(panel.background=element_blank()) + theme(aspect.ratio = 0.15) + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line.x = element_line(colour = "black", size= 0.5), axis.line.y = element_line(colour = "black", size= 0.5), axis.ticks.y = element_line(colour = "black", size= 0.5), axis.ticks.x = element_blank()) + xlab(label = "")+ ylab(label = "") + scale_x_continuous(label= NULL) +theme(plot.margin = unit(c(-0.25, 2, -0.25, 2), "cm"))
k 

Seventh is scatter plot with regression line

a<- 1:10
a

b<- 11:20
b

data1 <- data.frame(a,b)
data1

library(ggplot2)
k3<-ggplot(data1, aes(x=a, y=b))+ geom_point(shape=1, fill ="black", alpha= 1, color= "black", size=3) + geom_smooth(method = lm, size = 0.5, linetype ="dotted", fill ="black", color= "black", alpha = 0.3)
k3

k3<- k3 + expand_limits(x = c(0.5, 10.5), y = c(10.5,20.5)) +  scale_x_continuous(expand = c(0, 0), breaks = c(2,4, 6, 8, 10)) + scale_y_continuous(expand = c(0, 0),breaks = c(10, 12, 14, 16, 18, 20)) 
k3

k3 <- k3 +  theme(panel.background=element_blank())+ theme(aspect.ratio = 1) + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black", size= 0.5), axis.ticks.y = element_line(colour = "black", size= 0.5), axis.ticks.x = element_line(colour = "black", size= 0.5)) 
k3

k3<- k3 + scale_x_reverse(expand = c(0, 0)) 
k3

#Flip axes 
k3<- k3 + coord_flip() 
k3<- k3 + theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
k3

I want to arrange (1) composite plot (on left) and (2) scatter plot (on right)side by side. So I tried arranging that way using (1) ggarrange() [in ggpubr] and (2) plot_grid()[in cowplot], but I couldn't.

Could anybody help? Thankyou!

I want the layout to look like this

layout

2 Answers

I really hope, that I've understand you correctly. To be honest your code is a mess, so I was using a default iris dataset.

The hint is to use plot_grid twice:

library(ggplot2)
library(cowplot)

k <- iris %>% 
  ggplot(aes(x = Sepal.Length, y = Petal.Length)) +
  geom_line() +
  labs(x = "", y = "") +
  theme_classic() +
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

k3 <- iris %>% 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(x = "", y = "") +
  theme_classic()

grid1 <- cowplot::plot_grid(
  k, k, k, k, k, k,
  ncol = 1,
  align = "hv"
)

cowplot::plot_grid(grid1, k3, 
                   align = "hv",
                   rel_widths = c(1.5, 1), # you can control the relative width and height
                   nrow = 1)

enter image description here

Found solution! Got desired result with package 'patchwork' in combination with some changes in plot margins.

Here is the code and result

library(ggplot2)

iris 

# Line charts 
k <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_line() +
  labs(x = "", y = "") +
  theme_classic() +
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()) + 
  theme(plot.margin = unit(c(-0.25,-3,-0.25,0), "cm")) + 
  theme(aspect.ratio = 0.15)

# Line chart (k4) with y-axis label
k4 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_line() +
  labs(x = "", y = "") +
  theme_classic() +
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()) +
  theme(axis.title.y = element_text(
    vjust = 2,
    color = "black",
    size = 10,
    face = "bold"
  ))+ 
  theme(plot.margin = unit(c(-0.25,-3,-0.25,0), "cm"))+ 
  theme(aspect.ratio = 0.15)

# scatter plot
sc <-ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(x = "", y = "") +
  theme_classic()+
  theme(plot.margin = unit(c(0,0,0,-0.5), "cm"))+
  theme(aspect.ratio = 0.7) 
sc

library(patchwork)

p<- (k/k/k/k4/k/k)| sc
p


Result

Related