0% found this document useful (0 votes)
1 views

Lab02

The document provides an overview of basic plotting techniques in R, highlighting the flexibility and versatility of R's graphical tools. It covers the use of the plot function, graphical parameters, and customization options for creating effective visualizations. Additionally, it introduces the ggplot2 package, emphasizing its object-oriented approach and the ability to manipulate plots through layers.

Uploaded by

arigno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab02

The document provides an overview of basic plotting techniques in R, highlighting the flexibility and versatility of R's graphical tools. It covers the use of the plot function, graphical parameters, and customization options for creating effective visualizations. Additionally, it introduces the ggplot2 package, emphasizing its object-oriented approach and the ability to manipulate plots through layers.

Uploaded by

arigno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Lab 2.

Basic Plotting

Principles of Statistics

MAT2022, Hanyang University, Spring 2025

1 / 28
R Graphics

R has incredibly flexible plotting tools for data and model


visualization.

Mastering R’s graphical functionality does require practice, but the


fundamental concepts are straightforward.

In this lecture, we will explore an overview of the plot function


and some useful options for controlling the appearance of the final
graph.

2 / 28
Using Plot with Coordinate Vectors

▶ Treat your screen as a blank, two-dimensional canvas

▶ Plot points and lines using 𝑥-and 𝑦-coordinates with points


written as pair: (𝑥 value, 𝑦 value).

▶ plot: takes in two vectors and opens a graphic device where


it displays the result.

Note: The plot function is a one of R’s versatile generic functions.


It works differently for different objects and allows users to define
their own methods for handling objects.

3 / 28
foo <- c(1.1, 2, 3.5, 3.9, 4.2)
bar <- c(2, 2.2, -1.3, 0, 0.2)
plot(foo, bar)
2.0
1.5
1.0
0.5
bar

0.0
−0.5
−1.0

1.0 1.5 2.0 2.5 3.0 3.5 4.0

foo

4 / 28
Graphical Parameters

There are a wide range of graphical parameters that can be


supplied as argument to the plot function.

▶ type: tells R how to plot the supplied coordinates, e.g.,


points or joined by lines or both dots and lines

▶ main, xlab, ylab: options to include plot title, the


horizontal axis label, and the vertical axis label

▶ col: colors to use for plotting points and lines

5 / 28
▶ pch (point character): selects which character to use for
plotting individual points

▶ cex (character expansion): controls the size of plotted point


characters

▶ lty (line type): specifies the type of line to use to connect


the points, e.g., solid, dotted, or dashed

▶ lwd (line width): controls the thickness of plotted line

▶ xlim, ylim: provides limits for the horizontal range and


vertical range of the plotting region

6 / 28
Automatic Plot Types (type)
▶ type="p" (points only), "l" (lines only), "b" (both points
and lines), "o" (overplotting the points with lines), "n" (no
points or lines plotted)

par(mfrow=c(1,2))
plot(foo,bar,type="l");plot(foo,bar,type="b")
2.0

2.0
1.5

1.5
1.0

1.0
0.5

0.5
bar

bar
0.0

0.0
−0.5

−0.5
−1.0

−1.0

1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0

foo foo

7 / 28
Title and Axis Labels (main, xlab, ylab)

par(mfrow=c(1,2))
plot(foo,bar,type="o",main="My plot",
xlab="x axis label", ylab="location y")
plot(foo,bar,type="b",main="My plot\n title on two lines",
xlab="", ylab="")
My plot
My plot
title on two lines
2.0

2.0
1.5

1.5
1.0

1.0
location y

0.5

0.5
0.0

0.0
−0.5

−0.5
−1.0

−1.0
1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0

x axis label

8 / 28
Color (col; can make data much clearer)

par(mfrow=c(1,2))
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",col=2)
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",col="seagreen4")

My plot My plot
2.0

2.0
1.5

1.5
1.0

1.0
0.5

0.5
0.0

0.0
−0.5

−0.5
−1.0

−1.0

1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0

9 / 28
Note: You can set colors with the col parameter in a number of
ways. There are eight possible integer values. Also, you can specify
colors using RGB (red, green, and blue) levels and by creating your
own palettes.

10 / 28
Line and Point Appearances (pch, lty, cex, lwd)

par(mfrow=c(1,2))
plot(foo,bar,type="b",main="My plot",xlab="",
ylab="",col=4,pch=8,lty=2,cex=2.3,lwd=3.3)
plot(foo,bar,type="b",main="My plot",xlab="",
ylab="",col=6,pch=15,lty=3,cex=0.7,lwd=2)

My plot My plot
2.0

2.0
1.5

1.5
1.0

1.0
0.5

0.5
0.0

0.0
−0.5

−0.5
−1.0

−1.0

1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.0 1.5 2.0 2.5 3.0 3.5 4.0

11 / 28
Plotting Region Limits (xlim, ylim)

par(mfrow=c(1,2))
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",
col=4,pch=8,lty=2,cex=2.3,lwd=3.3,xlim=c(-10,5),ylim=c(-3,3))
plot(foo,bar,type="b",main="My plot",xlab="",ylab="",
col=6,pch=15,lty=3,cex=0.7,lwd=2,xlim=c(3,5),ylim=c(-0.5,0.2))

My plot My plot

0.2
3

0.1
2

0.0
1

−0.1
0

−0.2
−1

−0.3
−2

−0.4
−0.5
−3

−10 −5 0 5 3.0 3.5 4.0 4.5 5.0

12 / 28
Adding Points, Lines, and Text to an Existing Plot

Each call to plot will refresh the active graphics device for a new
plotting region.

Here are some useful, ready-to-use functions that will add a plot
without refreshing or clearing the window:
▶ points (add points)
▶ lines, abline, segments (add lines)
▶ text (writes text), arrows (add arrows)
▶ legend (adds a legend)

13 / 28
x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
plot(x,y,type="n",main="")
abline(h=c(-5,5),col="red",lty=2,lwd=2)
segments(x0=c(5,15),y0=c(-5,-5),x1=c(5,15),y1=c(5,5),
col="red",lty=3,lwd=2)
15
10
5
y

0
−5
−10
−15

5 10 15 20

14 / 28
plot(x,y,type="n",main="")
points(x[y>=5],y[y>=5],pch=4,col="darkmagenta",cex=2)
points(x[y<=-5],y[y<=-5],pch=3,col="darkgreen",cex=2)
points(x[(x>=5&x<=15)&(y>-5&y<5)],y[(x>=5&x<=15)&(y>-5&y<5)],
pch=19,col="blue")
points(x[(x<5|x>15)&(y>-5&y<5)],y[(x<5|x>15)&(y>-5&y<5)])
15
10
5
y

0
−5
−10
−15

5 10 15 20

15 / 28
plot(x,y,type="n",main="")
lines(x,y,lty=4)
arrows(x0=8,y0=14,x1=11,y1=2.5)
text(x=8,y=15,labels="sweet spot")
15

sweet spot
10
5
y

0
−5
−10
−15

5 10 15 20

16 / 28
plot(x,y,type="b",main="")
legend("bottomleft",
legend=c("overall process","sweet","standard",
"too big","too small","sweet y range","sweet x range"),
pch=c(NA,19,1,4,3,NA,NA),lty=c(4,NA,NA,NA,NA,2,3),
col=c("black","blue","black","darkmagenta","darkgreen","red","red"),
lwd=c(1,NA,NA,NA,NA,2,2),pt.cex=c(NA,1,1,2,2,NA,NA))
15
10
5
y

0
−5

overall process
sweet
−10

standard
too big
too small
sweet y range
−15

sweet x range

5 10 15 20

17 / 28
My final plot

15
sweet spot
10
5
y

0
−5

overall process
sweet
−10

standard
too big
too small
sweet y range
−15

sweet x range

5 10 15 20

18 / 28
The ggplot2 Package

Another important suite of graphical tools: ggplot2, a prominent


contributed package by Hadley Wickham (2009)

ggplot2 offers particularly powerful alternatives to the standard


plotting procedures in R.

gg stands for grammar of graphics.

ggplot2 standardizes the production of different plot and graph


types and let you build plots by defining and manipulating layers.

19 / 28
A Quick Plot with qplot
▶ You must install ggplot2 package.
▶ ggplot2 plots are stored as objects, meaning that they have
an underlying, static representation until you change the
object.

Note: The object-oriented nature of ggplot2 graphics means


tweaking a plot or experimenting with different visual features no
longer requires you to return every plotting command each time
you change something.

20 / 28
foo <- c(1.1, 2, 3.5, 3.9, 4.2)
bar <- c(2, 2.2, -1.3, 0, 0.2)
qplot(foo, bar, main="My qplot",
xlab="x axis label", ylab="location y")
My qplot

1
location y

−1

1 2 3 4
x axis label

21 / 28
Setting Appearance Constants with Geometric Modifiers
▶ To customize ggplot2 graphic, you alter the object itself.
▶ First store the qplot object you created earlier and then use
geom_point and geom_line with different style.

qplot(foo,bar,geom="blank") + geom_point() + geom_line()


2

1
bar

−1

1 2 3 4
foo

22 / 28
qplot(foo,bar,geom="blank") +
geom_point(size=3,shape=6,color="blue") +
geom_line(color="red",linetype=2)

1
bar

−1

1 2 3 4
foo

23 / 28
myqplot <- qplot(foo,bar,geom="blank") +
geom_line(color="red",linetype=2)
a <- myqplot + geom_point(size=3,shape=3,color="blue")
b <- myqplot + geom_point(size=4,shape=7,color="green")
grid.arrange(a,b, nrow=1, ncol=2) # gridExtra package

2 2

1 1
bar

bar
0 0

−1 −1

1 2 3 4 1 2 3 4
foo foo

24 / 28
Aesthetic Mapping with Geoms
▶ Geoms and ggplot2 provide efficient, automated ways to
apply different styles to different subsets of a plot.
▶ ggplot2 apply particular styles to different categories using a
factor object.
▶ The factor that holds these categories is called a variable,
which ggplot2 can map to aesthetic values.

Note: The above gets rid of much of the effort that goes into
isolating subsets of data and plotting them separately using base R
graphics.

25 / 28
▶ Based on x and y values, define several categories.

x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
ptype <- rep(NA,length(x=x))
ptype[y>=5] <- "too_big"
ptype[y<=-5] <- "too_small"
ptype[(x>=5&x<=15)&(y>-5&y<5)] <- "sweet"
ptype[(x<5|x>15)&(y>-5&y<5)] <- "standard"
ptype <- factor(x=ptype)
ptype
## [1] standard standard standard standard sweet sweet too_
## [8] too_big sweet too_small too_small too_small sweet too_
## [15] too_big standard too_small too_small standard too_big
## Levels: standard sweet too_big too_small

26 / 28
a <- qplot(x,y,color=ptype,shape=ptype)
b <- qplot(x,y,color=ptype,shape=ptype) + geom_point(size=4) +
geom_line(mapping=aes(group=1),color="black",lty=2) +
geom_hline(mapping=aes(yintercept=c(-5,5)),color="red") +
geom_segment(mapping=aes(x=5,y=-5,xend=5,yend=5),color="red",lty=3) +
geom_segment(mapping=aes(x=15,y=-5,xend=15,yend=5),color="red",lty=3)
grid.arrange(a,b, nrow=1, ncol=2) # gridExtra package

10 10

ptype ptype
standard standard
sweet sweet
y

y
0 0
too_big too_big
too_small too_small

−10 −10

5 10 15 20 5 10 15 20
x x

27 / 28
Reference

▶ Davies, T. M. The Book of R. No Starch Press. Chapter 7.

28 / 28

You might also like