2

我总共有 7 个地块。其中六个是折线图,它们将被对齐并排列在彼此下方,这样它们之间就没有空间 - 以制作一个复合图。

这是数据和 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()[在ggpubr]和(2)plot_grid()[在cowplot]中安排这种方式,但我做不到。

有人可以帮忙吗?谢谢!

我希望布局看起来像这样

布局

4

2 回答 2

1

我真的希望,我已经正确地理解了你。老实说,您的代码一团糟,所以我使用的是默认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)

在此处输入图像描述

于 2020-04-02T17:19:14.523 回答
1

找到解决方案!使用包“拼凑而成”并结合地块边距的一些变化获得了预期的结果。

这是代码和结果

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


结果

于 2020-04-04T16:25:21.477 回答