R ggplot-与2和3 y轴对齐的图的x轴长度相同
我希望将3-y轴图与2-y轴图对齐,以便x轴的长度相同 但是,由于第一个图的第三个轴,不同图的x轴长度不同R ggplot-与2和3 y轴对齐的图的x轴长度相同,r,ggplot2,r-grid,R,Ggplot2,R Grid,我希望将3-y轴图与2-y轴图对齐,以便x轴的长度相同 但是,由于第一个图的第三个轴,不同图的x轴长度不同 library(ggplot2) library(grid) library(patchwork) 我的数据: data_1 <- data.frame(x = c(1,2,3,4), y1 = c(5,8,1,6), y2 = c(90,40,70,50), y3 = c(600,800,9
library(ggplot2)
library(grid)
library(patchwork)
我的数据:
data_1 <- data.frame(x = c(1,2,3,4),
y1 = c(5,8,1,6),
y2 = c(90,40,70,50),
y3 = c(600,800,900,1000))
data_2 <- data.frame(x = c(1,2,3,4),
y1 = c(9,4,2,1),
y2 = c(45,70,20,10))
plot_1a <- ggplot(data = data_1, aes(x = x)) +
geom_line(aes(y = y1, color = "y1")) +
scale_y_continuous(sec.axis = sec_axis(trans = ~ ./ 0.09, name = "y2")) +
geom_line(aes(y = y2 * 0.09, color = "y2")) +
geom_line(aes(y = y3 * 0.008, color = "y3")) +
scale_color_manual(values = c("y1" = "red", "y2" = "green", "y3" = "blue")) +
guides(colour = FALSE) +
theme(axis.text.y.left = element_text(color = "red"),
axis.ticks.y.left = element_line(color = "red"),
axis.line.y.left = element_line(color = "red"),
axis.title.y.left = element_text(color = "red"),
axis.text.y.right = element_text(color = "green"),
axis.ticks.y.right = element_line(color = "green"),
axis.line.y.right = element_line(color = "green"),
axis.title.y.right = element_text(color = "green"))
plot_1b <- ggplot(data = data_1, aes(x = x)) +
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / 0.008, name = "y3")) +
labs(x = "", y = "") +
theme(axis.text.y.right = element_text(color = "blue"),
axis.ticks.y.right = element_line(color = "blue"),
axis.line.y.right = element_line(color = "blue"),
axis.title.y.right = element_text(color = "blue") ,
axis.text.y.left = element_blank(),
axis.line.y.left = element_blank(),
axis.ticks.y.left = element_blank(),
axis.line.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
panel.background = element_blank(),
plot.margin = margin(1, 0, 1, 1, "mm"))
plot_1 <- plot_1a + plot_1b + plot_layout(ncol = 2, widths = c(1,1e-3))
plot_2 <- ggplot(data = data_2, aes(x = x)) +
geom_line(aes(y = y1, color = "y1")) +
scale_y_continuous(sec.axis = sec_axis(trans = ~ ./ 0.09, name = "y2")) +
geom_line(aes(y = y2 * 0.09, color = "y2")) +
scale_color_manual(values = c("y1" = "red", "y2" = "green")) +
guides(colour = FALSE) +
theme(axis.text.y.left = element_text(color = "red"),
axis.ticks.y.left = element_line(color = "red"),
axis.line.y.left = element_line(color = "red"),
axis.title.y.left = element_text(color = "red"),
axis.text.y.right = element_text(color = "green"),
axis.ticks.y.right = element_line(color = "green"),
axis.line.y.right = element_line(color = "green"),
axis.title.y.right = element_text(color = "green"))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))
print(plot_1, vp = vplayout(1, 1))
print(plot_2, vp = vplayout(2, 1))
<代码> DATAY1
您可能想考虑向“代码>补丁”<代码>的维护者提交一个特征请求,以请求在NXN网格中进行整理。因为这是最简单的解决方案,所以我一直在玩弄它,但这似乎是不可能的
下面是一个使用网格的解决方案。从gridExtra
排列,从ggplot2
排列ggplotGrob
:
library(gridExtra)
p <- rbind(ggplotGrob(plot_1a), ggplotGrob(plot_2), size="last")
q <- ggplotGrob(plot_1b)
q$heights <- p$heights
grid.arrange(p,q,widths=c(8,1))
库(gridExtra)
P