码迷,mamicode.com
首页 > 其他好文 > 详细

02图形初阶

时间:2016-03-29 14:44:50      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

自定义坐标轴

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)

 

02图形初阶

标签:

原文地址:http://www.cnblogs.com/keyang/p/5332739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!