Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

《R Graphic Cookbook》第一章学习笔记 #9

Open
JackieMium opened this issue Mar 8, 2018 · 0 comments
Open

《R Graphic Cookbook》第一章学习笔记 #9

JackieMium opened this issue Mar 8, 2018 · 0 comments
Labels
R R related 做图 plot 基础 很基础的东西
Milestone

Comments

@JackieMium
Copy link
Owner

2017-05-25

书中涉及的示例数据可以在 GitHub 上找到。

散点图

使用自带的 cars 数据。数据分两列,分别为速度和路程。

Let's use one of R's inbuilt datasets called cars to look at the relationship between the speed of cars and the distances taken to stop (recorded in the 1920s).

plot(cars$dist~cars$speed)

得到基本的关于汽车行驶路程和行驶速度的散点图:

1

稍微修饰下图片,添加一些参数:

plot(cars$dist~cars$speed, # y~x
     main="Relationship between car distance & speed", # Plot Title
     type='p', ##Specify type of plot as p for point(default option)
     xlab="Speed (miles per hour)", #X axis title
     ylab="Distance travelled (miles)", #Y axis title
     xlim=c(0,30), #Set x axis limits from 0 to 30
     ylim=c(0,140), #Set y axis limits from 0 to 140
     xaxs="i", #Set x axis style as internal
     yaxs="i", #Set y axis style as internal
     col="red", #Set the color of plotting symbol to red
     pch=19) #Set the plotting symbol to filled dots)

然后图片变为:

2

pch 参数

pch是 plotting character 的缩写。pch 缺省下设定数据显示为点状。pch 符号可以使用0 : 25来表示26 个标识。21 : 25这几个符号还可以使用bg="颜色" 参数进行不同的颜色填充。颜色参数 col 则可以用于设置1:25所表示符号的颜色。作一张图看看 pch 都有哪些:

par(mfrow = c(5, 5))
for(i in 1:5){
  if(i < 5){
    for(j in 1:5){plot(1, pch = (i-1)*5 + j, cex = 2, col = 'black')}}
  else
    for(j in 1:5){plot(1, pch = (i-1)*5 + j, cex = 2, col = 'darkgreen', bg = 'red')}
}

4

折线图

示例数据 dailysales.csv。数据有两列,第 1 列为日期,第 2 列为销售量。

直接上图:

plot(sales$units~as.Date(sales$date,"%d/%m/%y"),
type="l", #Specify type of plot as l for line
main="Unit Sales in the month of January 2010",
xlab="Date",
ylab="Number of units sold",
col="blue")

生成一个显式每日销售量的折线图:

3

与散点图相比,基本上就是添加了 type="l" 这个参数,l 代表 line,即画线图,type 默认为 p 即point 点图,所以在画散点图的时候不指定也可以画图,画线图就需要显式的指定这个参数为 l

还可以在图上用另一个数据添加一条线:

sales$units2 <- sales$units -  1000
lines(sales$units2~as.Date(sales$date,"%d/%m/%y"), col="red")

3 1

条形图

使用示例数据 citysales.csv 。数据分 4 列, 第一列为城市名,后三列分别产品 A、B、C的销售量。

画条形图展示产品 A 在不同城市的销量:

barplot(sales$ProductA,
        names.arg= sales$City,
        col="black")

得到如下条形图:

5

默认画出的条形图是竖直的,也可以通过参数 horiz = TRUE 来画水平的条形图:

barplot(sales$ProductA, names.arg = sales$City, horiz = TRUE, col = 'black')

6

The labels for the bars are specified by the names.arg argument, but we use this argument only when plotting single bars. In the example with sales figures for multiple products, we didn't specify names.arg . R automatically used the product names as the labels and we had to instead specify the city names as the legend.

参数 names.arg 用来指定数据条的名称,但只有在画单个数据的条形图才需要这个参数, 多个数据的条形图不需要指定这个参数。

多个数据组的条形图

我们经常对多个组别之间多个数据画条形图,例如一共三组数据(比如某个指标 0h、24h 和 48h 分别测得的数值),每组数据包含三个指标(比如红细胞计数,血小板和血红蛋白)。这个时候就要用到多个数据组的条形图了,依然延续上个例子的数据,不过现在每个城市的销售产品都有 A、B、C 三种:

barplot(as.matrix(sales[,2:4]), legend = sales$City, col = heat.colors(5), beside = TRUE, border = 'white')

得到的图形图为:

5 1

The beside argument is used to specify whether we want the bars in a group of data to be stacked or adjacent to each other. By default, beside is set to FALSE , which produces a stacked bar graph. To make the bars adjacent, we set beside to TRUE .

直方图和密度图

一个包含 1000 个数据的正态分布的直方图的例子:

hist(rnorm(1000), col = heat.colors(5), border = 'white')

7

As you may have noticed in the preceding examples, the default setting for histograms is to display the frequency or number of occurrences of values in a particular range on the Y axis. We can also display probabilities instead of frequencies by setting the prob (for probability) argument to TRUE or the freq (for frequency) argument to FALSE .

直方图默认以数据的频数作图,如要以频率作图,可以指定参数 freq = FALSE 或者 prob = TRUE

如:

hist(rnorm(1000),col = heat.colors(5), border = 'white', freq = FALSE)
hist(rnorm(1000),col = heat.colors(5), border = 'white', probability = TRUE)

都会得到:

7 1

画密度分布图则需要额外使用 density() 函数:

plot(density(rnorm(1000)), col = 'red')

7 2


箱式图

示例数据 metals.csv。数据为伦敦不同地点空气中金属离子含量。数据第一列为不同地点,从第二列起为不同金属离子浓度值。

画箱式图展示金属离子含量:R

boxplot(metals[,2:ncol(metals)],
        xlab="Metals",
        ylab="Atmospheric Concentration in ng per cubic metre",
        main="Atmospheric Metal Concentrations in London")

8

The dark line inside the box for each metal represents the median of values for that metal. The bottom and top edges of the box represent the first and third quartiles respectively. Thus, the length of the box is equal to the interquartile range (IQR, difference between first and third quartiles). The maximum length of a whisker is a multiple of the IQR (default multiplier is approximately 1.5). The ends of the whiskers are at data points closest to the maximum length of the whisker.
All the points lying beyond these whiskers are considered outliers.

还可以通过对数据进行分组然后画箱式图,例如显示铜离子在不同地点的含量情况:

boxplot(copper$Cu ~ copper$Source, 
        xlab="Measurement Site",
        ylab="Atmospheric Concentration of Copper in ng per cubic metre",
        main="Atmospheric Copper Concentrations in London")

8 1

@JackieMium JackieMium added R R related 基础 很基础的东西 labels Mar 8, 2018
@JackieMium JackieMium changed the title R Graphic Cookbook》第一章学习笔记 《R Graphic Cookbook》第一章学习笔记 Mar 9, 2018
@JackieMium JackieMium added the 做图 plot label Mar 9, 2018
@JackieMium JackieMium added this to the Migration milestone Mar 9, 2018
@JackieMium JackieMium added this to R in 博文分类 Jun 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
R R related 做图 plot 基础 很基础的东西
Projects
Development

No branches or pull requests

1 participant