Primary graphics summary - used to draw graphics plot function

First, the use plot () function creating simple graphic
example

#描述病人对两种药物五个计量水平上的响应情况
#计量水平
does <- c(20,30,40,45,60)
#对药物A的响应情况
drugA <- c(16,20,27,40,60)
#对药物B的响应情况
drugB <- c(15,18,25,31,40)
#创建药物A的计量和响应关系图形
#plot()是R中为对象作图的一个泛型函数(它的输出将根据绘制对象类型的不同而变化)
plot(does,drugA,type="b")

Here Insert Picture Description
Second, add graphics parameters
by the parameter from a plurality of pattern features defined patterns (fonts, colors, axis, title, etc.)
a method of adding pattern, there are two parameters by adding one is a function of PAR (), one is directly the plot drawing functions and other advanced added directly.
Use par () function adds a graphical parameters unless otherwise will be modified before the end of the session (that is, in this document remain in effect unless another document or use dev.new () function to re-open a graph window) once again remains in effect
for par () no.readonly = TRUE parameter function may generate a list of the current graphics parameters can be modified. So before using par () function to modify parameters usually save up current unmodified graphical parameter list, in order to restore to the state before the unmodified after. E.g

#把当天的能够修改的图形参数列表赋值给opar
opar <- par(no.readonly = TRUE)
恢复为图形参数为opar的状态
par(opar)

Without adding any parameters par () function may generate a list containing all the parameters of the current drawing
examples

#例子
#首句复制了一份当前的图形参数设置,
opar <- par(no.readonly = TRUE)
#第二句将默认的线条类型修改为虚线(lty=2)并将默认的点的符号改变成了实心的三角(pch=17)
#注意修改了图形的设置并不会马上就变还要再次执行Plot语句才能改变,所以有了第三句
par(lty = 2,pch=12)
plot(does,drugA,type="b")
#这一句还原了原始设置,相当于结束了par(lty = 2,pch=12)参数设置(因为不结束会一直是这个设定),
#用于下次的作图,所以一般都会在最后一句结束Par语句的设置
par(opar)

Here Insert Picture Description
Another method of modifying the graphic parameter directly added directly to plot the like as advanced graphics functions.
In this case, the specified option is only valid piece of graphic itself,
pay attention to different advanced drawing functions to modify the graphics are not the same parameters can be specified directly.

Third, the symbols and lines
used to specify the type of parameter symbols and line
symbols specified PCH plotted points, 21 to 25 for the symbol may further specify the color of the border (col =) and fill color (bg =)
size cex specified symbol, cex is a value representing the size of a drawing symbol relative to the default zoom level.
lty specify the line type
lwd designated line width is a value representing the size of the line with respect to the default size of the zoom factor
examples

plot(does,drugA,type="b",lty=3,pch=21,cex=2,lwd=3,col="blue",bg="red")

Here Insert Picture Description

四,颜色
颜色的相关参数
col 默认的绘图颜色,某些函数(如lines和pie)可以接受一个含有颜色值的向量并自动循环使用
例如,如果设定col = c(“red”,“blue”)并需要绘制三条线,则第一条线将为红色,第二条线将为蓝色,第三条线又将为红色。
col.axis 坐标轴刻度文字的颜色
col.lab 坐标轴标签的颜色
col.main 标题颜色
col.sub 副标题颜色
fg 图形的前景色
bg 图形的背景色

颜色的名称
在R中,可以通过颜色下标,颜色名称,十六制的颜色值,RGB值或HSV值来指定颜色。
如,col = 1,col=“white”,col = “#FFFFFF”,col=rgb(1,1,1) 和col=hsv(0,0,1)都是指白色
函数rgb()可基于红-绿-蓝三色值生成颜色,而hsv()则基于色相-饱和度-亮度值来生成颜色。
函数colors()可以返回所有可用颜色的名称

创建复杂连续的颜色
R中也有多种用于创建连续型颜色向量的函数(可以生成颜色向量)包括rainbow(),heat.colors(),terrain.colors(),topo.colors(),cm.colors()
RColorBrewer包可以创建吸引人的颜色配对。
使用RColorBrewer包中的brewer.pal(n,name)可以创建一个颜色值的向量例如

install.packages("RColorBrewer")
library(RColorBrewer)
n <- 7
mycolors <- brewer.pal(n,"Set1")
barplot(rep(1,n),col=mycolors)

Here Insert Picture Description
多阶灰度色可以使用基础安装所自带的gray()函数生成。这时要通过一个元素值为0和1之间的
向量来指定各颜色的灰度。gray(0:10/10)将生成10阶灰度色。例子

n <- 10
mycolors <- heat.colors(n)
pie(rep(1,n),labels = mycolors,col=mycolors)
mygrays <- gray(0:n/n)
pie(rep(1,n),labels=mygrays,col=mygrays,main = "pie",col.main="red")
#例子2
plot(does,drugA,type="b",main = "color")
opar <- par(no.readonly = TRUE)
par(col.axis = "green")
par(col.lab="blue")
par(fg="blue")
par(bg="yellow")
par(col.main="red")

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
五,文本属性
图形参数同样可以指定字号,字体,字样。
用于指定文本大小的参数
cex 表示相对于默认大小缩放倍数的数值
cex.axis 坐标轴刻度文字的缩放倍数
cex.lab 坐标轴标签的缩放倍数
cex.main 标题的缩放倍数
cex.sub 副标题的缩放倍数
用于指定字体族,字号,和字样的参数
font 整数。用于指定绘图使用的字体样式。1 = 常规,2 = 粗体,3 = 斜体,4 = 粗斜体,5 = 符号字体
font.axis 坐标轴刻度文字的字体样式
font.lab 坐标轴标签的字体样式
font.main 标题的字体样式
font.sub 副标题的字体样式
ps 字体磅值。文本的最终大小为ps*cex
family 绘制文本时使用的字体族。标准的取值为serif(衬线),sans(无衬线)和mono(等宽)如果对这些字体都不满意就需要使用函数windowsFont()函数自己来创建字体映射,如果满意上述字体就可以直接指定family=serif等字体不用再创建映射
例子

par(opar)
par(font.lab = 3,cex.lab = 1.5, font.main = 4, cex.main=2)
plot(does,drugA,type="b",main = "color")

Here Insert Picture Description
字体的设置
字体族的设置,如果对R提供的几种字体并不满意则需要自己创建字符映射,在windows中,需要通过函数windowsFont()来创建映射。再把这些映射通过family来改变字体
注意不能直接使用family直接设置字体,需要先进行映射。注意通过par(family=“A”)是修改所有的字体类型
如何进行区分修改需要在专门的函数内修改,如title(“ziti”,family=“B”),这样就是只修改了标题的字体
例子

#例子1
windowsFonts(A=windowsFont("Arial Black"), B=windowsFont("Bookman Old Style"), c=windowsFont("Comic Sans MS"))
par(family="A")
plot(does,drugA,type="b",main="color")
#例子2
par(opar)
plot(does,drugA,type="b")
title("ziti",family = "A")

Here Insert Picture Description
Here Insert Picture Description
使用系统自带的字体

par(opar)
plot(does,drugA,type="b")
title("ziti",family = "sans")

Here Insert Picture Description

如果是以PDF或PostScript格式输出图形,则修改字体族可以直接通过family = "fontname"设置字体
可以通过names(pdfFonts())和names(postscriptFonts())查询字体列表

六,图形尺寸与边界尺寸

用于控制图形尺寸和边界大小的参数
pin 以英寸标识的图形尺寸(宽和高)
mai 以数值向量表示的边界大小,顺序为“下,左,上,右,单位为英寸
mar 以数值向量表示的边界大小,顺序为“下,左,上,右,单位为英分。默认值为c(5,4,4,2) +01
例子

表示一幅4英寸宽,3英寸高,上下边界为1英寸,左边界为0.5英寸,有边界为0.2英寸的图形
par(pin=c(4,3),mai=c(1,0.5,1,0.2))

例子2

#回复为原始的默认设置
par(opar)
#设置为一个图形窗口画多个图
par(mfrow=c(1,2))
#生成2英寸宽3英寸高的图形尺寸
par(pin=c(2,3))
#线条宽度为2,字体大小为默认值的1.5倍
par(lwd=2,cex=1.5)
#坐标轴刻度文字大小为0.75倍,斜体
par(cex.axis=0.75,font.axis=3)
plot(does,drugA,type="b",pch=19,lty=2,col="red")
plot(does,drugB,type="b",pch=23,lty=6,col="blue",bg="green")
par(opar)

Here Insert Picture Description
七,添加文本,自定义坐标轴和图例
除了使用图形参数,许多高级绘图函数本身就可以自行设定坐标轴和文本标注。
注意有些高级函数已经包含了标题和标签,可以在高级绘图函数plot()等语句或单独的par()语句中添加ann=FALSE来移除他们
例子

plot(does,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(20,60),ylim = c(0,70))

Here Insert Picture Description
7.1,标题
1,可以使用title()函数为图形添加标题和坐标轴标签。格式为:
title(main = “main title”,sub = “subtitle”,xlab = “x-axis label”, ylab = “y-axis label”),
2,函数title()中也可以指定其他图形参数比如文本大小字体,旋转角度和颜色等
3,注意有些高级函数已经包含了标题和标签,可以在高级绘图函数plot()等语句或单独的par()语句中添加ann=FALSE来移除他们,
4,函数title()一般来说用于添加信息到一个默认标题和坐标轴标签被ann=FALSE选项移除的图形中
例子

plot(does,drugA,type="b",col="red",lty=2,pch=2,lwd=2,ann = FALSE)
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, font.lab = 2)
#ann = FALSE用于移除默认标题和坐标轴标签

Here Insert Picture Description

7.2坐标轴
可以使用函数axis()来创建自定义的坐标轴,而非使用R中默认的坐标轴。其格式为
axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, …)

坐标轴选项
side 一个整数,表示在图形在哪边绘制坐标轴(1=下,2=左,3=上,4=右)
at 一个数值型向量,表示需要绘制刻度线的位置
labels 一个字符型向量,表示置于刻度线旁边的文字标签(如果为NULL,则直接使用at中的值)
pos 坐标轴线绘制的坐标(既与另一条坐标轴相交位置的值)
lty 线条类型
col 线条和刻度线颜色
las 标签是否平行于(=0)或垂直于(=2)坐标轴
tck 刻度线的长度,以相对于绘图区域大小的分数表示(负值表示在图形外侧,正值表示在内侧,0表示禁用刻度,1表示绘制网格线);默认值为-0.01

创建自定义坐标轴时,需要禁用高级绘图函数自动生成的坐标轴。参数axes=FALSE将禁用全部坐标轴
(包括坐标轴框架;除非你添加了参数frame.plot = TRUE).参数xaxt="n"和yaxt="n"将分别禁用X轴或Y轴(会留下框架线,只是去除了刻度)

axis, axes, xaxt, yaxt之间的区别
axis为一个函数使用用来创建自定义的坐标轴的
axes是一个可以在高级绘图函数和par函数中使用的参数是用来禁用全部坐标轴的
xaxt和yaxt也是一个在高级绘图函数和par函数中使用的参数,但是与axes不同的是它们是分别用来禁用x轴和y轴的而且这两个参数禁用后会留下框架线,只是去除了刻度线,而axes禁用后框架也没了。

例子

x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly = TRUE)
par(mar=c(5,4,4,8) +0.1)
#禁用了y轴,禁用了标题
plot(x,y,type="b",pch=21,col="red",lty=3,yaxt="n",ann=FALSE)
#添加x对1/x的直线
lines(x,z,type="b",pch=22,col="blue",lty=2)
#设置y轴,标签垂直于坐标轴
axis(2,at=x,labels=x,col.axis="red",las=2)
#round(),取整函数,digits=2表示保留两位小数
axis(4,at=z,labels= round(z,digits = 2), col.axis="blue",las=2,cel.axis=0.7,tck=-0.01)
mtext("y=1/x",side=4,line=3,cel.lab=1,las=2,col="blue")
title("An Example of Creative Axes",
      xlab = "X values",
      ylab = "Y=X",col.lab="blue")
par(opar)

Here Insert Picture Description

7.3,参考线
函数abline()可以用来为图形添加参考线。其格式为
abline(h=yvalues,v=xvalues)
例子

在Y为1,5,7的位置添加了水平实线
par(opar)
plot(c(1:10),c(1:10),type="b")
abline(h=c(1,5,7))
abline(v=seq(1,10,2),lty=2,col="blue")

Here Insert Picture Description

7.4,图例
其格式为,legend(location,title,legend,…)
常用的图例参数
location 指定图例的位置,可以通过x,y坐标指定,可以执行locator(1),然后通过鼠标单击给出图例位置,还可以使用关键字bottom,bottomleft,left,topleft,top,topright,right,bottomright,center放置图例。
如果你使用了以上的某个关键字来确认位置,那么可以同时使用参数inset=指定图例向图形内侧移动的大小
title 图例标题的字符串(可选)
legend 图例标签组成的字符型向量
…其他选项,如果图例标示的是颜色不同的线条,需要指定col=加上颜色值组成的向量。如果图例标示的是符号不同的点,则需要指定pch=加上符号的代码组成的向量。如果图例标示的是不同的线条宽度或者线条类型需使用lwd=或lty=加上宽度值或类型值组成的向量,要为图例创建颜色填充的盒型(常见于条形图,箱型图或饼图)需要使用参数fill=加上颜色值组成的向量
其他常用的图例选项还有用于指定你盒子样式的bty,指定背景色的bg,指定大小的cex以及指定文本颜色的text.col
指定horiz=TRUE将会水平放置图例
例子

par(opar)
#增加线条,文本,符号标签的宽度或大小
par(lwd=2,cex=1.5,font.lab=2)
#绘制图形
plot(does,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(does,drugB,type="b",pch=17,lty=2,col="blue")
abline(h=c(30),lwd=1.5,lty=2,col="gray")
#install.packages("Hmisc")
library(Hmisc)
#添加次要刻度线
#nx和ny分别指定了X轴和Y轴每两条主刻度线之间通过次要刻度线划分得到的区间个数,tick.ratio表示次要刻度线相对于主刻度线的大小比例
minor.tick(nx=3,ny=3,tick.ratio = 0.5)
#title图例名称,c("A","B")图例标签
legend("topleft",inset = 0.05,title = "Drug Type",c("A","B"),
       lty=c(1,2),pch=c(15,17),col=c("red","blue"))
par(opar)

Here Insert Picture Description

7.4,文本标注
通过text()和mtext()将文本添加到图形上。text()可向绘图区域内部标注文本,mtext()则向图形的四个边界之一添加文本
text(location,“text to place”,pos, …)
mtext(“text to plaxe”,side,line=n,…)
text()函数常用参数
location 文本的位置参数,可为一对x,y坐标,也可通过指定location为locator(1)使用鼠标交互式地确定摆放位置
labels 需要填写的文本
pos 文本相对于位置参数的方位。1=下,2=左,3=上,4=右。如果指定了Pos,就可以同时指定offset=作为偏移量以相对于单个字符宽度的比例表示
mtext()函数常用参数
side 指定用来放置文本的边,1=下,2=左,3=上,4=右。
text 需要填写的文本
line 用来指定内移或外移文本的大小,随着值的增加,文本将外移。也可使用adj=0将文本向左下对其。或使用adj=1右上对齐
除了用来添加文本标注外,text()函数也通常用来标示图形中的点。

函数plotmath()函数可以为图形主题或边界上的标题,坐标轴名称或文本标注添加数学符号
可以使用demo(plotmath)查看即时效果
例子

attach(mtcars)
plot(wt,mpg,
     main="Mileage vs. Car Weight",
     xlab = "Weight",ylab="Mileage",
     pch=18,col="blue")
#row.names()行的名称,pos文本相对于位置参数的方位
text(wt,mpg,row.names(mtcars),
     cex=0.6,pos=4,col="red")
row.names(mtcars)

Here Insert Picture Description
7.5 图形的组合
在R中使用函数par()或layout()可以容易地组合多副图形为一幅总括图形

1, specify a combination of graphics parameters mfrow to the position of each pattern
() function using the graphical parameter mfrow = c (nrows, ncols) to create rows filling pattern matrix, in par
using mfcol = c (nrows, ncols) to Creating columns filled figure matrix
example

attach(mtcars)
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")
detach(mtcars)
#例子2
attach(mtcars)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)

Here Insert Picture Description
Here Insert Picture Description2, using the layout () function to specify the location of each graphics combination patterns
with respect mfrow () of each pattern is consistent with the default size, layout () can occupy a plurality of positions in FIG
function layout () call to form as layout (mat), wherein the mat is a matrix that specifies the plurality of graphics to be combined
is located.
example

attach(mtcars)
layout(matrix(c(1,1,2,3),2,2,byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)

Here Insert Picture Description(Single complete arrangement and overlay graphics), 3, using par () function in fig parameter to complete a fine control of the graphical layout
example

attach(mtcars)
par(fig=c(0,0.8,0.55,1),new=TRUE)
plot(wt,mpg,xlab = "Miles Per Gallon",ylab = "Car Weight")
par(fig=c(0,0.8,0.55,1),new=TRUE)
boxplot(wt,horizontal = TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0,8),new=TRUE)
boxplot(mpg,axes=FALSE)
mtext('Enhanced Scatterplot',side=3,out=TRUE,line=-3)

Published 39 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42712867/article/details/96597738