【发布时间】:2020-12-01 00:41:27
【问题描述】:
我在R 中创建了一个并排图,其中两个图都应该使用相同的 y 轴。然而,左边的图是boxplot,右边的图是matplot,在这两个图中我设置了相同的y轴范围ylim = c(0, YMAX)。不幸的是,正如您在下面看到的那样,这些图似乎没有使用相同的布局范围 --- 条形图将范围直接带到轴的边缘,而 matplot 在轴的每个边缘都有一个缓冲区。因此,图上的 y 轴没有按预期排列。
#Create layout for plot
LAYOUT <- matrix(c(rep(1, 2), 2:3), nrow = 2, ncol = 2, byrow = TRUE);
layout(LAYOUT, heights = c(0.1, 1));
#Create plot matrix
par(mar = c(0.5, 2.1, 0.5, 2.1), mgp = c(3, 1, 0), las = 0);
plot.new();
text(0.5,0.5, 'Barplot and Violin Plot', cex = 1.2, font = 2);
par(mar = c(5.1, 4.1, 2.1, 2.1), mgp = c(3, 1, 0), las = 0);
#Generate data for plot
x <- 1:100
y <- rchisq(100, df = 40);
#Generate plots
DENS <- density(y);
YMAX <- 1.4*max(y);
barplot(y, names.arg = x, ylim = c(0, YMAX));
matplot(x = cbind(-DENS$y, DENS$y), y = DENS$x,
type = c('l', 'l'), lty = c(1, 1),
col = c('black', 'black'),
xlim = c(-max(DENS$y), max(DENS$y)),
ylim = c(0, YMAX),
xlab = 'Density', ylab = '');
如何调整此图以对齐 y 轴? (理想情况下,我希望右侧的图将刻度线放在轴的边缘,就像左侧的情况一样。)
【问题讨论】: