R——图形初阶

R——图形初阶

图形初阶

1
2
3
4
5
6
7
8
## 使用图形
attach(mtcars)
png("txcj1.png")
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
dev.off()
detach(mtcars)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 输出为pdf
attach(mtcars)
pdf("txcj1.pdf")
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
dev.off()
detach(mtcars)
# 除此之外,还可以使用win.metafile()/jpeg()/bmp()/tiff()/xfig()/postscript()

# 打开图形窗口
dev.new()
plot(wt, mpg)
dev.off

一个简单的例子

1
2
3
4
5
6
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18,25, 31, 40)

plot(dose, drugA, type = "b")
# type = "b":同时绘制点和线

图形参数

1
2
3
4
5
6
7
# 使用虚线(lty = 2)和实心三角形(pch = 17)作为点的符号
opar <- par(no.readonly = T) ##复制当前图形参数设置
par(lty = 2, pch = 17) ##设置虚线和实心三角
plot(dose, drugA, type = "b")
par(opar)
# 或者
plot(dose, drugA, type = "b", lty = 2, pch = 17)

符号和线条

1
2
3
4
5
6
7
# pch: 点型
# cex:指定符号的大小,其值为默认值的倍数
# lty:线型
# lwd:线宽
png("txcj2.png")
plot(dose, drugA, type = "b", lty = 3, lwd = 3, pch = 12, cex = 2)
dev.off()

颜色

1
2
3
4
5
6
7
# col:默认的绘图颜色,可以为一个向量
# col.axis:坐标轴刻度文字的颜色
# col.lab:坐标轴标签/名称的颜色
# col.main:标题的颜色
# col.sub:副标题的颜色
# fg:图形的前景色
# bg:图形的背景色

rainbow():创建彩虹色

1
2
3
4
5
n <- 10
mycolors <- rainbow(n)
png("txcj3.png")
pie(rep(1, 10), labels = mycolors, col = mycolors)
dev.off()

gray():创建多阶灰度

1
2
3
4
5
n <- 10
mycolors <- gray(0:n/n)
png("txcj4.png")
pie(rep(1, 10), labels = mycolors, col = mycolors)
dev.off()

文本属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# cex:默认字体大小的倍数
# cex.axis:坐标轴刻度文字大小
# cex.lab:坐标轴标签大小
# cex.main:标题大小
# cex.sub:副标题dax
# font:绘图中使用的字体样式,1常规2粗体3斜体4粗斜体5符号字体
# font.axis
# font.lab
# font.main
# font.sub
# ps:字体磅值,文本的最终大小为ps*cex
# family
par(font.lab = 3, cex.lab = 1.5, font.main = 4, cex.main = 2)
# 创建字体映射(Mac)
quartzFonts(
sans = quartzFont(rep("AppleGothic", 4)),
serif = quartzFont(rep("AppleMyungjp", 4))
)
# 然后就可以使用sans/serif作为family的取值。

图形尺寸与边界尺寸

1
2
3
4
5
6
7
8
9
10
11
# pin:以英寸表示的图形尺寸(宽和高)
# mai:以数值向量表示的边界大小(下左上右,英寸)
# mar:以数值向量表示的边界大小(下左上右,英分)
opar <- par(no.readonly = T)
png("txcj5.png")
par(pin = c(2,3))
par(lwd = 2, cex = 1.5)
par(cex.axis = .75, font.axis = 3)
plot(dose, drugA, type = "b", pch = 19, lty = 2, col = "red")
par(opar)
dev.off()

1
2
3
4
png("txcj6.png")
plot(dose, drugB, type = "b", pch = 23, lty = 6, col = "blue", bg = "green")
par(opar)
dev.off()

添加文本,自定义坐标轴和图例

1
2
3
4
5
6
7
8
9
png("txcj7.png")
plot(dose, drugA, type = "b",
pch = 2, lwd = 2,
main = "Clinical Trial for drug A",
sub = "This is hypothetical data",
xlab = "Dosage", ylab = "Drug Response",
xlim = c(0, 60), ylim = c(0, 70)
)
dev.off()

标题

1
2
3
4
5
6
7
title(
main = "",
sub = "",
xlab = "",
ylab = "",
col.lab = "green", cex.lab = 0.75
)

坐标轴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
axis(side, at = , labels = , pos = ,....)
# side:坐标轴的位置1下2左3上4右
# at:一个数值向量,表示需要绘制刻度线的位置
# labels:一个字符向量,表示置于刻度线旁边的文字标签
# pos:与另一坐标轴相交位置的值
# lty:线条类型
# col: 线条和刻度线颜色
# las:标签是否平行(=0)或垂直(=2)与坐标轴
# tck:刻度线的长度,正值表示在图形内侧,0表示禁用,1表示绘制网格线,默认值为-0.01.
png("txcj8.png")
x <- c(0:10)
y <- x
z <- 10/x

opar <- par(no.readonly = T)
# 增加边界大小
par(mar = c(5,4,4,8) + 0.1)

plot(x, y, type = "b",
pch = 21,
col = "red",
yaxt = "n",
lty = 3,
ann = F
)
# ann = F:移除坐标轴
lines(x, z, type = "b", pch = 22, col = "blue", lty = 2)
axis(2, at = x, labels = x, col.axis = "red", las = 2)
axis(4, at = z, labels = round(z, digits = 2), col.axis = "blue", las = 2, cex.axis = 0.7, tck = -0.01)
mtext("y = 1/x", side = 4, line = 3, cex.lab = 1, las = 2, col = "blue")
title("An Example of Creative Axes", xlab = "X values", ylab = "Y = X")
dev.off()

次要刻度线

1
2
3
4
# install.packages("Hmisc")
library(Hmisc)
minor.tick(nx = n1, ny = n2, tick.ratio = n3)
# n1/n2表示在主刻度线之间划分区间的个数,n3表示相对于主刻度线的大小。

参考线

1
2
3
abline(h = yvalues, v = xvalues)
abline(v = seq(1,10,2), lty = 2, col = "blue")
# 上面的代码表示在x的1,3,5,7,9处添加竖直的蓝色虚线

图例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
legend(location, title, legend...)
# location:bottom/bottomleft/left/topleft/top/topright/right/bottomright/center
# title:图例标题
# legend:图例标签组成的字符串向量
png("txcj9.png")
par(lwd = 2, cex = 1.5, font.lab = 2)
plot(dose, drugA, type = "b",
pch = 15, lty = 1, col = "red",
ylim = c(0, 60),
main = "Drug A vs. Drug B",
xlab = "Drug Dosage", ylab = "Drug Response"
)
lines(dose, drugB, type = "b",
pch = 17, lty = 2, col = "blue")
abline(h = c(30), lwd = 1.5, lty = 2, col = "gray")
library(Hmisc)
minor.tick(nx = 3, ny = 3, tick.ratio = 0.5)
legend("topleft", inset = 0.05,
title = "Drug Type",
c("A", "B"), lty = c(1,2),
col = c("red", "blue"))
par(opar)
dev.off()

文本标注

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
text(location, "text to place", pos, ...)
mtext("Text to place", side, line = n, ...)
# location:文本位置参数,可以为一对坐标,也可以通过指定location为locator(1)使用鼠标交互式的确定摆放位置。
# pos:文本相对于位置参数的位置,1下2左3上4右。
# side:指定用来放置文本的边。1下2左3上4右。也可以指定参数line来内移或者外移文本,随着值的增加,文本将外移。也可以使用adj=0将文本左下对齐,或使用adj = 1右上对齐。

# 添加散点标签
attach(mtcars)
png("txcj10.png")
plot(wt, mpg,
main = "Mileage vs. Car Weight",
xlab = "Weight", ylab = "Mileage",
pch = 18, col = "blue")
text(wt, mpg, row.names(mtcars),
cex = 0.6, pos = 4, col = "red")
dev.off()
detach(mtcars)

1
2
3
4
5
6
7
8
9
# 展示不同字体族
png("txcj11.png")
opar <- par(no.readonly = T)
par(cex = 1.5)
plot(1:7, 1:7, type = "n")
text(3, 3, "Example of default text")
text(4, 4, family = "mono", "Example of mono-spaced text")
text(5, 5, family = "serif", "Example of serif text")
dev.off()

图形的组合

1
2
3
4
5
6
7
8
9
10
11
png("txcj12.png")
attach(mtcars)
opar <- par(no.readonly = T)
par(mfrow = c(2,2))
plot(wt, mpg, main = "Scatterplot of wt vs. mpg")
plot(wt, disp, main = "Scatterplot of wt vs. disp")
hist(wt, main = "Histogram of wt")
boxplot(wt, main = "Boxplot of wt")
par(opar)
detach(mtcars)
dev.off()

1
2
3
4
5
6
7
8
9
10
png("txcj13.png")
attach(mtcars)
opar <- par(no.readonly = T)
par(mfrow = c(3,1))
hist(wt)
hist(mpg)
hist(disp)
par(opar)
detach(mtcars)
dev.off()

1
2
3
4
5
6
7
8
png("txcj14.png")
attach(mtcars)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)
dev.off()

图形布局的精细控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
png("txcj15.png")
opar <- par(no.readonly = T)
par(fig = c(0, 0.8, 0, 0.8))
plot(mtcars$wt, mtcars$mpg,
xlab = "Miles Per Gallon",
ylab = "Car Weight")
par(fig = c(0, 0.8, 0.55, 1), new = T)
boxplot(mtcars$wt, horizontal = T, axes = F)
par(fig = c(0.65, 1, 0, 0.8), new = T)
boxplot(mtcars$wt, horizontal = F, axes = F)

mtext("Enhanced Scatterplot", side = 3, outer = TRUE, line = -3)
par(opar)
dev.off()

# 参数 fig = 的取值为由四个值组成的两个区间,前两个数为横向区间,后两个书为纵向区间。

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×