R图形初阶段

R图形初阶段

plot(x, y, ...)

x,y 分别是x轴和y轴的数据,如果x是一个数据对,那么y则可以忽略

…… 为要传递给par的参数,也可以提前设置par参数,这里就可以不设置,如果要单独控制,就在这里设置

type 有几种类型

“P”为点,

“L”是线,

两个“B”,

对于“b”中仅行部分的“c”,

“o”表示两个“过密”,

“h”用于“直方图”(或“高密度”)垂直线,

“s”是楼梯图形,,

“n”表示没有阴谋。

XLAB      x轴的标题:参见标题。

YLAB      y轴的标题:参见标题。

ASP        y/x纵横比

把plot的各种type展示出来

par(mfrow=c(2,4))

t_type<-c("c","p","l","b","c","n","s","S")

for (i in 1:length(t_type)){

       plot(dose, drugA, pch=3, type=t_type[i])

       title(paste("type :", t_type[i]))

}

 

之间的区别一目了然了

 

 

 

par(lty=2, pch=17)

plot(dose, drugA, type = "b")

plot(dose, drugA, type = "b", pch=3)

 

par对全局控制

 

 

pch 指定绘制点的时候使用的负号

 

cex  字体放大大小,默认为1, 大于1 放大,小于1,缩小

lty  指线条的类型

lwd 指线条的宽度

 

 

 

plot(dose, drugA, type = "b", pch=3 ,cex=2)

指定图标大小放大2倍

 

matrix( rep(rep(1:10), 10), ncol=10, nrow=10, byrow=TRUE)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    4    5    6    7    8    9    10
 [2,]    1    2    3    4    5    6    7    8    9    10
 [3,]    1    2    3    4    5    6    7    8    9    10
 [4,]    1    2    3    4    5    6    7    8    9    10
 [5,]    1    2    3    4    5    6    7    8    9    10
 [6,]    1    2    3    4    5    6    7    8    9    10
 [7,]    1    2    3    4    5    6    7    8    9    10
 [8,]    1    2    3    4    5    6    7    8    9    10
 [9,]    1    2    3    4    5    6    7    8    9    10
[10,]    1    2    3    4    5    6    7    8    9    10

 

> data_t <- matrix( rep(rep(1:7), 10), ncol=10, nrow=7, byrow=FALSE)

> data-t

Error in data - t : non-numeric argument to binary operator

> data_t

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

[1,]    1    1    1    1    1    1    1    1    1     1

[2,]    2    2    2    2    2    2    2    2    2     2

[3,]    3    3    3    3    3    3    3    3    3     3

[4,]    4    4    4    4    4    4    4    4    4     4

[5,]    5    5    5    5    5    5    5    5    5     5

[6,]    6    6    6    6    6    6    6    6    6     6

[7,]    7    7    7    7    7    7    7    7    7     7

> for (i  in 1:length(data_t)%/%10){

+ lines(data_t[i, ], lty = i)

+ }

 

>

 

 

 

 

颜色

col

> for (i in 2:length(data_t)%/%10){

+     lines(data_t[i, ], lty=i, type="l", col=palette()[i])

+ }

 

>

 

col  默认的绘图颜色,如某些函数line pie

col.axis 刻度线的文字颜色

plot(data_t[1, ], lty=1, type="l", xlim=c(1, 10), ylim=c(1, 10),col.axis="red" )

col.lab  坐标轴标签的颜色

col.main 标题的颜色

col.sub  副标题的颜色

fg          图形的前景色

bg          图形的背景色

plot(data_t[1, ], lty=1, type="l", xlim=c(1, 10), ylim=c(1, 10), col.axis="red" , col.lab="blue")

 

> n<- 10
> mycolors <- rainbow(n)
> pie(rep(1, n), labels = mycolors, col = mycolors)

 

 

 

文本属性

cex 放大缩小

cex.axis  坐标轴刻度文字的缩放倍数

cex.lab

cex.main

cex.sub

 

 

字体属性

font, 有4个属性 1=常规,2=粗体,3=斜体, 4=粗斜体,5=符号字体(以Adobe符号编码表示)

font.axis

font.lab

font.main

font.sub

ps

family   绘制文本时使用的字体族,标准的取值serif衬先,sans无衬线, mono等宽

 

在windows中可以通过windowsFont来创建这类映射

 

 

 

图形大小尺寸控制

 

eg:

par(pin=c(4,3))  一副4英寸宽,3英寸高

par(mai=c(1,0.5, 1, 0.2)) 上 下边界为1英寸,左边界为0.5英寸,右边界为0.2英寸

来个列子

par(pin=c(1,2))

pie(rep(1, n), labels = mycolors, col = mycolors)

par(pin=c(4,6))

pie(rep(1, n), labels = mycolors, col = mycolors)

par(pin=c(6,6))

pie(rep(1, n), labels = mycolors, col = mycolors)

 

 

dose <- c(20, 30, 40, 50, 60)

drugA <- c(16, 20, 27, 40, 60)

drugB <- c(15, 18, 25, 31, 40)

opar <- par(no.readonly=TRUE)

par(pin=c(2,2), mfrow=c(1,2))

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)

 

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

 

 

 

 

> plot(dose, drugA, type="b", col="red", lty=2, pch=2,lwd=2,main="clinical trails for drugA", sub = "this is hypothetical data", xlab="Dosage", ylab="Drug Response", xlim=c(0,60), ylim=c(0,70))
> title(main = "mytitle")
> plot(dose, drugA, type="b", col="red", lty=2, pch=2,lwd=2,sub = "this is hypothetical data", xlab="Dosage", ylab="Drug Response", xlim=c(0,60), ylim=c(0,70))
> title(main = "mytitle")

在plot中定义main title 或者用title() 来定义main title

 

 

坐标轴函数 axis()来创建

 

side 用1,2,3,4分别制定下 左,上 ,右四个方向

at 一个数值类型向量,表示需要绘制刻度线的位置

 

par(pin=c(4,5))

plot(dose, drugA, type="b", col="red", lty=2, pch=2,lwd=2,sub = "this is hypothetical data", xlab="Dosage", ylab="Drug Response", xlim=c(0,60), ylim=c(0,70))

axis(c(1,2,3,4))

axis(1, at=c(1:60))

axis(2, at=c(1:70))

 

x<- c(1:10)

y<- x

z<- 1/x

 

par(mar=c(5,4,4,8)+0.1)

plot(x,y, type="b", pch=21, lwd=2, col="red", yaxt="n", lty=3, ann=FALSE)

lines(x, z, type="b", pch=22, col="blue", lty=2)

此时的图像是

现在来标记xyz轴

 

 

mtext中line=1, 与line=6的区别

 

x<- c(1:10)

y<- x

z<- 10/x

par(mar=c(5,4,4,8)+0.1)

plot(x,y, type="b", pch=21, lwd=2, col="red", yaxt="n",  xaxt="n", lty=3, ann=FALSE)

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=-.01)

mtext("z=10/x", side=4, line = 3, cex.lab=1, las=2, col="blue")

mtext("y= x" ,side =2, line=3,cex.lab=1, las=2, col="red")

line  on which MARgin line, starting at 0 counting outwards.

mtext("Title Test", side=3, line = 3, cex.lab=1, las=2, col="blue", font=4)

title("Test B")  #也可以用这种方式来加Title

 

minor.tick次要刻度线

最近创建的图形中,都只有主刻度线,如果需要次刻度线,就需要使用Hmisc包中的minor.tick()函数进行创建

minor.tick(nx=n, ny=n, tich,ratio=n)

其中nx和ny分别制定了x轴和y轴没两条主刻度线之间次要刻度线划分得到的区间个数

minor.tick(nx=6, ny=6,  tick.ratio=0.5)

在x轴的每两条主刻度线之间添加5条次要刻度线,并在Y轴的每两条主刻度线之间添加5条次刻度线。

 

abline参考线

函数abline()可以用来为图形添加参考线,其使用格式为:

abline(h=yvalues, x=xvalues)

函数abline()中也可以指定其他图像参数

eg

abline(h=c(1,3,4,7))

即在y轴1,3,5,7的位置添加垂直的蓝色虚线。

 

 

legend图例

 

 

 

 

dose <- c(20, 30, 40, 50, 60)

drugA <- c(16, 20, 27, 40, 60)

drugB <- c(15, 18, 25, 31, 40)

opar<- par(no.readonly=TRUE)

par(lwd=2, cex=1.5, font.lab=2)

plot(dose, drugA, type="b", pch=15, lty=1, col="red", ylim=c(0,60), xlim=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(20,40), lwd=1.5, lty=2, col="gray")

library(Hmisc)

minor.tick(nx=3, ny=3, tick.ratio=0.5)

legend("topleft", inset=.05, title="Drug Type", c("A", "B"), lty=c(1,2), pch=c(15,17), col=c("red", "blue"))

par(opar)

 

 

 

text()/mtext文本标注

legend参数是一个向量字符,表示图例中的文字

dose <- c(20, 30, 40, 50, 60)

drugA <- c(16, 20, 27, 40, 60)

drugB <- c(15, 18, 25, 31, 40)

opar<- par(no.readonly=TRUE)

par(lwd=2, cex=1.5, font.lab=2)

plot(dose, drugA, type="b", pch=15, lty=1, col="red", ylim=c(0,60), xlim=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(20,40), lwd=1.5, lty=2, col="gray")

library(Hmisc)

minor.tick(nx=3, ny=3, tick.ratio=0.5)

legend(10,60,  title="Drug Type", legend=c("A", "B", "C"), lty=c(1,2), pch=c(15,17), col=c("red", "blue"))

par(opar)

 

 

 

opar<- par(no.readonly = TRUE)

par(cex=1.5)

plot(1:7, 1:7, type = "n")

text(3,3, "Example of default text")

text(4,4, family="mono", "Example fo mono-spaced text")

text(5,5, family="serif", "Example fo serif-spaced text")

 

 

attach(mtcars)

plot(wt,mpg, main = "wt Vs mpg", xlab = "wt" , ylab = "mpg", pch=18, col="blue")

text(wt,mpg, row.names(mtcars), cex=0.4, pos=4, col="red")

detach(mtcars)

pos=1 时在坐标下面

 

plotmath数学标注

在text中写数学符号

 

 

 

 

 

 

 

 

 

 

 

 

 

 

par/layout图形的组合

在R中使用par和layout函数可以容易的组合多幅图形

在par函数中使用mfrow=c(nrows, ncols)来创建按行填充的,行数为nrows, 列数为ncols的图形矩阵。

也可以用nfcol=c(nrows, ncols)按列来填充的矩阵

attach(mtcars)

opar<- par(no.readonly=TRUE)

par(mfrow=c(2,2))

plot(wt,mpg, main="Scatterplot of wt VS. mpg")

plot(wt, mpg, main="Scatterplot of wt VS. disp")

hist(wt, main="Histogram of wt")

boxplot(wt, main="Boxplot of wt")

 

attach(mtcars)

opar<- par(no.readonly=TRUE)

par(mfrow=c(3,1))

hist(wt)

hist(mpg)

hist(disp)

par(opar)

detach(mtcars)

 

对于hist都有一个默认的标题,如果要去掉这个标题,则设置main=“”就可以禁用它。或者是使用ann=FALSE

 

 

layout()函数调用形式为layout(mat), mat是一个矩阵,它制定了所要组合的多个图形所在的位置。

 

layout()mat用矩阵设置窗口的划分,矩阵的0元素表示该位置不画图,非0元素必须包括从1开始的连续的整数值,比如:1……N,按非0元素的大小设置图形的顺序。widths用来设置窗口不同列的宽度,heights设置不同行的高度。par()mfcol,mfrow参数也有类似layout的功能。layout()函数的一般形式为layout(mat)mat为一矩阵,mat元素的数量决定了一个output device被等分成几份相同元素为一块。

layout(matrix(c(1,2,3,0,2,3,0,0,3),nr=3)) matrix9个元素,具有这样的形式:

       [,1] [,2] [,3]

[1,]    1    0    0

[2,]    2    2    0

[3,]    3    3    3

把这个矩阵传入layout函数,我们就能得到这样的output device

attach(mtcars)

opar<- par(no.readonly=TRUE)

layout(matrix(c(1,1,1,2,2,3,4,5,5), 3,3, byrow=TRUE))

hist(wt)

hist(mpg)

hist(disp)

hist(wt)

hist(mpg)

par(opar)

detach(mtcars)

 

attach(mtcars)

opar<- par(no.readonly=TRUE)

layout(matrix(c(1,1,2,3), 2,2, byrow=TRUE))

hist(wt)

hist(mpg)

hist(disp)

par(opar)

detach(mtcars)

 

 

 

m<-matrix(1:4,2,2);m  #建立矩阵m,2列2行

layout(m,widths=c(1,3),heights=c(3,1)) #将当时装置按照m进行划分,宽度之比为1:3,高度之比为3:1

layout.show(4)

 

m<-matrix(c(1,1,2,1),2,2);m  #建立矩阵

layout(m,widths=c(2,1),heights=c(1,2)) #按照矩阵编号进行分割,编号相同的为同一块,宽度为2:1,高度为1:2

layout.show(2)

 

m<-matrix(0:3,2,2)#,注意,此矩阵中有00是不绘图的,可以查看一下效果

layout(m,c(1,3),c(1,3)) #行为1:3,列为1:3

layout.show(3)

 

 

layout.show(layout(matrix(c(1:9), 3,3),widths=c(1,2,3),heights=c(3,2,1)) )

 

 

fig图形的精准布局

 

fig=c(x1,x2,y1,y2),4个数值分别为左下角到左,右,下、上边界的距离与对应边的百分比数

 

 

要理解这幅图的绘制原理,请试想完整的绘图区域:左下角坐标为(0,0),而右上角坐标为
(1,1)。参数fig=的取值是一个形如c(x1, x2, y1, y2)的数值向量。

第一个fig=将散点图设定为占据横向范围0~0.8,纵向范围0~0.8。

上方的箱线图横向占据0~0.8,纵向0.55~1。右侧的箱线图横向占据0.65~1,纵向0~0.8。fig=默认会新建一幅图形,所以在添加一幅图到一幅现有图形上时,请设定参数new=TRUE。

我将参数选择为0.55而不是0.8,这样上方的图形就不会和散点图拉得太远。类似地,我选择
了参数0.65以拉近右侧箱线图和散点图的距离。你需要不断尝试找到合适的位置参数。

 

各独立子图所需空间的大小可能与设备相关。如果你遇到了“Error in plot.new(): figure margins too large”这样的错误,请尝试在整个图形的范围内修改各个子图占据的区域位
置和大小。

以上是R语言par(fig=)函数的详细解释的全部内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有R语言par(fig=)函数的详细解释的相关内容,欢迎继续使用右上角搜索按钮进行搜索数据处理--R语言 ,以便于您获取更多的相关知识。

opar <- par(no.readonly=TRUE)

par(fig=c(0, 0.5, 0, 0.5))

plot(wt, mpg, xlab="Miles Per Gallon", ylab="Car Weight")

par(fig=c(0, 1, 0.5, 1), new=TRUE)

hist(wt)

par(fig=c(0.5, 1,0, 0.5), new=TRUE)

hist(wt)

par(opar)

对于精准控制,一定要理解fig=c(x1,x2, y1,y2)对平面的剖分

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值