基于直方图和散点图延伸出来的其他绘图细节

图形是一个有效传递分析结果的呈现方式。R是一个非常优秀的图形构建平台,它可以在生成基本图形后,调整包括标题、坐标轴、标签、颜色、线条、符号和文本标注等在内的所有图形特征。本章将带大家领略一下R在图形构建中的强大之处,也为后续更为高阶图形构建铺垫基础。


目 录

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

1.1 认识hist

1.2 认识plot

2 图形参数

  • 符号和线条

  • 颜色

  • 文本属性

  • 图形尺寸和边界尺寸

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)


640?wx_fmt=png

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


640?wx_fmt=png

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)


640?wx_fmt=png

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


640?wx_fmt=png


2 图形参数


主要包括以下图形参数

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

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

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

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

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

640?wx_fmt=png


640?wx_fmt=png

640?wx_fmt=other

type:呈现形式

"p" for points,

"l" for lines,

"b" for both,

"c" for the lines part alone of "b",

"o" for both ‘overplotted’,

"h" for ‘histogram’ like (or ‘high-density’) vertical lines,

"s" for stair steps,

"S" for other steps, see ‘Details’ below,

"n" for no plotting.

640?wx_fmt=other

640?wx_fmt=other

640?wx_fmt=other

640?wx_fmt=jpeg



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


3.1标题

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

640?wx_fmt=png640?wx_fmt=png

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)	
	
	
attach(mtcars)	
plot(wt,mpg,main = 'Mileage vs. Car Weight')	
text(wt,mpg,mpg,cex = 0.6,pos=4,col='red') #输出下图3	
detach(mtcars)

640?wx_fmt=png

(图1)

640?wx_fmt=png

(图2)

640?wx_fmt=png

(图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)


640?wx_fmt=png


640?wx_fmt=png

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'))


640?wx_fmt=png



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)

640?wx_fmt=png


布局与组合示例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)

640?wx_fmt=png

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值