可视化:RStudio 图形系统 - 进阶

356 阅读5分钟

本文已参加「新人创作礼」活动,一起开启掘金创作之路。

这次带来的是 RStudio 的进阶图形系统。

各个知识点后面都有对应的小练习哦,大家可以利用刚刚学到的知识来试着写写看!

Intermedia R Graph System R图形系统进阶

除了利用R自带的命令直接绘制标准的统计图形, R也提供了非常多的选项来画出高度自定义的图形. 本节我们将讨论如何精细的控制图形.

  • 添加元素
  • 高级绘图函数和低级绘图函数
  • 添加点
  • 添加线
  • 添加线段
  • 添加矩形
  • 添加任意多边形
  • 添加箭头
  • 添加图例
  • 添加文字
  • 修改坐标轴
  • 精确定位
# 添加元素

plot(1:5, 1:5, pch=15, main="new plot")
points(3, 4)

# 高级绘图函数和低级绘图函数

# 高级(high level)绘图函数默认会创建一副新画布, 包含图形元素,外框,内框,坐标轴,标题,等
# 高级绘图函数举例: plot, barplot, boxplot, pie, histogram 等

# 低级(low level)绘图函数不会创建新画布, 而会将在已有的画布上添加新的图形元素

# 添加点
# points(x, y, col, cex, pch, ...)
points(1:5, 5:1, cex=2, pch=2:6, col="red")

# 添加线
# lines(x, y, col, lwd, lty, ...)
lines(1:5, c(4,3,5,3,4), lwd=0.8, lty=1, col="blue")

# 可以控制点和线, 那就几乎可以绘制任意图形了

# 添加线段
# segments(x0, y0, x1, y1, col, lwd, lty, ...)
segments(1:4, 1:4, 2:5, 1:4, col="darkgreen", lty=2, lwd=2)

# 添加直线
# abline(a, b, h, v, col,...)  # y = a + b*x
plot(0:5, 0:5)
abline(a=0, b=1, col="red")
abline(3,-1)
abline(h=1)
abline(v=2)
abline(v=0:5, h=0:5, col="gray", lty=2)

# 添加矩形
# rect(xleft, ybottom, xright, ytop, density = NULL, angle = 45, col = NA, border = NULL, lty, lwd, ...)
plot(1:5, 1:5, type="n")
rect(1,1,2,2)
rect(1,1,2,2, density=10)
rect(4,4,5,5, density=10, angle=80)
rect(4,4,5,5, density=10, angle=130)

rect(1:4, 4:1, 2:5, 5:2, col="gray", lty=2, lwd=4:1)


# 添加任意多边形
# polygon(x, y = NULL, density = NULL, angle = 45, border = NULL, col = NA, lwd, lty, ...)
plot(1:5, 1:5, type="n")
polygon(c(1,3,2), c(1,1,4))
polygon(c(1,3,2), c(1,1,4), border="red")
polygon(c(1,3,2), c(1,1,4), col="red")

polygon(c(2,4,5,3), c(2,2,4,4), border="orange", lwd=3)


# 添加箭头
# arrows(x0, y0, x1 = x0, y1 = y0, length = 0.25, angle = 30, code = 2, col, lty, lwd, ...)
arrows(1,3,3,3)
arrows(1,4,4,4, length=1, angle=60)

# 添加图例
# legend(x, y = NULL, legend, fill = NULL, col = par("col"), border = "black", lty, lwd, pch, angle = 45, density = NULL, bty = "o",...)
plot(1:5, 1:5, type="n")
legend(1,5, legend="cyl", cex=1.5, pch=15, col="red")
legend(2,5, legend="cyl", cex=1.5, pch=15, col="red", bty="n")
legend(3,5, legend=c("cyl:4","cyl:6"), cex=1.5, pch=15, 
       col=c("red","blue"))

# 添加文字
# text(x, y, labels, cex, offset=0.5, ...)
plot(1:5, 1:5, type="n")
text(2,2, "a", cex=2)
text(1:5, 5:1, letters[1:5])


# 修改坐标轴
plot(10^(1:5), type="b")
plot(10^(1:5), type="b", log="y")
plot(10^(1:5), type="b", log="y", las=1)
plot(10^(1:5), type="b", log="y", las=2)
plot(10^(1:5), type="b", log="y", las=3)
plot(10^(1:5), type="b", log="y", las=0)

plot(10^(1:5), type="b", log="y", las=1, axes=F)
axis(side=1) # side的1234分别代表: 下, 左, 上, 右
axis(side=2, at=c(10, 100, 1000, 1:9*10^4, 100000), las=1)
box()

# 坐标轴, 刻度, 标题 的颜色
plot(10^(1:5), type="b",
     main="main title",
     sub="sub title",
     col.axis="blue",
     col.lab="red",
     col.main="purple",
     col.sub="orange")

# 精确定位
# locator(n = 512, type = "n", ...)
locator(1)
locator(1, type="p")
locator(1, type="p", pch=19, col="red")
locator(2, type="l")
locator(3, type="l")
locator(3, type="o")

练习

# Q1
# 创建一个空白画布, 并添加 10个点, 3条曲线(不同线型)

# Q2
# 绘制一个等腰梯形

# Q3
# 绘制一个带有图例(legend)的条形图(barplot)

习题

# Q1
# 写一个函数: regular_polygon(n, x, y, col, border), 能绘制出正n边形, 且多边形中心默认在原点, 通过col和border可以设置正多边形内部和边框的颜色

# Q2
# 写一个函数: donut_plot(x, col), 能绘制出 甜甜圈图(donut chart)

# Q3
# 写一个函数: barplot_new(x, col), 能绘制出 条形图, 并且在每一个条形上注明对应的数值大小

# Q4
# 写一个函数: f_area(f, interval), 能绘制出一个连续函数下方的面积图像, 参数interval代表了自变量的取值区间

# Q5 (比较复杂, 选做)
# 写一个函数: clock(time), 能绘制出一个时钟, time 为时钟显示的时间(格式:"hh:mm")

参考图形

  • Q1

image.png

  • Q2

image.png

  • Q3

image.png

  • Q4

image.png

par

par() 参数提供了60多种对于画布的设置, 以下是一些常用的设置:

  • 当前par参数值
  • 控制背景颜色
  • 控制画布大小
  • 多幅图像
# par() 参数
par() # 查看所有par参数的当前取值
par()$cex # 查看par参数中某个特定参数的值

# 参数存档
op = par() # 保存当前全部参数值, 便于未来恢复


# 控制背景颜色
par()$bg
par(bg="gray")
plot(1)

# 控制画布大小
par()$mar
par(mar=c(1,1,1,1))

# 多幅图像
par()$mfrow
par(mfrow=c(2,3))
for(i in 1:6) barplot(i, main=i, col=i)

# 恢复par存档值
par(op)

# 多幅图像的复杂呈现
# layout(mat, widths = rep.int(1, ncol(mat)), heights = rep.int(1, nrow(mat)), respect = FALSE)
lay = layout(matrix(c(1,0,2,3),2,2,byrow = TRUE), widths = c(4,1.5), heights = c(1.5,4,1), respect = TRUE)
layout.show(lay)


par(mar=c(0,4,3,1))
hist(mtcars$wt, breaks=10, axes=F, xlab="", ylab="", col="gray", main="")

par(mar=c(4,4,1,1))
plot(mtcars$mpg ~ mtcars$wt)

par(mar=c(3,0,1,1))
barplot(hist(mtcars$mpg, breaks=10,plot=F)$counts, axes=F, xlab="", ylab="", col="gray", space=0, horiz=T)

习题

# 将前一部分的习题中的Q1~Q4的结果画在同一个画布中, 并且给每一题的图加上对应的标题序号(比如"Q1")