R语言中基本图形的绘制

1、首先绘制一个简单的线性回归的例子

attach(mtcars)
plot(wt, mpg)
abline(lm(mpg ~ wt))
title("Regression of MPG on Weight")
detach(mtcars)

R语言中基本图形的绘制_第1张图片

以下的例子主要针对下表进行绘制


R语言中基本图形的绘制_第2张图片

dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type = "b")   dose是横坐标,drugA是纵坐标 type=b是折线图

R语言中基本图形的绘制_第3张图片

图形参数的介绍

修改图形的参数可以使用函数par(operation=value,operation=value,。。。)

这种修改的作用范围直到会话结束


opar <- par(no.readonly = TRUE)   得到可修改的参数列表
par(lty = 2, pch = 17)  修改图形的参数列表
plot(dose, drugA, type = "b")
par(opar)     将参数列表还原

R语言中基本图形的绘制_第4张图片

符号与线条

R语言中基本图形的绘制_第5张图片

颜色属性

R语言中基本图形的绘制_第6张图片

文本属性与字体属性

R语言中基本图形的绘制_第7张图片

R语言中基本图形的绘制_第8张图片

图形尺寸与边界尺寸

R语言中基本图形的绘制_第9张图片

以下是一个完整的例子。

ose <- 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)

R语言中基本图形的绘制_第10张图片

添加文本、自定义坐标轴,图例

plot(dose, 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(0, 


60), 
    ylim = c(0, 70))

R语言中基本图形的绘制_第11张图片

使用标题函数title()

title(main="",sub="",xlab="",ylab="",col.main="",col.sub="",cex.main="",cex.sub="")

自定义坐标轴

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)

R语言中基本图形的绘制_第12张图片

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(lwd = 2, cex = 1.5, font.lab = 2)
plot(dose, 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(dose, drugB, type = "b", pch = 17, lty = 2, 
    col = "blue")
abline(h = c(30), lwd = 1.5, lty = 2, col = "grey")
library(Hmisc)
minor.tick(nx = 3, ny = 3, tick.ratio = 0.5)
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)

R语言中基本图形的绘制_第13张图片

如何将坐标中点的信息显示在图上使用二个函数

text()mtext()

以下的例子是text()

attach(mtcars)
plot(wt, mpg, main = "Milage 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)

R语言中基本图形的绘制_第14张图片

图形的组合实现

par(mfrow=(nrows,ncols))默认按列填充

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)

R语言中基本图形的绘制_第15张图片

可以使用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)

R语言中基本图形的绘制_第16张图片


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语言中基本图形的绘制_第17张图片

你可能感兴趣的:(R)