用R语言ggplot2拼接多个分组曲线图时某条折线出现偏移
两个分组曲线图分开绘制都正常,但是两个图合并到一起,用cowplot做成一页多图形式就会出现其中一条折线的位置发生向上偏移
分开绘制结果如下:
一页多图结果如下
代码如下:
library(ggplot2)
library(cowplot)
x = c(1,2,3,4)
b1 = c(0.0457,0.038,0.0304,0.0139)
b2 = c(0.0704,0.0642,0.0574,0.044)
b3 = c(0.1284,0.1244,0.1152,0.1036)
b4 = c(0.1255,0.1186,0.1034,0.0941)
b5 = c(0.1346,0.1247,0.118,0.1059)
m = c(0.1373,0.1351,0.137,0.1399)
m1 = c(0.1324,0.1286,0.1324,0.1282)
m2 = c(0.055,0.0537,0.0596,0.0609)
m3 = c(0.1134,0.1118,0.1167,0.1181)
p1 <- ggplot()+
theme_bw()+
geom_line(aes(x, m, color="1"))+
geom_point(aes(x, m, color="1"),size=1)+
geom_line(aes(x, b1, color="2"))+
geom_point(aes(x, b1, color="2"),size=1)+
geom_line(aes(x, b2, color="3"))+
geom_point(aes(x, b2, color="3"),size=1)+
geom_line(aes(x, b3, color="4"))+
geom_point(aes(x, b3, color="4"),size=1)+
geom_line(aes(x, b4, color="5"))+
geom_point(aes(x, b4, color="5"),size=1)+
geom_line(aes(x, b5, color="6"))+
geom_point(aes(x, b5, color="6"),size=1)+
geom_line(aes(x, m1, color="7"))+
geom_point(aes(x, m1, color="7"),size=1)+
geom_line(aes(x, m2, color="8"))+
geom_point(aes(x, m2, color="8"),size=1)+
geom_line(aes(x, m3, color="9"))+
geom_point(aes(x, m3, color="9"),size=1)+
xlab("(1)")+
ylab("y")+
scale_y_continuous(limits = c(0,0.16),breaks=seq(0,0.16,0.02))+
labs(colour = "group")+
theme(text=element_text("serif"))+
theme(text = element_text(size=7),
legend.key.height = unit(0.2,"cm"))
#print(p1)
m2 <- c(0.1468,0.1423,0.1508,0.1501)
b12 <- c(0.0497,0.0436,0.0342,0.0165)
b22 <- c(0.0727,0.0636,0.0547,0.0462)
b32 <- c(0.1362,0.1284,0.1217,0.1049)
b42 <- c(0.1307,0.1224,0.1106,0.0907)
b52 <- c(0.1486,0.1392,0.1334,0.1196)
m12 <- c(0.1314,0.1308,0.1376,0.1298)
m22 <- c(0.0569,0.0544,0.0614,0.0601)
m32 <- c(0.1195,0.117,0.1237,0.1258)
p2 <- ggplot()+
theme_bw()+
geom_line(aes(x, m2, color="1"))+
geom_point(aes(x, m2, color="1"),size=1)+
geom_line(aes(x, b12, color="2"))+
geom_point(aes(x, b12, color="2"),size=1)+
geom_line(aes(x, b22, color="3"))+
geom_point(aes(x, b22, color="3"),size=1)+
geom_line(aes(x, b32, color="4"))+
geom_point(aes(x, b32, color="4"),size=1)+
geom_line(aes(x, b42, color="5"))+
geom_point(aes(x, b42, color="5"),size=1)+
geom_line(aes(x, b52, color="6"))+
geom_point(aes(x, b52, color="6"),size=1)+
geom_line(aes(x, m12, color="7"))+
geom_point(aes(x, m12, color="7"),size=1)+
geom_line(aes(x, m22, color="8"))+
geom_point(aes(x, m22, color="8"),size=1)+
geom_line(aes(x, m32, color="9"))+
geom_point(aes(x, m32, color="9"),size=1)+
xlab("(2)")+
ylab("y")+
scale_y_continuous(limits = c(0,0.16),breaks=seq(0,0.16,0.02))+
labs(colour = "group")+
theme(text=element_text("serif"))+
theme(text = element_text(size=7),
legend.key.height = unit(0.2,"cm"))
#print(p2)
p <- cowplot::plot_grid(p1, p2, nrow = 1)
print(p)
其中分开绘制的图(1)(2)都是正常的,但是利用cowplot一页多图将两张图拼接到一起,就会出现图(1)的紫色折线发生偏移,请问各位这是什么原因导致的,怎样解决?