Feng Li

School of Statistics and Mathematics

Central University of Finance and Economics

- Graphical facilities are an important and extremely versatile component of the R environment.
- It is possible to use the facilities to display a wide variety of statistical graphs and also to build entirely new types of graph

- High-level plotting functions create a new plot on the graphics device, possibly with axes, labels, titles and so on.
- Low-level plotting functions add more information to an existing plot, such as extra points, lines and labels.
- Interactive graphics functions allow you interactively add information to, or extract information from, an existing plot, using a pointing device such as a mouse.

Including

`plot()`

,`hist()`

...High-level plotting functions are designed to generate a complete plot of the data passed as arguments to the function. Where appropriate, axes, labels and titles are automatically generated (unless you request otherwise.)

High-level plotting commands always start a new plot, erasing the current plot if necessary.

Sometimes the high-level plotting functions do not produce exactly the kind of plot you desire.

In this case, low-level plotting commands can be used to add extra information (such as points, lines or text) to the current plot.

Some of the more useful low-level plotting functions are:

`points()`

,`lines()`

When creating graphics, particularly for presentation or publication purposes, R's defaults do not always produce exactly that which is required. You can, however, customize almost every aspect of the display using graphics parameters.

The

`par()`

function is used to access and modify the list of graphics parameters for the current graphics device.

In [2]:

```
# Simple high level plot function
n <- 100
a = rnorm(100)
plot(a, xlab = "Day", ylab = "Return",
type = "l", col = "red")
```

In [3]:

```
# Add points to existing plot
n <- 100
a = rnorm(n)
plot(a, xlab = "Day", ylab = "Return", type = "l", col = "red")
b = rnorm(n, sd = 0.3)
points(b, col = "blue", type = "l", lty = "dotted", lwd = 2)
```

In [4]:

```
par(mfrow = c(1, 2)) # Using graphical parameter to arrange plots
x <- seq(-5, 5, 1)
plot(x = x, col = "purple", ylab = c(-1, 1), xlab = "hello world!",
main = "My best plot美图秀秀", type = "p", lwd = 1)
points(x, type = "l")
a = rnorm(100)
plot(a, xlab = "Day", ylab = "Return", type = "l", col = "red")
b = rnorm(100, sd = 0.3)
points(b, col = "blue", type = "l", lty = "dotted", lwd = 2)
```

In [5]:

```
# the style of axis labels via las=0,1,2,3
par(las=2)
plot(0,main='change las')
```

In [6]:

```
par(mfrow=c(2,4))
par(bty="o")
plot(0,main="par(bty=\"o\")")
par(bty="l")
plot(0,main="par(bty=\"l\")")
par(bty="7")
plot(0,main="par(bty=\"7\")")
par(bty="n")
plot(0,main="par(bty=\"n\")")
par(bty="c")
plot(0,main="par(bty=\"c\")")
par(bty="u")
plot(0,main="par(bty=\"u\")")
par(bty="]")
plot(0,main="par(bty=\"]\")")
```

In [7]:

```
x = seq(-5, 5, 0.1)
fn = dnorm(x)
ft = dt(x, df = 4)
plot(x, fn, xlab = "X", ylab = "Density", main = "Density plot", col = "Blue", type = "l", lwd = 3)
points(x, ft, col = "Red", type = "l", lwd = 3)
```

In [8]:

```
x <- rt(100, df=4) # give 100 random t with df 12
hist( x, col = "light blue") # make a histgram
```

In [ ]:

```
```

In [9]:

```
set.seed(1) # set the seed
par(mar=c(5,5,5,5)) # set the margin
x1<-sort(rnorm(100,mean=20,sd=5)) # asumption this Interest rate
x2<-x1^3 # this is GDP
plot(x2,axes=FALSE,type="l",col="blue",xlab="",ylab="") # plot GDP
axis(1) # add the axis on bottom
axis(2,col="blue") # add the asis on left
par(new=TRUE) # add new plot on the current graph
plot(x1,,axes=FALSE,type="l",col="red",xlab="",ylab="") # plot Interest rate
axis(4,col="red") # add the axis on right
mtext(c("Time","GDP(mil. $)","Interest rate(%)"),side=c(1,2,4),line=3) # give the three axises labels
title("GDP with interest rate") # give the main title
```