You are on page 1of 18

OVERVIEW

• One of the best things about R is the ability


to create publication-quality graphs
Introduction to R: • To get an idea you can browse through
Data Manipulation and Statistical > demo(graphics)
Analysis > demo(persp)

• Understanding the graphics will facilitate


BASIC R GRAPHICS understanding R programming in general
Leilani A. Nora • How a plotting function deals with data often
Assistant Scientist depends on the data type (e.g. matrix,
factor, vector) given to it.

OVERVIEW PLOTTING COMMANDS

• Types of basic graphics that will be • High-level plotting functions:


discussed are: create a new plot on the graphics device
- Histogram
- Boxplot • Low-level plotting functions:
- Scatterplot add more information on existing plot
(extra points, lines, and labels)
- Line Graph
- Bar charts / Bar Graph • Interactive graphics functions:
- Error Bars allow you interactively add information to
an existing plot using a pointing device
such as mouse.
HIGH-LEVEL PLOTTING FUNCTIONS LOW-LEVEL PLOTTING FUNCTIONS
• Produces new graph on the graphics device • Add additional information to an existing
• Selected high level plotting functions graph
• Selected low-level plotting functions

GRAPHICAL PARAMETERS: par() GRAPHICAL WINDOW


• When a graphical function is executed, R
• The graphs can be modified with the function par() opens the graphical window and displays the
> par(bg=“
par(bg=“yellow”
yellow”) graph

# bg – all subsequent plots will have a yellow • To generate a graphic in a new window, use
background the command:
- x11() in Linux
• There are 73 graphical parameters, some of them - windows() in Windows
have very similar functions.
• The exhaustive list of these parameters can be read
with ?par
• Some par elements can be modified from within high
and low level plotting functions
SUMMARY OF GRAPHICS AND
DATA FILE

GRAPHICS DATA FILE


Histogram gdata1.csv
data1.csv HISTOGRAM
Boxplot gdata1.csv
data1.csv
Scatterplot gdata2.csv
Linegraph gdata2.csv
Bar Graph gdata3.csv and gdata
gdata2.csv
2.csv
(matrix)
Error Bars gdata1.csv
data1.csv

DATAFRAME :gdata1.csv DATA FRAME: gdata1


• Consider the Read data file gdata1.csv
gdata1.csv
data from Split > gdata1 <- read.table(“
read.table(“gdata1.csv",
gdata1.csv",
plot design with header=T, sep=",")
Nitrogen as MP > gdata1
and Variety as Nitrogen Variety Rep Yield
SP, replicated 4 1 0 C4-63 1 3464
times. 2 60 C4-63 1 4768
3 90 C4-63 1 6224
4 120 C4-63 1 5792
. . .
47 90 Peta 3 4146
48 120 Peta 3 3638
HISTOGRAM : hist() DATAFRAME gdata : hist()
• Ex1. To obtain histogram for Y
• Histogram is an important tool for
exploratory data analysis that provides a > hist(gdata1
hist(gdata1$Yield)
graphical summary of the shape or data’s • Displays the
distribution. histogram of Yield
at Graphics
• The generic function hist() computes a window
histogram of the given data values.

• Details of hist() function.


> hist(x, …)
# x – a vector of values

GRAPHICAL par EXAMPLE : hist()


• Ex2. Modifying Histogram
> hist(gdata1$Yield, main='Histogram
of Yield', col=‘
col=‘yellow2',
border=‘
border=‘blue', xlab=“
xlab=“Y Class”
Class”,
ylab="Frequency",
xlim=c(0,10000),ylim=c(0, 11))

# main - main title of the histogram


# col - color of filled bars
# border - foreground colour of bars
# ylab – y axis label
# xlim / ylim – x and y axis limit
boxplot()
• Produce a box-and-whisker plot(s) of the given
(grouped) values.
BOXPLOT • Details of boxplot() function.
> boxplot(formula, data=NULL, …,
na.action=NULL)
formula – a formula, such as y~grp
where : y - is a numeric vector of data
values to be split into groups
according to the grouping variable,
grp (factor)

DATAFRAME gdata1 : boxplot()


• Ex2. To obtain boxplot of Y by Site
• Ex1. To obtain boxplot of Y > boxplot(gdata1$Yield~
> boxplot(gdata1$Yield) gdata$Variety)
plot()

• plot(x,y) or plot(y~x)
- x and y are the variables to be used for
SCATTERPLOT the x and y axes, respectively
- x and y are both numeric variables

DATAFRAME :gdata2.csv DATA FRAME: gdata2

• Nitrogen x Variety Summary of Means Read data file gdata2.csv


gdata2.csv
> gdata2 <- read.table(“
read.table(“gdata2.csv",
gdata2.csv",
header=T, sep=",")
> gdata2
Nitrogen C463 IR5 IR8 Peta
1 0 3183 4306 4253 4481
2 60 5443 5982 5672 4816
3 90 5987 6259 6400 4812
4 120 6014 6895 6733 3816
5 150 6687 6951 7563 2047
6 180 6065 6540 8701 1881
SCATTERPLOT: plot()
SCATTER PLOT
• Ex1. Scatter plot of GY and Nitrogen Rate

> attach(gdata2)
> plot(Nitrogen, C463, main=“Plot
of Yield at Different Nitrogen
Rate”, xlab=“Nitrogen Rate",
ylab=“Grain Yield, col="Red")

# x – Nitrogen
# y – C463

PLOTTING CHARACTERS: PLOTTING CHARACTERS


plot(pch=)
• Ex2. With graphical parameter pch

> plot(…, pch=19)

# … - same content as Ex1

# pch – plotting character that controls


the type of symbol.
– Can either be an integer from 1-25 • Colours can be obtained with options
and character (“*”, “?”, “.”, …) col=“blue”, bg=“yellow” (only for the
symbols 21-25)
STYLE OF AXES LABELS: CUSTOMIZING A PLOT
plot(las=)
• Ex3. With graphical parameter las
> plot(…, pch=19, las=3)

# … - same content as Ex1


# las - style of axis labels
0: always parallel to the axis [default],
1: always horizontal
2: always perpendicular to the axis,
3: always vertical.

CUSTOMIZING A PLOT CUSTOMIZING A PLOT


> par(cex=0.9, bg=“lightyellow”,
col.axis=“blue”,
mar=c(4,4,2.5,0.5))
> plot(…)
# par – modify graphical parameters such as bg,
col.axis and mar
# cex – controls the size of the text
# bg – background color
# col.axis –color of the numbers on the axes
# mar - use to set the margin size specified in
inches of the form c(bot, left, top, right).
- The default is mar=c(5, 4, 4, 2)
LINE CHART : plot(type=)
• Ex1. Connecting points using plot()
> plot(Nitrogen, C463, main=“
main=“Plot of
Yield at Different Nitrogen Rate”
Rate”,
LINE CHART xlab=“
xlab=“Nitrogen Rate", ylab=“
ylab=“Grain
Yield, col="Red“
col="Red“, pch=19, las=3,
xlim=c(0,180), ylim=c(1000,8000),
type=“
type=“o”, lty=1)
lty=1)
# type = “p” # point
= “l” # line
= “o” # overplotted point and line
= “b” # points joined by line
= “s” # stair steps
= “h” # histogram like vertical line
= “n” # does not produce points or line

LINE CHART : plot(lty=) LINE CHART : plot()


• Ex1. Connecting points using plot()
# lty - style of axis labels option to change
line type. The default value is 1.
lines() ADDING LINE CHART

• Use to create line charts that adds • Ex2. Adding line charts to plot()
information to a graph > lines(IR5~Nitrogen, pch=19,
col="blue", type="o", lty=1)
> lines(x,y, type= …)
> lines(IR8~Nitrogen, pch=19,
> lines(y~x, type= …) col=“green", type="o", lty=1)
> lines(Peta~Nitrogen, pch=19,
# x and y – numeric vectors of points to connect
col=“brown", type="o", lty=1)

ADDING LINE CHART ADDING A LEGEND : legend()


• Adds legends to plots.
> legends(x, legend, pch, col,
lty, …)
# x – use to position the legend that can be a
single keyword from the list, “bottomright”
bottomright”,
“bottom”
bottom”, “bottomleft”
bottomleft”, “left”
left”, “topleft”
topleft”, “top”
top”,
“topright”
topright”, “right”
right” and “center”
center”
# legend – a character
ADDING A LEGEND : legend() ADDING A LEGEND
• Ex3. Adding a legend
> legend("topleft", legend=c("C4-
63", "IR5", "IR8", "Peta"),
pch=1:4, bty="n")
> detach(gdata2)

# pch – plotting symbols appearing in the legend


# col – color of points or lines appearing in the
legend
# lty – line types and widths appearing in the
legend

SUPPRESSING PLOT OF AXES ADD AN AXIS : axis()


• Ex4. Suppressing plot of axes • Adds an axes to the current plot
> plot(Nitrogen, C463, main=“
main=“Plot of > axis(side, at, …)
Yield at Different Nitrogen Rate”
Rate”,
xlab=“
xlab=“Nitrogen Rate", ylab=“
ylab=“Grain
# side – an integer specifying which side of the
Yield, col="Red“
col="Red“, pch=19, las=3,
type=“
type=“o”, lty=1, xaxt=“
xaxt=“n”, yaxt=“
yaxt=“n”) axis is to be drawn on.
side = 1 : below side = 3 : above
# xaxt and yaxt – a character which specifies the side = 2 : left side = 4 : right
x-axis and y-
y-axis type. Specifying “n” # at – points at which tick-
tick-marks are to be drawn
suppresses plotting of the axis.
ADD AN AXIS : axis()
Ex5. Adds an axes to the current plot
> plot(…
plot(…) # same as Ex4
> axis(
axis(side=1,
side=1, at=seq(0,180,30))
> axis(
axis(side=2,
side=2, at=seq(3000,7000,500)) BAR GRAPH / BAR PLOT

DATAFRAME : gdata3.csv
barplot()
barplot(height, # vector or matrix of values describing
• Summary of Variety Means
# the bars which make up the plot.
beside = FALSE, # stacked by default. If
# TRUE bars will be side
# by side
density = NULL, # a vector giving the density of
# shading lines, in lines per inch,
# for the bars or bar components.
# The default value of NULL
# means that no shading lines are
# drawn.
xlab = NULL, # label for x-axis
ylab = NULL, # label for y-axis
ylim = NULL) # limit for y-axis
DATA FRAME: gdata3 SIMPLE BAR PLOT : gdata3
Read data file gdata3.csv
gdata3.csv > barplot(gdata3$Yield)
> gdata3 <- read.table(“
read.table(“gdata3.csv",
gdata3.csv",
header=T, sep=",")
> gdata3
Variety Yield
1 V1 4.7559
2 V2 5.0410
3 V3 5.0581

WITH LABELS AND COLORS DATA FRAME FOR GROUPED


BAR PLOT : gdata2.csv
> barplot(gdata3$
barplot(gdata3$Yield,
Yield, xlab="Variety",
ylab="Grain Yield",
names.arg=c(“
ames.arg=c(“V1",
V1",“
“V2",
V2",“
“V3“
V3“), Read data file gdata2.csv
gdata2.csv
col=c(“
col=c(“pink", “lightblue", > gdata4 <- <- read.table(“
read.table(“gdata2.csv",
“yellowgreen“
yellowgreen“)) header=T, sep=",”
sep=",”,row.names=“
,row.names=“Nitrogen”
Nitrogen”)
> gdata4 < t(as.matrix(gdata4))
# names.arg – a vector 0 60 90 120 150 180
of names to be plotted C463 3183 5443 5987 6014 6687 6065
below each bar or group IR5 4306 5982 6259 6895 6951 6540
of bars IR8 4253 5672 6400 6733 7563 8701
Peta 4481 4816 4812 3816 2047 1881
GROUPED BAR PLOT – gdata4 ADD LEGEND
> legend("topleft", Vlevel, bty="n",
> barplot(gdata4
barplot(gdata4,
, beside=T, fill=c("pink", "lightblue",
xlab="Variety", ylab="Grain Yield", "yellowgreen", "red"))
col=c("pink", "lightblue",
"yellowgreen,
"yellowgreen, “red”
red”))

PACKAGE ‘sciplot’
• A collection of functions that creates
graphs with error bars for data collected
from one-way or higher factorial designs.
ERROR BARS
• It has three available functions:
> bargraph.CI()
> lineplot.CI()
> se()
BARGRAPH WITH ERROR BARS: BARGRAPH WITH ERROR BARS:
bargraph.CI() bargraph.CI()
• Use to obtain barplot of the mean and standard
error of a response variable.
• Usage:
• Usage: > bargraph.CI(x.factor, response,
group, density, angle, legend,
> bargraph.CI(x.factor, response, x.leg, cex.leg …)
group, …)
# x.leg, y.leg – to over ride the default legend
# x.factor – a factor whose levels will form the placement
x-axis
# cex.leg – character expansion value for
# response – numeric response variable legend values
# group – grouping factor whose levels will
form groups of bars for each level of x.factor

BARGRAPH WITH ERROR BARS: BARGRAPH WITH ERROR BARS


gdata1
> bargraph.CI(Nitrogen, Yield,
group=Variety, data=gdata1
data=gdata1,
,
density=45, col=c("pink",
"lightblue", "yellowgreen“
"yellowgreen“,
“red”
red”), legend=T, x.leg=1,
cex.leg=0.8)
LINE PLOT WITH ERROR BARS: LINEGRAPH WITH ERROR BARS
lineplot.CI()
> lineplot.CI(Nitrogen, Yield,
• Use to obtain lineplot of the mean and standard group=Variety, data=gdata1
data=gdata1,
,
error of a response variable. density=45, col=c(“
col=c(“red", “blue",
• Usage: "green"), legend=T, x.leg=1,
cex.leg=0.8, xlab=“
xlab=“Nitrogen
> lineplot.CI(x.factor, response, Rate”
Rate”, pch=c(15:18))
group, type…
type…)

# group – grouping factor whose levels will


form the traces
# type – the type of plot: lines, points, or both

LINEGRAPH WITH ERROR BARS

GRAPHICAL DEVICES
GRAPHICAL DEVICE USAGE OF GRAPHICS DEVICE
• Before you create your graphs, you can decide
on a format for output. The format is called a jpeg(filename=“
jpeg(filename=“plot.jpg”
plot.jpg”, width=480,
device. height=480, pointsize=12,
• A graphical device is a graphical window or a units=“
units=“px”
px”)
file.
file.
png() – same parameters as jpeg
• Some commonly used graphical device
pdf() Produces PDF file pdf(file=“
pdf(file=“plot.pdf”
plot.pdf”, width=7,
height=7, paper = “a4”
a4”)
png() Produces a bitmap PNG file
jpeg() Produces a bitmap JPEG file # filename - the name of the output file

# width and height – width and height of the device

USAGE OF GRAPHICS DEVICE COMPARISONS OF GRAPHICS


DEVICE
jpeg(…
jpeg(…, pointsize=12,
pointsize=12, units=“
units=“px”
px”)
• Plots in JPEG and PNG format can easily
# pointsize – default pointsize of plotted text, converted to many other bitmap formats.
interpreted as big points(1/72 inch) at res dpi
• PNG format is lossless and is best for line
# units – The units in which height and width are diagrams. While JPEG format is lossy
given. Can be px(pixels), in, cm or mm.
• In pdf() multiple graphics can be saved in one
• When you have finished with a device, be sure file.
to terminate the device driver by using dev.off()
• graphics.off() closes all open graphics devices.
SAVING GRAPHS IN FILES
• Ex. Saving histogram in png device

>
>
png(filename=“
png(filename=“hist.png”
hist(gdata$Y)
hist.png”) THANK YOU! ☺
> dev.off() Please do Exercise C

You might also like