R语言初级绘图操作

目 录

1 认识常见的图形函数hist和plot

1.1 认识hist

1.2 认识plot

2 图形参数

2.1符号和线条

2.2颜色

2.3文本属性

2.4图形尺寸和边界尺寸

3 文本标注、自定义坐标轴和图例

3.1 标题

3.2 点标注

3.3 参考线

3.4 图例

4 图形布局与组合

往期回顾

 

正 文

1 认识常见的图形函数hist和plot

1.1 认识hist

hist(柱形图)是呈现一维数据的一种常用图形。

#hist函数表达式
hist(x, breaks = "Sturges",
     freq = NULL, probability = !freq,
     include.lowest = TRUE, right = TRUE,
     density = NULL, angle = 45, col = NULL, border = NULL,
     main = paste("Histogram of" , xname),
     xlim = range(breaks), ylim = NULL,
     xlab = xname, ylab,
     axes = TRUE, plot = TRUE, labels = FALSE,
     nclass = NULL, warn.unused = TRUE, ...)
主要参数解释:

x:定义数据向量
breaks:定义柱形图分组。可以是一个常数,定义分组个数,例如:breaks = 12;
        可以是一个有序数据集,定义分组的边界,其中两端边界即为x的最大最小值,例如:breaks = c(4*0:5, 10*3:5, 70, 100, 140
freq:定义频数/频率计算,默认freq=TRUE,频数;freq=FALSE,频率。
main:定义图标题
xlim/ylim:定义x/y横纵坐标范围
xlab/ylab:定义x/y横纵坐标名称

hist示例

set.seed(4)
x <- rchisq(100, df = 6)
hist(x)

hist(x,breaks = 20,freq = FALSE)

1.2 认识plot

plot(散点图)是最常见的展现双变量的图形。

#plot函数表达式
plot(x, y, ...) #常规形式定义数据
plot(y~x, ...) #函数形式定义数据,不常用

plot示例

> require(stats)
> head(cars,3)
  speed dist
1     4    2
2     4   10
3     7    4
> plot(cars)

> require(stats)
> head(cars,3)
  speed dist
1     4    2
2     4   10
3     7    4
> plot(cars)
> plot(cars$dist~cars$speed)

2 图形参数

2.1符号和线条:pch(点形状)、cex(点大小)、lty(线形状)、lwd(线宽度)

2.2颜色:col(线/点颜色)……

2.3文本属性:字体的缩放比例或加粗cex、font

2.4图形尺寸和边界尺寸

pin(英寸表示图形尺寸)、mai(以数值向量表示边界大小,顺序(下、左、上、右),单位:英寸)、mar(以数值向量表示边界大小,顺序(下、左、上、右),单位:英分)

> require(stats)
> plot(cars$speed,cars$dist,type='b',lty=3,lwd=3,pch=15,cex=2)

3 文本标注、自定义坐标轴和图例

3.1标题

plot(wt,mpg) #输出下左图
title(main="xxxxx") #在plot(wt,mpg)图上添加标题

3.2 点标注

attach(mtcars)
plot(wt,mpg,main = 'Mileage vs. Car Weight') #输出下图1
text(wt,mpg,row.names(mtcars),cex = 0.6,pos=4,col='red') #输出下图2
detach(mtcars)

图1

图2

attach(mtcars)
plot(wt,mpg,main = 'Mileage vs. Car Weight')
text(wt,mpg,mpg,cex = 0.6,pos=4,col='red') #输出下图3
detach(mtcars)

图3

3.3 参考线

函数abline()可以用来添加参考线,格式如下:
abline(h=yvalues,v=xvalues)

示例

attach(mtcars)
plot(wt,mpg,main = 'Mileage vs. Car Weight')
abline(h=c(min(mpg),mean(mpg),max(mpg)))
detach(mtcars)

attach(mtcars)
plot(wt,mpg,main = 'Mileage vs. Car Weight abline(lm(mpg~wt))')
abline(lm(mpg~wt))
detach(mtcars)

3.4 图例

图例格式如下

legend(location,title,legend……)
#location位置
#title图例标题
#legend图例标签组成(可以使字符串向量)
示例
#2-4行原始数据
dose <- c(20,30,40,45,60)
drugA <- c(16,20,27,40,60)
drugB <- c(15,18,25,31,40)

#7-11行作图
plot(dose,drugA,type='b',col='red',lty=2,pch=2,lwd=2,
     main = 'Clinical Trials for Drug A',
     sub = 'This is hypothetical data',
     xlab = 'Dosage',ylab = 'Drug Response',
     xlim = c(0,60),ylim = c(0,70))
     
#14行添加线条
lines(dose,drugB,type='b',pch=17,lty=2,col='blue')

#17行添加辅助线
abline(h=c(30),lwd=1.5,lty=2,col='grey')

#20行添加图例
legend('topleft',inset = .05,title = 'drug type',c('A','B'),lty = c(1,2),pch = c(15,17),col=c('red','blue'))

4 图形布局与组合

在R中使用函数par()或layout()可以容易地组合多幅图形为一幅总括图形。par()函数中使用图形参数mfrow=c(nrows, ncols)来创建按行填充的、行数为nrows、列数为ncols的图形矩阵。另外,可以使用nfcol=c(nrows, ncols)按列填充矩阵。

布局与组合示例1:

attach(mtcars)
opar <- par(no.readonly = TRUE)
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)

 

布局与组合示例2:

opar <- par(no.readonly = TRUE)
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 = TRUE)
boxplot(mtcars$wt, horizontal = TRUE, axes = FALSE)
par(fig = c(0.65, 1, 0, 0.8), new = TRUE)
boxplot(mtcars$mpg, axes = FALSE)
mtext("Enhanced Scatterplot", side = 3, outer = TRUE, 
line = -3)
par(opar)

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值