0018-R语言绘图基础


  1. 2020-07-29更新:使用代码块,看起来更方便、更改了第6节;

这次不介绍如ggplot2等绘图包, 只记录一些R基础就能实现的东西~

跟着我爪子敲一遍,相信你会有收获!

有用的话请点赞收藏┗|`O′|┛ 嗷~~

能点个大大的关注就更感谢了 !! Orz~


目录:

  1. plot绘图基础
  2. boxplot-箱线图
  3. 使用plot函数一样可以绘制箱线图(boxplot)
  4. plot来绘制Dataframe信息
  5. pairs函数绘制matrix和Dataframe
  6. coplot函数绘制协同图
  7. hist绘制直方图
  8. dotchart绘图
  9. axes-边框控制
  10. log参数,对数据取对数
  11. type参数-点的类型
  12. 图的标注xlab ylab main sub
  13. points加点函数
  14. line-加线函数
  15. par函数
  16. 使用par与lwd
  17. text函数添加标记
  18. abline-绘制参考线
  19. polygon-给图加上多边形
  20. title函数给图形加标题
  21. axis-控制坐标轴
  22. xlim与ylim-坐标轴的取值范围

!!!多图

流量预警

tips:

- "##"代表output

-"#"代表注释


1. plot绘图基础

x <- c(1,2,3,4)
y <- c(5,1,2,9)
plot(x,y)

plot(x)

  • 使用plot绘制factor
y <- factor(c("a","b","c","b","a","a","a","C","c"))
plot(y)

a <- sample(c("a","b","c"), 10, replace=T)
a
##  [1] "b" "b" "c" "a" "b" "b" "a" "a" "b" "b"
f <- factor(a)
f
##  [1] b b c a b b a a b b
## Levels: a b c
plot(f)


2. boxplot

举个栗子

par(mfrow=c(1,3))
a <- sample(1:100, 100, replace = T)
a
##   [1]  30  73  45  64  62  52  79  72  82  10  28  46  90  99   1  89  24  32
##  [19]  74  97  47  55  19  20  64  10  36   7  87  98  35  58  82  52  13  39
##  [37]  21   7  40  34  69  54  92  34   5  34  13  36   5  92  10  67  67  39
##  [55]   3  22  23  81  95  16  33  10  69  84  53  25  79  28  88  16  83  75
##  [73]  76  58 100  32  98  13  50  12  93  87  95   3  57  87   7  31  16  49
##  [91]  61  71  47  37  14  11  83  75  29  38
boxplot(a)

如果你不懂par的用法

?par

然后就会在Rstudio右下角出现帮助信息

然后par(mfrow=c(1,3))的意思就是一个图版显示1行3列

添加几个异常值

b <- c(a, -180:-190, 181:180)
boxplot(b)

这样就看到在max最大值之上,min最小值之下,都有一些“离群点”

接下来引用R自带的一个数据集InsectSprays

class(InsectSprays)
## [1] "data.frame"
summary(InsectSprays)
##      count       spray
##  Min.   : 0.00   A:12
##  1st Qu.: 3.00   B:12
##  Median : 7.00   C:12
##  Mean   : 9.50   D:12
##  3rd Qu.:14.25   E:12
##  Max.   :26.00   F:12InsectSprays
##    count spray
## 1     10     A
## 2      7     A
## 3     20     A
## 4     14     A
## 5     14     A
## 6     12     A
## 7     10     A
## 8     23     A
## 9     17     A
## 10    20     A
## 11    14     A
## 12    13     A
## 13    11     B
## 14    17     B
# ……太长略过
## 69    26     F
## 70    26     F
## 71    24     F
## 72    13     F
# 注意,~表示因果关系,这里的意思是横坐标(自变量)为count,纵坐标(因变量)为spray
boxplot(count~spray, data=InsectSprays)

加上颜色

boxplot(count~spray, data=InsectSprays, col=2:7)

进阶的boxplot

boxplot(count~spray, data = InsectSprays, col = "lightgray")

boxplot(count~spray, data = InsectSprays, col = "lightgray")
boxplot(count~spray, data=InsectSprays,notch=T, add=T, col=2:7)
## Warning in bxp(list(stats = structure(c(7, 11, 14, 18.5, 23, 7, 12, 16.5, : some
## notches went outside hinges ('box'): maybe set notch=FALSE# notch关键字: 保证我绘的图有凹进去的一小块
# add关键字: 将图画在原来的图之上


3. 使用plot函数一样可以绘制箱线图(boxplot)

plot将第一个参数输入为factor就可以画箱图

par(mfrow=c(1,2))
y <- c(10,506,140,200)
x <- c(1,2,1,2)
plot(x,y)a <- y
b <- x
b <- as.factor(b)
plot(b,a)

下面实现这张图

y <- c(88,99,66,77,88,97,33,55,66,99,88,99,77,55,66,77,98,99,96,90,80)
y
##  [1] 88 99 66 77 88 97 33 55 66 99 88 99 77 55 66 77 98 99 96 90 80
f <- factor(c(rep("班级1",6), rep("班级2",3), rep("班级3",5), rep("班级4",7)))
f
##  [1] 班级1 班级1 班级1 班级1 班级1 班级1 班级2 班级2 班级2 班级3 班级3 班级3
## [13] 班级3 班级3 班级4 班级4 班级4 班级4 班级4 班级4 班级4
## Levels: 班级1 班级2 班级3 班级4
plot(f,y)


4. plot来绘制Dataframe信息

  • plot(df): df是Dataframe
  • plot(~expr): expr是对象名称的表达式
  • plot(y~expr): y是任意一个对象
df <- data.frame(age=c(10,12,13),height=c(150,160,170),weight=c(50,60,70)
)
df
##   age height weight
## 1  10    150     50
## 2  12    160     60
## 3  13    170     70
plot(df)

plot(~age+height,data=df)

plot(weight~age+height, data=df)


5. pairs函数绘制matrix和Dataframe

x <- matrix(1:9, nrow = 3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
pairs(x)

x <- matrix(1:10, nrow = 2)
x
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
pairs(x)

所以一列就是一个分量var

x <- matrix(1:10, nrow = 5)
x
##      [,1] [,2]
## [1,]    1    6
## [2,]    2    7
## [3,]    3    8
## [4,]    4    9
## [5,]    5   10
pairs(x)

这里就是两个分量var

6. coplot函数绘制协同图

a <- c("a","b","a","b","a","b")
n1 <- c(1,2,3,4,5,6)
n2 <- c(100,200,300,400,500,600)
df <- data.frame(a,n1,n2)
df
##   a n1  n2
## 1 a  1 100
## 2 b  2 200
## 3 a  3 300
## 4 b  4 400
## 5 a  5 500
## 6 b  6 600
coplot(n1~n2|a)

意思就是n1做纵坐标(因变量), 左边是含有“a”字符的feature的n2的值作为横坐标(自变量),右边是含有“b”的n2值作为横坐标

所以coplot可以绘制的就是,在给定一个feature,这里是factor a的情况下,分别绘制另外两个feature(这里是n1,n2)之间的关系


7. hist绘制直方图

data <- c(rep(1,20),rep(2,11),rep(3,6))
hist(data,breaks = c(0.5,1.5,2.5,3.5))

beaks断点,控制直方图每个方形的起始 下面了解freq,控制显示Frequency频次还是Density频率, main控制title

par(mfrow=c(1,2))
hist(data,breaks = c(0.5,1.5,2.5,3.5), freq = T, main="freq=T")
hist(data,breaks = c(0.5,1.5,2.5,3.5), freq = F, main="freq=F")

太丑了,加颜色

par(mfrow=c(1,2))
hist(data,breaks = c(0.5,1.5,2.5,3.5), freq = T, main="freq=T",col = "red")
hist(data,breaks = c(0.5,1.5,2.5,3.5), freq = F, main="freq=F",col = rainbow(3))

8. dotchart绘图 横轴是数值,纵轴是标签

这里引用R自带的数据VADeaths,为Virginia州在1940年的人口死亡率

VADeaths
##       Rural Male Rural Female Urban Male Urban Female
## 50-54       11.7          8.7       15.4          8.4
## 55-59       18.1         11.7       24.3         13.6
## 60-64       26.9         20.3       37.0         19.3
## 65-69       41.0         30.9       54.6         35.1
## 70-74       66.0         54.3       71.1         50.0
dotchart(VADeaths)

9. axes-边框控制

- axes=FALSE: 表示图形没有坐标轴

- axes默认为TRUE

x <- 1:100
y <- rnorm(100)
par(mfrow=c(1,2))
plot(x,y)
plot(x,y,axes=F)

10. log参数,对数据取对数

- log = “x”: 表示对x轴的数据取对数

- log = “y”: 表示对y轴的数据取对数

- log = “xy”: 表示对x轴y轴的数据同时取对数

x <- c(100:400)
y <- c(100:400)
par(mfrow=c(2,2))
plot(x,y,main = "no log")
plot(x,y,log="x", main = "logX")
plot(x,y,log="y", main = "logY")
plot(x,y,log="xy",main = "logX, logY")

要注意的是! 坐标轴上的值没有log处理, 但是scale发生了变化,注意看第一个图和其他图的坐标轴的scale差别!

再举一个例子

y <- rnorm(100,mean = 1,sd = 0.2)
y
##   [1] 0.8980091 0.6852322 1.2044800 1.2776998 1.2134941 1.5039153 1.0459592
##   [8] 0.9735094 0.9632086 0.7840762 0.9561820 1.0682900 1.2299661 0.7964328
##  [15] 1.2501282 1.2538794 1.1110020 1.1281667 0.9146932 1.1525602 0.9235296
##  [22] 1.2587914 1.1502627 0.8474249 0.8605189 0.7996569 0.7705961 1.0117431
##  [29] 1.0216131 0.7203251 0.9868219 1.0719735 1.0109386 0.9261525 1.0817812
##  [36] 0.8693676 1.0760423 0.9864053 1.1460755 1.0351856 0.8639127 0.8492640
##  [43] 0.7028910 0.9551903 0.9944747 0.9613023 1.0191328 1.3326953 1.0689955
##  [50] 1.0532902 1.0196450 0.9958206 1.1413616 1.1432881 1.0067486 0.8076296
##  [57] 0.9381742 0.9088661 1.0080376 1.1991312 0.9870369 0.6442198 0.9884344
##  [64] 0.7986086 0.9030633 1.2628671 0.9356189 0.8920940 1.3426112 0.6536987
##  [71] 0.9891035 0.8206377 1.2027576 1.1888137 1.1056899 0.4963600 0.7903617
##  [78] 1.2959905 0.9425905 1.4026465 0.8265211 0.7913631 0.9399355 1.3695836
##  [85] 0.7709729 1.1433862 0.8246760 1.2058195 0.8117328 1.2648799 1.0891554
##  [92] 1.5258819 1.0580903 1.0592030 1.0821120 0.8887973 0.8354623 1.2193486
##  [99] 0.9504487 1.1167717
x <- 1:100
x
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
par(mfrow=c(2,2))
plot(x,y,main="no log")
plot(x,y,log="x", main = "logX")
plot(x,y,log="y", main = "logY")
plot(x,y,log = "xy",main = "logX, logY")

从no log图可以看到,数据都是集中在0.6~1.4之间的,对x取log之后发现,大量的数据是集中在1附近的,这个在no log图中就看不太明显,logxlogy同理,而logy就看不太明显

11. type参数-点的类型

type = ?

  • p: 点, 默认
  • l: 线
  • b: 点和连接线
  • o: 点覆盖在线上
  • h: 从点到x周的垂直线
  • s: 阶梯图形
  • n: 不显示数据
x <- 1:20
y <- sample(1:100, 20)
par(mfrow=c(2,2))
plot(x,y,main = "p",type = "p")
plot(x,y,main = "l",type = "l")
plot(x,y,main = "b",type = "b")
plot(x,y,main = "o",type = "o")

par(mfrow=c(2,2))
plot(x,y,main = "h",type = "h")
plot(x,y,main = "s",type = "s")
plot(x,y,main = "n",type = "n")

12. 图的标注xlab ylab main sub

  • xlab: x轴的说明
  • ylab: y轴的说明
  • main: 图的说明
  • sub: 子图的说明
x <- 1:20
y <- sample(1:1000, 20)
par(mfrow=c(1,2))
plot(x,y)
plot(x,y,xlab = "this is X label", ylab = "this is Y label", main = "this is main/title", sub = "this is sub title of the sub plot")

13. points加点函数

  • 在已有图上加点
  • 功能相当于plot(x,y)
x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
par(mfrow=c(2,1))
plot(x,y,type="l")
plot(x,z,type = "l")

上面是绘制成了两个图

x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
plot(x,y,type="l")
points(x,z,type = "l",col="red")

但是有一些z的值超出范围了(仔细看左上角的小红线),咋整呢?看下面

x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
plot(x,y,type="l",ylim=c(0,300))
points(x,z,type = "l",col="red")

x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
plot(x,y,type="l",ylim=c(0,300))
points(x,z,type = "p",col="red")

当然也可以更改type让红线变成点或者其他样式

14. line-加线函数

  • 在已有的图上加线
  • 功能相当于plot(x,y,type=“l”)
x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
plot(x,y,type="l",ylim=c(0,300))
lines(x,z,type = "l",col="red")

x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
plot(x,y,type="l",ylim=c(0,300))
lines(x,z,type = "b",col="red")

x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
plot(x,y,type="l",ylim=c(0,300))
lines(x,z,type = "h",col="red")

x <- c(1:10)
y <- sample(1:200, 10)
z <- sample(150:300, 10)
plot(x,y,type="l",ylim=c(0,300))
lines(x,z,type = "s",col="red")

15. par函数

  • par(mfrow=c(m,n)): 将绘图区分为m行n列,可以画m*n个图
  • par(new=TRUE): 叠加
x <- 1:20
y1 <- sample(1:1000,20)
y2 <- sample(1:1000,20)
plot(x,y1,type = "l")
par(new=TRUE)
plot(x,y2,col="red",type = 'o')

x <- 1:20
y1 <- sample(1:1000,20)
y2 <- sample(1:1000,20)
plot(x,y1,type = "l")
par(new=TRUE)
plot(x,y1,col="red",type = 'p')

所以我们通过这种办法就得到了黑线和红点的结合!

16. 使用par与lwd

接下来画我们的封面图!

  • lwd设置线条宽度
  • 与par结合
x <- 1:20
y1 <- sample(1:1000,20)
plot(x,y1,type = "l",col="red",lwd=10)

plot(x,y1,type="l",col="yellow",lwd=5)

神奇的结果出现了!

plot(x,y1,type = "l",col="red",lwd=10)
par(new=TRUE)
plot(x,y1,type="l",col="yellow",lwd=5)

Beautiful!

17. text函数添加标记

下图的细节 - type=“n”: 不绘点! - text(x,y)

x <- 1:20
y <- sample(1:10000000000, 20)
plot(x,y,type = "n")
text(x,y)

x <- c(1:5)
y <- c(6:10)
plot(x,y,type = "b")
text(x,y)

可以看到有个问题是字和点重合了看不清楚

x <- c(1:5)
y <- c(6:10)
plot(x,y,type = "b")
text(x+0.1,y-0.05)

这样就好看多了!

x <- c(1:5)
y <- c(6:10)
plot(x,y,type = "b")
text(x+0.1,y-0.05, labels = c("A","B","C","D"))

这里的细节是,labels来自定义值,但是看到有五个点我们只定义了4个所以,最后一个触发了R的自动补齐,也就是repeat了这个向量,也就是从A又开始了

18. abline-绘制参考线

  • abline(a,b): 绘制一条y=bx+a的直线
  • abline(h=y): 绘制一条通过所有点的水平直线
  • abline(v=x): 绘制一条通过所有点的垂直直线
plot(1:5,1:5)
abline(h=4,col="blue",lty=3)
abline(h=2)
abline(v=1,col="red",lty=2)
abline(v=4.5,lty=4,col=9)
abline(-3,3, lty=1) # y = 3x-3

细节可以?abline查看帮助文档嗷

19. polygon-给图加上多边形

x <- 1:10
y <- rnorm(x)
plot(x,y,type="l")

x <- 1:10
y <- rnorm(x)
x1 <- c(2,4,4,2)
y1 <- c(0,0,1,1)
plot(x,y,type="l")
polygon(x1,y1,col = 'pink',)

x <- 1:10
y <- rnorm(x)
x1 <- c(2,4,4,2)
y1 <- c(0,0,1,1)
plot(x,y,type="l")
polygon(x1,y1,col = 'yellow', border = 5,lty = 10)

同,了解更多使用?polygon

20. title函数给图形加标题

x <- 1:10
y <- rnorm(x)
x1 <- c(2,4,4,2)
y1 <- c(0,0,1,1)
plot(x,y,type="l")
polygon(x1,y1,col = 'pink',)
title("this is a title")

那么有人要问title和main sub的区别呢? 还是这个图,我画两次

x <- 1:10
y <- rnorm(x)
x1 <- c(2,4,4,2)
y1 <- c(0,0,1,1)par(mfrow=c(1,2))
plot(x,y,type="l",main = "this is main 1",sub = "this is sub1")
polygon(x1,y1,col = 'pink')
plot(x,y,type="l", main = "this is main 2", sub = 'this is sub2')
polygon(x1,y1,col = 'black')par(new=TRUE,mfrow=c(1,1))
title("this is a title")

这样子是不是理解了, 先画了一个含有俩子图的图,俩子图都有各自的main和sub 然后这时候不能直接title,直接title默认是和this is main2这个位置冲叠的, 可以试试! 画到中间的位置思路就是新开一个重叠画布

21. axis-控制坐标轴

axis(side,…) - 1: 底部 - 2: 左侧 - 3: 顶部 - 4: 右侧 axis一般需要和axes=F搭配使用

x <- 1:10
y <- rnorm(x)
par(mfrow=c(2,2))
plot(x,y,type="l",axes = F); axis(1)
plot(x,y,type="l",axes = F); axis(2)
plot(x,y,type="l",axes = F); axis(3)
plot(x,y,type="l",axes = F); axis(4)

22. xlim与ylim-坐标轴的取值范围

x <- c(1:10)
y <- sample(1:100,10)
par(mfrow=c(2,2))
plot(x,y)
plot(x,y,xlim=c(4,8))
plot(x,y,ylim=c(0,50))
plot(x,y,xlim = c(1,5),ylim = c(50,80))

编辑于 07-29

R learning 十八讲 0018-R语言绘图基础相关推荐

  1. R Learnilng 十八讲1-6

    [R learning]-0001-向量-创建向量 1. 直接创建 # 创建等差序列 x1 <- 1:10 x1 ## [1] 1 2 3 4 5 6 7 8 9 10 # 创建一个值的向量 x ...

  2. R Learnilng 十八讲13-17

    0013-选择结构 1. 条件表达式if x <- 24 if(x%%2==0) print("偶数") ## [1] "偶数" x <- 23 i ...

  3. R Learnilng 十八讲7-12

    0007-矩阵-矩阵中元素的访问 矩阵概念 在R语言中,矩阵Matrix是将数据按行和列组织的一种数据对象,相当于二维数组,可以用于描述二维的数据 与向量相似,矩阵的每个元素都拥有相同的数据类型.通常 ...

  4. R count函数_[R learning]-0018-R语言绘图基础, 画了一天,不妨进来看看~

    =================== = 个人练习R语言的笔记 = = 跟我一起来 Coding⑧! = =================== 0018-R语言绘图基础 2020-07-29更新: ...

  5. 小甲鱼Python3学习笔记之第二十八讲(仅记录学习)

    第二十八讲:文件:因为懂你,所以永恒 一.知识点: 0.file对象利用open函数来创建. 1.file文件的打开模式:f = open('文件地址','r/w/x/a等') 'r':只读模式,以只 ...

  6. 趣谈网络协议笔记-二(第十八讲)

    趣谈网络协议笔记-二(第十八讲) DNS协议:网络世界的地址簿 自勉 勿谓言之不预也 -- 向为祖国牺牲的先烈致敬! 正文 DNS用于域名解析,但也不仅仅是用于域名解析,不仅仅是将域名转换成IP. 在 ...

  7. R极简教程-10:R语言绘图基础

    R语言最强大的一点就是画图,那个无比强大的画图系统,再加上各种各样的神级R包,让我写R语言中最喜欢的一点就是写绘图代码. 简单来说,R语言原生的绘图系统已经非常强大了,根本不需要其他东西的辅助,就可以 ...

  8. python字符串操作入门十八讲——合集一

    字符串操作十八讲合集 导读

  9. 前端简单入门第十八讲 使用jQuery实现表格的隔行换色

    还记得之前我使用JavaScript来实现表格的隔行换色效果吗?如果读者初次翻阅本文,可记得看看前端简单入门第十二讲 使用JavaScript完成后台数据展示表格的隔行换色!现在我就来使用jQuery ...

最新文章

  1. 比特币现金可能成为市值第四大最大的加密货币
  2. Python 技术篇-用paramiko库实现winodws本地文件上传至linux服务器实例演示
  3. the largest issue in management
  4. 数据迁移,不停机上线的正确姿势
  5. async await 同步方法调用异步方法死锁
  6. c/c++ 标准库 string
  7. java File_encoding属性
  8. 算法一看就懂之「 递归 」
  9. git常用命令(史上最经典)
  10. python fund_Python fund-my-watcard包_程序模块 - PyPI - Python中文网
  11. iOS 点击tabbarItem的时候根据登录状态判断加载哪个视图控制器
  12. 散粉在哪个步骤用_无限回购的散粉
  13. java 实体类重写排序,对自定义对象进行排序(C++/Java) | 学步园
  14. 关于nginx不能随服务器启动而正常启动的修复脚本
  15. c++ encode 函数_encode 在C++中的用法
  16. python 布尔值取反_如何在Python中获得布尔值的相反(否定)?
  17. 贪心算法(Greedy Algorithm)理论篇
  18. 书生电子合同_部编版一年级语文上册写字表生字组词汇总【有电子版】
  19. 手把手教你批量剪辑视频
  20. 网页抓取/数据抽取/信息提取软件工具包MetaSeeker

热门文章

  1. 超强实操!手把手教学Kinect深度图与RGB摄像头的标定与配准
  2. 因为高校规定博士生毕业必须发核心论文!导师表态:那我拒绝招收研究生
  3. CVPR 2020 | 将深度学习算法应用于移动端最新研究汇总
  4. android edittext限制字节_android EditText输入限制
  5. docker 数据卷 volume
  6. 基于生成式深度学习方法设计潜在2019-nCoV蛋白酶抑制剂
  7. BBC NEWS | AI设计的新药分子首次进入临床试验
  8. CentOS7(64位)安装Jupyter Notebook
  9. 机房布线的最高境界……
  10. 【3月30日直播】新冠病毒全基因组测序——Midnight试剂盒及整体解决方案