第3章 图形初阶

注:R语言的再复习之路

图形参数

opar <- par(no.readonly = TRUE)  #复制当前的图形参数设置
par(lty = 2, pch = 17)           #修改参数设置
plot(dose, drugA, type = 'b')    #画图
par(opar)                        #还原原始的图形参数设置

1. 符号和线条

参数描述
pch绘制点时使用的符号
cex符号的大小,默认为1
lty线条类型
lwd线条宽度,默认为1
plot(dose, drugA, type = 'b', lty = 3, lwd = 3, pch = 15, cex = 2)

2. 颜色

参数描述
col绘图颜色
col.axis坐标轴刻度文字的颜色
col.lab坐标轴标签名称的颜色
col.main主标题的颜色
col.sub副标题的颜色
fg图形的前景色
bg图形的背景色

如果需要生成多种颜色,可以使用rainbow(n)或者brewer.pal(n, name)

3. 文本属性

参数描述
cex符号的大小,默认为1
cex.axis坐标轴刻度文字的大小,默认为1
cex.lab坐标轴标签名称的大小,默认为1
cex.mian主标题的大小,默认为1
cex.sub副标题的大小。默认为1
参数描述
font绘图的字体样式。1=常规, 2=粗体, 3=斜体, 4=粗斜体, 5=符号字体
font.axis坐标轴刻度文字的字体样式
font.lab坐标轴标签名称的字体样式
font.main主标题的字体样式
font.sub副标题的字体样式
par(font.lab = 3, cex.lab = 1.5, font.main = 4, cex.main = 2)

4. 图形尺寸与边界尺寸

参数描述
pin以英寸表示的图形尺寸
mai以数值向量表示的边界大小,顺序为“下,左,上,右”,单位为英寸
# 例子,使用图形参数控制图形外观
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly = TRUE)
par(pin = c(2, 3))
par(lwd = 2, cex = 1.5)
par(cex.axis = 0.75, font.axis = 3)
plot(dose, drugA, type = 'b', pch = 19, lty = 2, col = 'red')
plot(dose, drugB, type = 'b', pch = 23, lty = 6, col = 'blue', bg = 'green')
par(opar)

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

1. 标题

#格式
title(main = 'main title', sub = 'sub title', xlab = 'x-axis label', ylab = 'y-axis label')

#例子
title(main = 'My title', col.main = 'red', sub = 'My Subtitle', col.sub = 'blue', xlab = 'My X label', ylab = 'My Y label', col.lab = 'green', cex.lab = 0.75)
  • 函数title()中也可以使用颜色,文本属性(大小、字体样式)等图形参数
  • 一般会在函数plot()里面添加参数ann = FALSE,然后在plot()函数外面使用title()来设置主标题、副标题、X坐标轴标签、Y坐标轴标签。

2. 坐标轴

参数描述
side一个整数,表示在图形的哪边绘制坐标轴(1=下, 2=左, 3=上, 4=右)
at一个数值型向量,表示需要绘制刻度线的位置
labels一个字符向量,表示置于刻度线旁边的文字标签(如果为NULL,则直接使用at中的值)
pos坐标轴线绘制位置的坐标
lty线条类型
col线条和刻度线颜色
las标签是否平行于(=0)或垂直于(=2)坐标轴
tck刻度线长度(负值表示在图形外侧,正值表示在图形内侧,默认值为-0.01)
#格式
axis(side, at = , labels = , pos = , lty = , col = , las = , tck = , ...)

#例子
x <- c(1:10)
y <- x
z <- 10 / x
opar <- par(no.readonly = TRUE)
par(mar = c(5, 4, 4, 8) + 0.1)
plot(x, y, type = 'b', pch = 21, col = 'red', yaxt = 'n', lty = 3, ann = FALSE)
lines(x, z, type = 'b', pch = 22, col = 'blue', lty = 2)
axis(side = 2, at = x, labels = x, col.axis = 'red', las = 2)
axis(side = 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')
par(opar)

3. 参考线

#格式
abline(h = yvalues, v = xvalues)

#例子
abline(v = seq(1, 10, 2), lty = 2, col = 'blue')

注:同样的参考线的函数abline()也可以添加符号、线条、颜色、文本属性(大小、样式)等图形参数

4. 图例

参数描述
location(1)执行locator(1),然后鼠标单击图形确定图例位置(2)给定图例左上角的x,y坐标;(3)使用关键字bottom, bottomleft, left, topleft, top, topright, right, bottomright, center,然后再用参数inset = 来指定图例向图形内侧移动的大小
title图例标题的字符串
legend图例标签组成的字符型向量
horiz逻辑型参数,horiz = TRUE将水平放置图例
#格式
legend(location, title, legend, ...)

#例子
legend('topleft', inset = 0.05, title = 'Drug Type', c('A', 'B'), lty = c(1, 2), pch = c(15, 17), col = c('red', 'blue'))

5. 文本标注

  • text()向绘图区域内部添加文本
  • mtext()向图形的四个边界之一添加文本
参数描述
location文本的位置参数,(x, y)坐标
pos文本相对于位置参数的方位。1=下,2=左,3=上,4=右。若指定了pos,就可以再指定offset=作为偏移量
side指定用来放置文本的边。1=下, 2=左, 3=上, 4=右
attach(mtcars)
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')
detach(mtcars)

图形的组合

#方法1
par(mfrow = c(2, 2))  #按行填充
par(mfcol = c(2, 2))  #按列填充

#方法2
layout(matrix(1, 1, 2, 3), 2, 2, byrow = TRUE, widths = c(3, 1), heights = c(1, 2))

注:

  • 叠加图形利用par(new=TRUE)即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值