注: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)
图形的组合
par( mfrow = c( 2 , 2 ) )
par( mfcol = c( 2 , 2 ) )
layout( matrix( 1 , 1 , 2 , 3 ) , 2 , 2 , byrow = TRUE , widths = c( 3 , 1 ) , heights = c( 1 , 2 ) )
注: