R学习5-Graphics

Made by Mike_Zhang


所有文章:
R学习1-基础
R学习2-I/O
R学习3-Vector List Matrix
R学习4-Data Frame
R学习5-Graphics


1 Introduction

plot function:

1
> plot(x)

1.1 High-level graphics function

start a new graph, initialize graph window.

plot(): Plot function;

boxplot: create box plot;

hist(): create histogram;

qqnorm(): Quantile-quantile(Q-Q) plot;

curve(): function graph.

1.2 Low-level graphics function

cannot start a new graph, add something to the graph, based on the High-level graphics function.

points(): add points;

lines(): add lines;

abline: add a straight line;

segments(): add line segments;

polygon(): add a closed polygon;

text() add text.

Call High-level graphics function first, then call the low one.


2 Title & Label

Parameter:

main: set title;
xlab: set x-axis label;
ylab: set y-axis label.

1
> plot(dfrm, main='This is title',xlab='x-lab',ylab='y-lab')

OR

1
2
> plot(dfrm,ann=False) # ignore the annotation first
> title(main='This is title',xlab='x-lab',ylab='y-lab') # use title() to set


3 Grid

  1. set type="n" in plot() function to hide the graph;
  2. use grid() function to show the grid;
  3. use low-level functions to draw the graph again.

For example,

1
2
3
> plot(dfrm, main='This is title',xlab='x-lab',ylab='y-lab',type="n")
> grid()
> points(dfrm)

grid()

4 Legend

legend function:

1
2
3
4
legend(x, y, labels, pch=c("type1","type2",...)) # Legend for points
legend(x, y, labels, lty=c("type1","type2",...)) # Legend for lines based on line type
legend(x, y, labels, lwd=c("type1","type2",...)) # Legend for lines based on line width
legend(x, y, labels, col=c("type1","type2",...)) # Legend for colors

x, y: coordinates for legend box (top-left corner);
label: vector of characters of legend;
last argument: based on which species.

For example,

1
2
> plot(dfrm, main='This is title',xlab='x-lab',ylab='y-lab')
> legend(1,50,'The Point',pch=1)

legend()

5 Scatter Plot

1
> plot(x,y)

x,y: two parallel vectors.

1
> plot(dfrm)

dfrm: a two column data frame.

For example,

1
2
3
4
> v1 <- c(1,2,3,4,5,6,7,8,9,10)
> v2 <- c(12,15,16,5,30,20,50,60,10,25)
> dfrm <- data.frame('xVal'=v1,'yVal'=v2)
> plot(dfrm)

plot(dfrm)

5.1 Multiple-Group Scatter Plot

Set pch parameter in plot() function:

1
plot(x,y,pch=name)

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> dfrm
name xVal yVal
1 a 1 12
2 a 2 15
3 a 3 16
4 a 4 5
5 a 5 30
6 b 6 20
7 b 7 50
8 b 8 60
9 b 9 10
10 b 10 25

> with(dfrm,plot(xVal,yVal,pch=name))
>
> with(dfrm,legend(1,50,c('for a','for b'),pch=name[5:6]))

Multiple-Group

5.2 Regression Line of Scatter Plot

Use lm() and abline() function:

1
2
> m <- lm(v2~v1)
> abline(m)

For example,

1
2
3
4
> with(dfrm,plot(xVal,yVal,pch=name))
> with(dfrm,legend(1,50,c('for a','for b'),pch=name[5:6]))
> m <- lm(v2~v1)
> abline(m)

Regression Line

6 Bar Chart

barplot() function:

1
barplot(c(height1,height2,...))

For example,

1
2
> barplot(v2, main='Bar Chart',names.arg=c(1,2,3,4,5,6,7,8,9,10),
xlab='name',ylab='yVal') # names.arg for the argument of x-axis

Bar Chart

6.1 Coloring

col parameter of barplot() function:

1
> barplot(v,col='red')

For example,

1
2
barplot(v2, main='Bar Chart',names.arg=c(1,2,3,4,5,6,7,8,9,10),
xlab='name',ylab='yVal',col=('red'))

Coloring

Colour based on the data of graph:

1
2
3
4
> rel.hts <- (v2 - min(v2)) / (max(v2) - min(v2))
> grays <- gray(1 - rel.hts)
> barplot(v2, main='Bar Chart',names.arg=c(1,2,3,4,5,6,7,8,9,10),
xlab='name',ylab='yVal',col=grays)

Coloring

7 Line

Use plot() function:

1
> plot(v1,v2,type="l")

Line

7.1 Line Config

Appearance of the line:

1
2
3
4
5
6
7
lty="solid" or lty=1 (default)
lty="dashed" or lty=2
lty="dotted" or lty=3
lty="dotdash" or lty=4
lty="longdash" or lty=5
lty="twodash" or lty=6
lty="blank" or lty=0 (inhibits drawing)

Width of the line:

1
> plot(x, y, type = "l", lwd=3) # default = 1

Colour of the line:

1
> plot(x, y, type = "l", col="red") # default = black

7.2 Multiple Line

Use xlim and ylim to set the interval of graph first.

1
2
3
4
> xLim <- range(c(x1,x2))
> yLim <- range(c(y1,y2))
> plot(x1, y1, type="l", xlim = xLim, ylim = yLim)
> lines(x2, y2, type="l") # then use low-level function to add another line

7.3 Vertical & Horizontal Line

1
2
> abline(v=a) # vertical line at x = a
> abline(h=b) # horizontal line at h = b

For example:

1
2
3
> plot(v1,v2,type="l")
> abline(v=3)
> abline(h=40)

Vertical & Horizontal Line

8 Box Plot

1
> boxplot(v2) # v2 is a numeric vector

For example,

1
2
3
> v2
[1] 12 15 16 5 30 20 50 60 10 25
> boxplot(v2)

Box Plot

9 Histogram

1
> hist(v2) # v2 is a numeric vector

For example:


Histogram

9.1 Density Estimated Line

Use the density() function:

1
2
> hist(v2,prob=T)
> lines(density(v2))

Density Estimated Line

9.2 Discrete Histogram

Use plot() function and set type parameter.

1
2
> v <- c(1,1,2,2,2,3,3,3,3,3,4,4,5)
> plot(table(v),type="h",lwd=5)

Discrete Histogram

10 Function Graphics

Use curve() function:

1
> curve(sin,-5,5)

sin Graphics

OR

1
2
3
4
> f <- function(x) 1/x
> curve(f,-10,10)
> abline(v=0)
> abline(h=0)

Function Graphics

11 Plot Display

Set mfrow parameter to divide the window into a Matrix,
then call plot() function to fill the window.

1
2
3
4
5
> par(mfrow=(c(2,2)))
> plot(table(v),type="h",lwd=5)
> plot(table(v2),type="h",lwd=5)
> boxplot(v2)
> plot(v1,v2,type="l")

Plot Display

mfrow parameter fills window row by row, while mfcol fills column by column.


参考

P. Teetor, R Cookbook. Sebastopol: O’Reilly Media, Incorporated, 2011.


写在最后

R语言相关的知识会继续学习,继续更新.
最后,希望大家一起交流,分享,指出问题,谢谢!


原创文章,转载请标明出处
Made by Mike_Zhang




感谢你的支持

R学习5-Graphics
https://ultrafish.io/post/R-learning-5/
Author
Mike_Zhang
Posted on
December 24, 2021
Licensed under