生物信息学生R入门教程–绘图基础

散点图及趋势线

一维点图使用dotchart函数。

> x <- mtcars[order(mtcars$mpg), ]  # 依据mpg排序
> x$cyl <- factor(x$cyl)  # cyl转换成factor
> x$color[x$cyl == 4] <- "red"
> x$color[x$cyl == 6] <- "blue"
> x$color[x$cyl == 8] <- "darkgreen"
> dotchart(x$mpg, labels = row.names(x), cex = 0.7, groups = x$cyl, main = "Gas Milage for Car Models\ngrouped by cylinder", 
+     xlab = "Miles Per Gallon", gcolor = "black", color = x$color)

图3: 按汽缸数分组不同车型油耗

二维散点图使用plot函数。直趋势线使用abline函数,拟合曲线在拟合后使用line函数绘制。

> attach(mtcars)
## The following objects are masked from mtcars (position 3):
## 
##     am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
> plot(wt, mpg, main = "Scatterplot Example", xlab = "Car Weight ", ylab = "Miles Per Gallon ", 
+     pch = 19)
> # 添加拟合曲线
> abline(lm(mpg ~ wt), col = "red")  # 渐近线 (y~x) 
> lines(lowess(wt, mpg), col = "blue")  # lowess line (x,y)

图4: 一个简单的拟合曲线例子

曲线使用lines函数。其type参数可以使用“p”, “l”, “o”, “b”, “c”, “s”, “S”, “h”, “n"等。

> x <- c(1:5)
> y <- x
> opar <- par(pch = 22, col = "blue")  # 使用蓝色绘制散点 
> par(mfrow = c(2, 4))  # 将画布分割成两行四列
> opts <- c("p", "l", "o", "b", "c", "s", "S", "h")
> for (i in 1:length(opts)) {
+     heading <- paste("type=", opts[i], sep = "")
+     plot(x, y, main = heading)
+     lines(x, y, type = opts[i])
+ }

图5: 不同类型的曲线图

> par(opar)

2 thoughts on “生物信息学生R入门教程–绘图基础

  1. 您好!
    我在绘制箱式图时boxplot(Pset, col = cols, ylim = c(0.95,1.22), main = “NUSE”,las = 2),图中细胞名称如MDB-MB-231,会自动变成MDA.MB.231。这该怎么调整?
    非常感谢!

    1. boxplot(Pset, col = cols, ylim = c(0.95,1.22), main = “NUSE”, las = 2, names=c(…, “MDB-MB-231”, …))。中间的…要换成MDB-MB-231前后的相应的名称。

发表评论

电子邮件地址不会被公开。 必填项已用*标注