设为首页 加入收藏

TOP

02图形初阶
2017-10-10 12:10:11 】 浏览:2643
Tags:图形

自定义坐标轴

axis()

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

其中的at和labels要一致,表示画刻度的位置。

次要刻度线

Hmisc包的minor.tick()函数

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

其中nx和ny指在两条主刻度线之间划分的区间个数,tick.ratio表示次刻度线相对于主刻度线的大小比例。

参考线

abline(h=c(1,5,7),v=seq(1,10,2),lty=2,col="blue")

h为水平参考线,v为垂直参考线(在1、3、5、7、9的位置)

图例

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

inset distance(s) from the margins as a fraction of the plot region when legend is placed by keyword.

文本标注

text(location,"text to place",pos,...)向绘图区内部添加文本。location可为一对x,y坐标,或location(1)用鼠标确定。

mtext("text to place",side,line=n,...)向图形四边之一添加文本。

图形组合

函数par()或layout()

par()

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)

mfrow=c(2,2)表示2行2列按行填充,mfcol=c(2,2)表示2行2列按列填充。

layout()

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

widths和heights表示各列的相对宽度和相对高度,第一行宽占比3、第二行宽占比1,同理第一列高占比1、第二列高占比2。绝对宽度用lcm()来指定,如widths=lcm(5)。

其中2,2表示按2行2列排列。

图形布局的精细控制

fig()

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)

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇r语言中怎样查看函数源代码 下一篇R语言和机器学习---重要网站分享

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目