如何使用R中的Lappy将y轴标签移离R绘图
我有以下代码(感谢@Rawr in的回答):如何使用R中的Lappy将y轴标签移离R绘图,r,plot,lapply,R,Plot,Lapply,我有以下代码(感谢@Rawr in的回答): labes1您可以单独定义ylab,就像您对xlab所做的一样,并设置line参数来定义它与绘图的距离() 我得到了一个运行示例,将您的代码和您前面问题中的@rawr结合起来 set.seed(1) z <- ts(matrix(rt(200 * 10, df = 3), 200, 10), start = c(1961, 1), frequency = 12) z <- z * 1e5 # to make "wide" y-axis
labes1您可以单独定义ylab,就像您对xlab所做的一样,并设置line
参数来定义它与绘图的距离()
我得到了一个运行示例,将您的代码和您前面问题中的@rawr结合起来
set.seed(1)
z <- ts(matrix(rt(200 * 10, df = 3), 200, 10), start = c(1961, 1), frequency = 12)
z <- z * 1e5 # to make "wide" y-axis labels
## vectors of x, y, and main labels
xl <- sprintf('x label %s', 1:10)
yl <- sprintf('y label %s', 1:10)
ml <- sprintf('main label %s', 1:10)
labes1 <- c("P(LNG)","","Volume(LNG)","","P(oil)","","Can.GDP","","US GDP","")
titles <- c("Levels","","","","","Log Difference","","","","")
par(mfrow = c(5, 2), mar = c(0.3, 6, 0, 2), oma = c(5, 0, 3, 2))
lapply(1:10, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = "Quarter", ylab = "", axes = FALSE) # set ylab to ""
axis(2, las = 1)
title(ylab = labes1[ii], line = 4) # set the line at an appropriate distance
box()
if (ii %in% 9:10) {
axis(1)
title(xlab = 'Quarter', xpd = NA)
}
if (ii %in% 1:2)
title(main = c('Levels', 'Log Difference')[ii], xpd = NA, line = 1)
})
set.seed(1)
z看起来在两个图之间的列中有更多的空间。请尝试c(“,”P(LNG)”,“,”体积(LNG)”,“,”P(油)”,“,”Can.GDP“,”US GDP”)
。另一个选项是重新缩放y轴上的变量,使其处于一个合适的范围内(比如说0-20),然后在标签中添加适当的单位。例如,将美国GDP除以1000000,则标签将为“美国GDP(x1000000)”。y轴刻度标签将是1.0、1.2、1.4、1.6、1.8、2.0
。另一个选项是使用layout
功能,该功能允许对多面板打印的打印尺寸进行更多控制。与par(mar=…)
不同,您可以以更复杂的方式改变边距。
set.seed(1)
z <- ts(matrix(rt(200 * 10, df = 3), 200, 10), start = c(1961, 1), frequency = 12)
z <- z * 1e5 # to make "wide" y-axis labels
## vectors of x, y, and main labels
xl <- sprintf('x label %s', 1:10)
yl <- sprintf('y label %s', 1:10)
ml <- sprintf('main label %s', 1:10)
labes1 <- c("P(LNG)","","Volume(LNG)","","P(oil)","","Can.GDP","","US GDP","")
titles <- c("Levels","","","","","Log Difference","","","","")
par(mfrow = c(5, 2), mar = c(0.3, 6, 0, 2), oma = c(5, 0, 3, 2))
lapply(1:10, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = "Quarter", ylab = "", axes = FALSE) # set ylab to ""
axis(2, las = 1)
title(ylab = labes1[ii], line = 4) # set the line at an appropriate distance
box()
if (ii %in% 9:10) {
axis(1)
title(xlab = 'Quarter', xpd = NA)
}
if (ii %in% 1:2)
title(main = c('Levels', 'Log Difference')[ii], xpd = NA, line = 1)
})