地块安排困难

Difficulty in arranging plots

我总共有7个地块。 其中六个是折线图,它们要对齐并排列在彼此下方,使得它们之间没有 space - 以制作一个复合图。

这是数据和 ggplot2 代码,我使用同一个折线图 6 次只是为了解释我的问题

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 

第七张是带回归线的散点图

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

我想并排排列 (1) 复合图(左侧)和 (2) 散点图(右侧)。所以我尝试使用 (1) ggarrange() [in ggpubr] 和 (2) plot_grid()[in cowplot] 来安排这种方式,但我做不到。

有人能帮忙吗?谢谢!

我希望布局看起来像这样

我真的希望我没有正确理解您的意思。老实说,你的代码一团糟,所以我使用的是默认的 iris 数据集。

提示是使用plot_grid两次:

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)

已找到解决方案!使用包 'patchwork' 结合绘图边距的一些变化得到了预期的结果。

这是代码和结果

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