1. Base Function vs ggplot2

We look at simple plotting with base functions (without using any packages). R has very good visualization package called ggplot2. Tutorial for that is on another page.

2. Plotting

Orig <- read.csv("https://nmimoto.github.io/datasets/plants.csv")
                # load the dataset directly from website.
dim(Orig)       # n=32. Ther are 3 columns
## [1] 32  3
names(Orig)     # look up the names of columns
## [1] "Cost"   "MWatts" "Date"
head(Orig)
##     Cost MWatts  Date
## 1 345.39    514 67.92
## 2 460.05    687 68.58
## 3 452.99   1065 67.33
## 4 443.22   1065 67.33
## 5 652.32   1065 68.00
## 6 642.23   1065 68.00
plot(Orig$Cost)       # scatter plot of Cost column

plot(Orig$MWatts)     # scatter plot of MWatts column

plot(Orig$MWatts, Orig$Cost)    #  MWatts vs Cost

Note that MWatts is on x-axis.

2. Plotting options

Let’s load lynx.csv from my website.

Orig <- read.csv("https://nmimoto.github.io/datasets/lynx.csv")
                # load the dataset directly from website.
head(Orig)      # the data only has one column called "Lynx"
##   Lynx
## 1  269
## 2  321
## 3  585
## 4  871
## 5 1475
## 6 2821
dim(Orig)       # n = 114
## [1] 114   1
X = Orig$Lynx   # Call "Lynx" column of "Orig" data as "X" for convenience.


Plot types

plot(X)               # scatter plot is the default for plot()
plot(X, type="p")     # type="p" gives the same scatter plot

plot(X, type="l")     # line plot

plot(X, type="o")     # both


Change line width

plot(X, type="l", lwd=2)      # thick line

plot(X, type="l", lwd=4)      # thicker line


Change color

plot(X, type="l", lwd=2, col="red")

plot(X, type="l", lwd=2, col="green")


Zooming in/out

plot(X, type="l", xlim=c(0,60), ylim=c(0,4000))     # Zooming In

plot(X, type="l", xlim=c(0,200), ylim=c(0,10000))   # Zooming Out


Put title and Label

plot(X, type="l",
     xlab="Year", ylab="Number of Lynx Caught",
     main="Lynx.csv")


How to look up more options

Googling something like “R how to change line type” is the best bet. You can also look at R documentation by command:

?plot



3. Draw lines to Plot

You can add horizontal and vertical lines to existing plot.

Orig <- read.csv("https://nmimoto.github.io/datasets/lynx.csv")
X = Orig$Lynx

plot(X, type="l")
M = mean(X)
abline(h=M, col="red")    # h for horizontal
abline(v=30, col="blue")  # v for vertical

You can also draw line with intercept and slope.

plot(X, type="l")

abline(a=1000, b=5, col="red")    # a=intercept. b=slope



4. Overlaying Two Plots

Orig2 <- read.csv("https://nmimoto.github.io/datasets/wine.csv")
                   # load in wine.csv from webpage
head(Orig2)        # has 1 column called "wine"
##   wine
## 1  464
## 2  675
## 3  703
## 4  887
## 5 1139
## 6 1077
dim(Orig2)         # n = 142
## [1] 142   1
Y = Orig2$wine     # Call "wine" column as "Y" for convenience.


Single plots

Note that X was defined as Orig$Lynx above.

plot(X, type="l")

plot(Y, type="l")


Overlay two plots

plot(X, type="l")      # plot X on its own
lines(Y, col="red")    # overlay Y

Note that lines() uses type=“l” as default. You can change it by usint type= command if you need to.

plot(Y, type="l", col="red")     # plot Y on its own
lines(X)                         # overlay X

Note that lines() use uses the plot that is already drawn by plot(). So which one to plot first does matter. You can specify the range of the plot in plot().

plot(X, type="l", xlim=c(0,200), ylim=c(0, 8000))    # plot X with more range
lines(Y, col="red")                                  # overlay Y



5. Plotting in Panels

You can plot more than 1 plot on a same page. Here’s horizontal stack.

layout(matrix(1:2, 1, 2))    # set up layout as (2x1) matrix
plot(X, type="l")
plot(Y, type="l")

layout(1)                    # back to 1 plot layout

Vertical stack.

layout(matrix(1:2, 2, 1))    # set up layout as (2x1) matrix
plot(X, type="l")
plot(Y, type="l")

layout(1)                    # back to 1 plot layout

As 2 by 2 panel.

layout(matrix(1:4, 2, 2))    # set up layout as (2x2) matrix
plot(X, type="l")
plot(Y, type="l")
plot(X, type="l")
plot(Y, type="l")

layout(1)                    # back to 1 plot layout



Code Only

### 1. Plotting
Orig <- read.csv("https://nmimoto.github.io/datasets/plants.csv")
                # load the dataset directly from website.
dim(Orig)       # n=32. Ther are 3 columns
names(Orig)     # look up the names of columns
head(Orig)

plot(Orig$Cost)       # scatter plot of Cost column
plot(Orig$MWatts)     # scatter plot of MWatts column
plot(Orig$MWatts, Orig$Cost)    #  MWatts vs Cost



### 2. Plotting options
# load **lynx.csv** from my website.
Orig <- read.csv("https://nmimoto.github.io/datasets/lynx.csv")
                # load the dataset directly from website.
head(Orig)      # the data only has one column called "Lynx"
dim(Orig)       # n = 114
X = Orig$Lynx   # Call "Lynx" column of "Orig" data as "X" for convenience.


# Plot types
plot(X)               # scatter plot is the default for plot()
plot(X, type="p")     # type="p" gives the same scatter plot
plot(X, type="l")     # line plot
plot(X, type="o")     # both

# Change line width
plot(X, type="l", lwd=2)      # thick line
plot(X, type="l", lwd=4)      # thicker line

# Change color
plot(X, type="l", lwd=2, col="red")
plot(X, type="l", lwd=2, col="green")


# Zooming in/out
plot(X, type="l", xlim=c(0,60), ylim=c(0,4000))     # Zooming In
plot(X, type="l", xlim=c(0,200), ylim=c(0,10000))   # Zooming Out


# Put title and Label
plot(X, type="l",
     xlab="Year", ylab="Number of Lynx Caught",
     main="Lynx.csv")


# How to look up more options
# Googling something like "R how to change line type" is the best bet.
# You can also look at R documentation by command:
?plot


### 3. Draw lines to Plot

# You can add horizontal and vertical lines to existing plot.
Orig <- read.csv("https://nmimoto.github.io/datasets/lynx.csv")
X = Orig$Lynx

plot(X, type="l")
M = mean(X)
abline(h=M, col="red")    # h for horizontal
abline(v=30, col="blue")  # v for vertical


# You can also draw line with intercept and slope.
plot(X, type="l")

abline(a=1000, b=5, col="red")    # a=intercept. b=slope

### 4. Overlaying Two Plots
Orig2 <- read.csv("https://nmimoto.github.io/datasets/wine.csv")
                   # load in wine.csv from webpage
head(Orig2)        # has 1 column called "wine"
dim(Orig2)         # n = 142

Y = Orig2$wine     # Call "wine" column as "Y" for convenience.

# Single plots
# Note X was defined as Orig$Lynx above.
plot(X, type="l")
plot(Y, type="l")


# Overlay two plots
plot(X, type="l")      # plot X on its own
lines(Y, col="red")    # overlay Y

plot(Y, type="l", col="red")     # plot Y on its own
lines(X)                         # overlay X

# lines() uses xllim and yllim that is already there
plot(X, type="l", xlim=c(0,200), ylim=c(0, 8000))    # plot X with more range
lines(Y, col="red")                                  # overlay Y


### 5. Plotting in Panels
# Here's horizontal stack.
layout(matrix(1:2, 1, 2))    # set up layout as (2x1) matrix
plot(X, type="l")
plot(Y, type="l")
layout(1)                    # back to 1 plot layout


#  Vertical stack.
layout(matrix(1:2, 2, 1))    # set up layout as (2x1) matrix
plot(X, type="l")
plot(Y, type="l")
layout(1)                    # back to 1 plot layout


#  As 2 by 2 panel.
layout(matrix(1:4, 2, 2))    # set up layout as (2x2) matrix
plot(X, type="l")
plot(Y, type="l")
plot(X, type="l")
plot(Y, type="l")
layout(1)                    # back to 1 plot layout