2017-05-27 19:16:25
par
: 指定版式和全局参数plot
, plot.new
smoothScatter
, barplot
, boxplot
, bxp
, dotchart
, hist
, pie
, stars
pairs
, spineplot
, matplot
, assocplot
, coplot
, cdplot
, stripchart
, sunflowerplot
, mosaicplot
, fourfoldplot
, symbols
lines
, abline
, segments
, points
, pch
, box
, rect
, curve
, arrows
, polygon
, polypath
, matlines
, matpoints
title
, legend
, text
, mtext
axis
, grid
, rug
par
par()
打印所有制图版式参数的当前取值par("<参数>")
打印某个特定参数的当前取值par(<参数>=...)
修改特定参数的取值> par("mar") [1] 5.1 4.1 4.1 2.1 > par(mar=c(5, 4, 4, 2)) > par("mar") [1] 5 4 4 2
mfrow
: 向量c(<行个数>, <列个数>)
,按行序出图par(mfrow=c(2, 2), mai=c(0.8, 0.8, 0, 0.2)) for (i in 0:1) for (j in 1:0) with(subset(mtcars, am==i & vs==j), plot(wt, mpg))
mfcol
: 向量c(<行个数>, <列个数>)
,按列序出图par(mfcol=c(2, 2), mai=c(0.8, 0.8, 0, 0.2)) for (i in 0:1) for (j in 1:0) with(subset(mtcars, am==i & vs==j), plot(wt, mpg))
mar
: 向量c(下, 左, 上, 右)
,单位是“行”
mai
: 向量c(下, 左, 上, 右)
,单位是“英寸”
oma
: 向量c(下, 左, 上, 右)
,单位是“行”,默认值: c(0, 0, 0, 0)omi
: 向量c(下, 左, 上, 右)
,单位是“英寸”,默认值: c(0, 0, 0, 0)omd
: c(x1, x2, y1, y2),单位是相对于设备尺寸的"%",默认值: c(0, 1, 0, 1)还有一些美学/视觉效果类参数。具体请查阅?par
bg
: 背景色cex
: 注释图文的大小family
: 字体fg
: 前景色font
: 字体效果lty
: 线条的种类lwd
: 线宽pch
: 点的种类散点图: 两变量相关性。数据很多时,用smoothScatter
with(mtcars, plot(wt, mpg, type='p'))
# 默认type='p'
文本型变量作散点图
stripchart(decrease ~ treatment, vertical=TRUE, data=OrchardSprays)
直方图: 单变量密度分布
with(mtcars, hist(mpg))
或用线段图
with(mtcars, plot(mpg, type='h'))
连续性变量的分布
boxplot(ToothGrowth$len)
多个箱式图
boxplot(len ~ dose, data = ToothGrowth)
分类变量的比较
hp <- mtcars$hp names(hp) <- row.names(mtcars) barplot(hp)
自变量较多时用横向条图
hp <- mtcars$hp names(hp) <- row.names(mtcars) barplot(hp, horiz=TRUE)
表示趋势
plot(AirPassengers) # 时间序列数据默认type='l'
阶梯折线
plot(AirPassengers, type='s') # 阶梯图
pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species", pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)])
iris.S=array(NA, dim=c(50, 4, 3), dimnames=list(NULL, colnames(iris)[-5], levels(iris$Species))) for(i in 1:3) iris.S[,,i] <- data.matrix(iris[1:50+50*(i-1), -5]) matplot(iris.S[,"Petal.Length",], iris.S[,'Petal.Width',], pch="scv")
x <- margin.table(Titanic, c(1,4)) assocplot(x)
coplot(lat ~ long | depth, data = quakes)
with(iris, stripchart(Sepal.Width ~ Species))
sunflowerplot(iris[, 3:4])
mosaicplot(~ Sex + Age + Survived, data = Titanic, color = TRUE)
with(iris, spineplot(Species ~ Sepal.Width))
with(iris, cdplot(Species ~ Sepal.Width))
x <- aperm(UCBAdmissions, c(2, 1, 3)) fourfoldplot(x, mfcol=c(2, 3))
par(mfrow=c(1,3)) plot(cars) plot(cars);points(cars[cars$dist > 2.6 * cars$speed,], col='red') plot(cars);points(cars[cars$dist > 2.6 * cars$speed,], pch=19)
pch
pch
代表点的种类,可以是整数
你甚至可以用其他文本符号
plot(cars, pch="\u8F66", cex=0.75)
par(mfrow=c(1,3)) plot(cars) plot(cars);lines(stats::lowess(cars)) plot(cars);abline(h=40,v=15,col="red")
lty
lty
是线条形状,可以是整数,或一个不超过8位的数值文本(连-断-连-断…的形式)
plot(cars, type='n') for (i in 1:6) abline(h=20*i, lty=i)
x <- stats::runif(12); y <- stats::rnorm(12) plot(x, y) s <- seq(length(x)-1); s <- s[-length(s)] segments(x[s], y[s], x[s+2], y[s+2], col= 'pink')
x <- stats::runif(12); y <- stats::rnorm(12) plot(x, y) s <- seq(length(x)-1) # one shorter than data arrows(x[s], y[s], x[s+1], y[s+1], col= 1:3)
plot(cars) rect(10, 20, 20, 80, border="red")
plot(cars) polygon(c(10, 20, 20, 10), c( 20, 30, 100, 60), border="gray")
par(mfrow=c(1, 4)) plot(cars) plot(cars); title("Scatter Dist vs Speed", sub="1920s") plot(cars, xlab="Speed (mph)", ylab="Stopping distance (ft)") plot(cars); box(col="blue")
par(mar=c(4, 4, 1, 1)) with(mtcars, plot(wt, mpg, type="n")) # 不出图 with(subset(mtcars, am==0), text(wt, mpg, labels=row.names(mtcars), cex=0.6, col=1)) with(subset(mtcars, am==1), text(wt, mpg, labels=row.names(mtcars), cex=0.6, col=2))
par(mar=c(4, 4, 1, 1)) with(mtcars, plot(wt, mpg), type='n') # 不出图 with(subset(mtcars, am==0), points(wt, mpg, pch=20, col=1)) with(subset(mtcars, am==1), points(wt, mpg, pch=20, col=2)) legend("topright", pch=20, col=c(1, 2), legend=c("Auto", "Manual"))
extrafont
包family
参数可指定字体,但默认只支持"serif"、"sans"、"mono"等值extrafont
可将操作系统字体映射到R作图系统install.packages("extrafont")
extrafont::font_import()
fonttable
查看映射字体名称fonttable()$FullName
列表中的注册字体名称
library(extrafont) with(mtcars, plot(wt, mpg, main='汽车', family="Microsoft YaHei"))
?plotmath
)expression()
plot(0, 0, main = expression(theta == 0), ylab = expression(hat(gamma) == 0), xlab = expression(sum(x[i] * y[i], i==1, n)))
x <- rnorm(100) hist(x, xlab=expression( "The mean (" * bar(x) * ") is " * sum(x[i]/n,i==1,n)))
substitute
函数如果表达式中包含实时运算,要用substitute
函数转义
par(mar=c(4, 4, 1, 1)) x <- rnorm(100) y <- x + rnorm(100, sd = 0.5) plot(x, y, xlab=substitute(bar(x) == k, list(k=mean(x))), ylab=substitute(bar(y) == k, list(k=mean(y))))
Thank you!