当前位置:   article > 正文

R语言绘图基础教程

r语言绘图

R语言绘图基础教程

一、R语言绘图初阶

首先下载包,绘制一张图:

#install.packages(c("Hmisc", "RColorBrewer"))
#install.packages(c("vcd", "plotrix", "sm", "vioplot"))
library(ggplot2, lib.loc=""~/R/lib"")

#生存一个可以修改的当前图形参数列表
par(ask=TRUE)
opar <- par(no.readonly=TRUE)

#载入数据集
attach(mtcars) # mtcars是R中自带的数据集,包含多种汽车的数据
plot(wt, mpg)
abline(lm(mpg~wt)) #abline是画一条或多条直线,lm是线性模型
title("Regression of MPG on Weight")

#不用了一定要detach
detach(mtcars)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

1、图形参数

字体、颜色、坐标轴、标签等通过**函数par()**来指定选项。以这种方式设定的参数值除非被再次修改,否则将在
会话结束前一直有效。其调用格式为:

par(optionname=value,optionname=name,...)
  • 1

不加参数地执行par()将生成一个含有当前图形参数设置的列表。添加参数no.readonly=TRUE可以生成一个可以修改的当前图形参数列表

例如一个药品的例子:

# Input data for drug example
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")
opar <- par(no.readonly=TRUE) #很重要
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

2、符号和线条

在这里插入图片描述

![](https://img-blog.csdnimg.cn/adbbc4f897b54e32837b21c408070915.png#pic_center

在这里插入图片描述

对上面的例子,现修改一些参数:

par(lty=2, pch=17)            # change line type and symbol
plot(dose, drugA, type="b")   # generate a plot
par(opar)                     # restore the original settings,恢复原设置 

plot(dose, drugA, type="b", lty=3, lwd=3, pch=17, cex=2)
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

在这里插入图片描述

3、颜色

在这里插入图片描述

# choosing colors
library(RColorBrewer)
n <- 7
mycolors <- brewer.pal(n, "Set1")
barplot(rep(1,n), col=mycolors)
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels=mycolors, col=mycolors)
  • 1
  • 2
  • 3

在这里插入图片描述

饼图改成灰色:

mygrays <- gray(0:n/n)
pie(rep(1, n), labels=mygrays, col=mygrays)
  • 1
  • 2

在这里插入图片描述

4、文本属性

# View font families 
opar <- par(no.readonly=TRUE)
par(cex=1.5)
plot(1:7,1:7,type="n")
text(3,3,"Example of default text")
text(4,4,family="mono","Example of mono-spaced text")
text(5,5,family="serif","Example of serif text")
par(opar)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

在这里插入图片描述

仍用上方药物例子:

par(lwd=2, cex=1.5)
par(cex.axis=.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)				
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

在这里插入图片描述

5、图形尺寸与边界尺寸

在这里插入图片描述

par(pin=c(2, 3))#图形大小
  • 1

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

可使用title()函数为图形添加标题和坐标轴标签,调用格式:
 title(main="maintitle", sub="subtitle", xlab="x-axis label", ylab="y-axis label")
  • 1
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(18, 60), ylim=c(0, 70)) #x轴刻度大小
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

你可以使用函数axis()来创建自定义的坐标轴,而非使用R中的默认坐标轴,其格式为:

axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...)  #例子在下面
  • 1

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/66055
推荐阅读
相关标签
  

闽ICP备14008679号