在 ggplot 中缝合面
Stitch facets in ggplot
我有 2 个数据集,我想 ggplot
并排,以便它们共享 y-axis
。
我想为此使用 ggplot
的 facet_wrap
,但需要了解如何将它们 stitch
结合在一起。这是我目前所拥有的:
df.1 <- data.frame(x=c(-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736),
y=c(62.8805462356349,73.027603062927,88.4090942806369,87.6879626013305,55.9895740872068,93.5396099910227),
side=1,stringsAsFactors = F)
df.2 <- data.frame(x=c(1.32192809488736,3.32192809488736,1.32192809488736,1.32192809488736),
y=c(73.027603062927,7.33717302418609,87.6879626013305,93.5396099910227),
side=2,stringsAsFactors = F)
df <- rbind(df.1,df.2)
df$side <- factor(df$side,levels=c(1,2))
require(ggplot2)
ggplot(df,aes(x=x,y=y))+geom_point()+facet_wrap(~side,ncol=2,scales="free")+stat_smooth(method="lm",formula=y~x,colour="black")+theme(strip.text.y=element_text())
如何去掉右面的 y 轴并去掉面之间的 space,使它们显示为一个图形?此外,它们需要具有相同的 y 轴坐标。
明确地说,我使用两个 facets
的原因是因为我分别为每个 df
安装了一个 lm。
使用 ylim
设置 y 限制。用 panel.spacing.x
调整间隙。使用 axis.text.y
删除不需要的标签,使用 axis.ticks
.
删除刻度线
ggplot(df,aes(x=x,y=y))+geom_point()+ ylim(0, 120) +
facet_wrap(~side,ncol=2,scales="free")+
stat_smooth(method="lm",formula=y~x,colour="black")+
theme(strip.text.y=element_text(), axis.text.y = element_blank(),
axis.ticks = element_blank(), panel.spacing.x=unit(0, "lines"))
您可以调整刻面之间的边距以删除 space。另外,使用 scales="free_x"
这样只有 x 轴刻度是自由的,而 y 轴刻度是相同的。因为每个facet的y轴刻度都一样,右边的刻度不会重复:
theme_set(cowplot::theme_cowplot())
ggplot(df,aes(x=x,y=y))+geom_point() +
facet_wrap(~side, ncol=2, scales="free_x") +
stat_smooth(method="lm", formula=y~x, colour="black") +
theme(panel.spacing = unit(-1.25, "lines"))
但您也可以避免刻面,并使用颜色美学来获得单独的线条。这种方法将所有内容都放在一个面板上,因此您不必担心在各个方面之间排列 x 尺度:
ggplot(df,aes(x=x, y=y)) +
geom_point() +
stat_smooth(method="lm",formula=y~x, aes(colour=side))
我有 2 个数据集,我想 ggplot
并排,以便它们共享 y-axis
。
我想为此使用 ggplot
的 facet_wrap
,但需要了解如何将它们 stitch
结合在一起。这是我目前所拥有的:
df.1 <- data.frame(x=c(-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736,-0.678071905112638,1.32192809488736),
y=c(62.8805462356349,73.027603062927,88.4090942806369,87.6879626013305,55.9895740872068,93.5396099910227),
side=1,stringsAsFactors = F)
df.2 <- data.frame(x=c(1.32192809488736,3.32192809488736,1.32192809488736,1.32192809488736),
y=c(73.027603062927,7.33717302418609,87.6879626013305,93.5396099910227),
side=2,stringsAsFactors = F)
df <- rbind(df.1,df.2)
df$side <- factor(df$side,levels=c(1,2))
require(ggplot2)
ggplot(df,aes(x=x,y=y))+geom_point()+facet_wrap(~side,ncol=2,scales="free")+stat_smooth(method="lm",formula=y~x,colour="black")+theme(strip.text.y=element_text())
如何去掉右面的 y 轴并去掉面之间的 space,使它们显示为一个图形?此外,它们需要具有相同的 y 轴坐标。
明确地说,我使用两个 facets
的原因是因为我分别为每个 df
安装了一个 lm。
使用 ylim
设置 y 限制。用 panel.spacing.x
调整间隙。使用 axis.text.y
删除不需要的标签,使用 axis.ticks
.
ggplot(df,aes(x=x,y=y))+geom_point()+ ylim(0, 120) +
facet_wrap(~side,ncol=2,scales="free")+
stat_smooth(method="lm",formula=y~x,colour="black")+
theme(strip.text.y=element_text(), axis.text.y = element_blank(),
axis.ticks = element_blank(), panel.spacing.x=unit(0, "lines"))
您可以调整刻面之间的边距以删除 space。另外,使用 scales="free_x"
这样只有 x 轴刻度是自由的,而 y 轴刻度是相同的。因为每个facet的y轴刻度都一样,右边的刻度不会重复:
theme_set(cowplot::theme_cowplot())
ggplot(df,aes(x=x,y=y))+geom_point() +
facet_wrap(~side, ncol=2, scales="free_x") +
stat_smooth(method="lm", formula=y~x, colour="black") +
theme(panel.spacing = unit(-1.25, "lines"))
但您也可以避免刻面,并使用颜色美学来获得单独的线条。这种方法将所有内容都放在一个面板上,因此您不必担心在各个方面之间排列 x 尺度:
ggplot(df,aes(x=x, y=y)) +
geom_point() +
stat_smooth(method="lm",formula=y~x, aes(colour=side))