如何将轴标签添加到 simple.scatterplot (UsingR)
How to add axis labels to a simple.scatterplot (UsingR)
包 UsingR
有一些很棒的开箱即用绘图工具。但是,使用 simple.scatterplot
函数我不知道如何添加轴标签。
library(UsingR)
simple.scatterplot(iris$Sepal.Length, iris$Sepal.Width, xlab='hello axis')
Error in plot.default(x, y, xlim = xrange, ylim = yrange, xlab = "x", :
formal argument "xlab" matched by multiple actual arguments
图表当然可以在不使用 xlab arg 的情况下生成,我尝试使用 mtext
命令,但标签最终出现在页面中间。
mtext(side=1, text='hello axis')
我也尝试过编辑函数本身,但都没有成功:
mysimple.scatterplot <- function (x, y)
{
def.par <- par(no.readonly = TRUE)
n <- length(x)
xhist <- hist(x, sqrt(n), plot = FALSE)
yhist <- hist(y, sqrt(n), plot = FALSE)
top <- max(c(xhist$counts, yhist$counts))
xrange <- c(min(x), max(x))
yrange <- c(min(y), max(y))
nf <- layout(matrix(c(2, 0, 1, 3), 2, 2, TRUE), c(3, 1),
c(1, 3), TRUE)
layout.show(nf)
par(mar = c(3, 3, 1, 1))
plot(x, y, xlim = xrange, ylim = yrange, xlab = 'Hello X-axis', ylab = 'Hello Y-axis',
...)
abline(lm(y ~ x))
par(mar = c(0, 3, 1, 1))
barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0,
col = gray(0.95))
par(mar = c(3, 0, 1, 1))
barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0,
col = gray(0.95), horiz = TRUE)
par(def.par)
}
原因在这里:
layout.show(nf)
par(mar = c(3, 3, 1, 1)) # <-- Here
plot(x, y, xlim = xrange, ylim = yrange, xlab = 'Hello X-axis', ylab = 'Hello Y-axis',
...)
边距更改为较小的值,因此标签不在绘图中,它们在区域之外。
包 UsingR
有一些很棒的开箱即用绘图工具。但是,使用 simple.scatterplot
函数我不知道如何添加轴标签。
library(UsingR)
simple.scatterplot(iris$Sepal.Length, iris$Sepal.Width, xlab='hello axis')
Error in plot.default(x, y, xlim = xrange, ylim = yrange, xlab = "x", :
formal argument "xlab" matched by multiple actual arguments
图表当然可以在不使用 xlab arg 的情况下生成,我尝试使用 mtext
命令,但标签最终出现在页面中间。
mtext(side=1, text='hello axis')
我也尝试过编辑函数本身,但都没有成功:
mysimple.scatterplot <- function (x, y)
{
def.par <- par(no.readonly = TRUE)
n <- length(x)
xhist <- hist(x, sqrt(n), plot = FALSE)
yhist <- hist(y, sqrt(n), plot = FALSE)
top <- max(c(xhist$counts, yhist$counts))
xrange <- c(min(x), max(x))
yrange <- c(min(y), max(y))
nf <- layout(matrix(c(2, 0, 1, 3), 2, 2, TRUE), c(3, 1),
c(1, 3), TRUE)
layout.show(nf)
par(mar = c(3, 3, 1, 1))
plot(x, y, xlim = xrange, ylim = yrange, xlab = 'Hello X-axis', ylab = 'Hello Y-axis',
...)
abline(lm(y ~ x))
par(mar = c(0, 3, 1, 1))
barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0,
col = gray(0.95))
par(mar = c(3, 0, 1, 1))
barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0,
col = gray(0.95), horiz = TRUE)
par(def.par)
}
原因在这里:
layout.show(nf)
par(mar = c(3, 3, 1, 1)) # <-- Here
plot(x, y, xlim = xrange, ylim = yrange, xlab = 'Hello X-axis', ylab = 'Hello Y-axis',
...)
边距更改为较小的值,因此标签不在绘图中,它们在区域之外。