1、代码
x <- seq(-pi/2, pi/2, length=50)
x1 <- seq(-1, 1, length=50)
opar <- par(no.readonly=T)
par(fig=c(0, 0.5, 0, 1)) # 第一子图
# 绘制正弦曲线
plot(x, sin(x), type="o", pch=4, col=2, ylim=c(-2, 2), xaxt="n", ann=F)
# 添加反正弦曲线图
lines(x1, asin(x1), type='b', pch=8, col=3)
# 添加参考线
tk <- c(-pi/2, -1, 0, 1, pi/2)
abline(h=tk, lty=2, col=8)
abline(v=tk, lty=2, col=8)
# 设置图形标题和坐标轴标题
title("sin & arcsin", xlab="x", ylab="y",)
# 设置坐标刻度
z <- c(-pi/2, -1, 0, 1, pi/2)
labels <- c("-π/2", "-1", "0", "1", "π/2")
axis(1, at=z, labels=labels)
axis(2, at=z, labels=labels)
# 添加图例
legend("topleft", inset=0.02, c("sin", "arcsin"), col=c(2, 3), lty=c(1, 6), pch=c(4, 8),
bg="gray95", cex=0.7, pt.cex=0.3, seg.len=0.1, text.width=0.1, x.intersp=0.1)
par(fig=c(0.6, 1, 0, 1), new=T) # 第二子图
# 绘制余弦函数图像
plot(x+pi/2, cos(x+pi/2), type="o", pch=4, col=7, ylim=c(-1.5, 3.5),xlim=c(-1.5, 3.5), xaxt="n", yaxt="n", ann=F)
# 添加反余弦曲线图
lines(x1, acos(x1), type='b', pch=10, col=4)
# 添加参考线
tk <- c(-1, 0, 1, pi/2, pi)
abline(h=tk, lty=2, col=8)
abline(v=tk, lty=2, col=8)
# 设置图形标题和坐标轴标题
title("cos & arccos", xlab="x", ylab="y",)
# 设置坐标刻度及标签
z <- c(-1, 0, 1, pi/2, pi)
labels <- c("-1", "0", "1", "π/2", "π")
axis(1, at=z, labels=labels)
axis(2, at=z, labels=labels)
# 添加图例
legend("topright", inset=0.02, c("cos", "arccos"), col=c(2, 3),
lty=c(4, 7), pch=c(4, 10), bg="gray95", cex=0.6, pt.cex=0.3,
seg.len=0.1, text.width=0.1, x.intersp=0.1)
par(opar)
按语:
R 基础绘图系统有点复杂。