getwd() # print the current working directory
## [1] "C:/Users/noviyantisagala/OneDrive - Bina Nusantara University/WORK/Teaching Material/even21-22/DataMining&Visualization/Lab"
ls() # list the objects in the current workspace
## character(0)
#setwd(mydirectory) # change to mydirectory
setwd("C:/Users/noviyantisagala/Documents")
history() # display last 25 commands history(max.show=Inf) # display all previous commands loadhistory(file=“myfile”) # default is “.Rhistory”
savehistory(file=“myfile”) # default is “.Rhistory”
The base plotting system is the original plotting system for R. The basic model is sometimes referred to as the “artist’s palette” model. The idea is you start with blank canvas and build up from there.
In more R-specific terms, you typically start with plot
function (or similar plot creating function) to initiate a plot
and then annotate the plot with various annotation functions
(text, lines, points,
axis)
The base plotting system is often the most convenient plotting system to use because it mirrors how we sometimes think of building plots and analyzing data. If we don’t have a completely well-formed idea of how we want to look at some data, often we’ll start by “throwing some data on the page” and then slowly add more information to it as our thought process evolves.
The core plotting and graphics engine in R is encapsulated in the following packages:
graphics: contains plotting functions for the “base”
graphing systems, including plot, hist,
boxplot and many others.
grDevices: contains all the code implementing the
various graphics devices, including X11, PDF, PostScript, PNG,
etc.
The grDevices package contains the functionality for
sending plots to various output devices. The graphics
package contains the code for actually constructing and annotating
plots.
## Create the plot / draw canvas
with(cars, plot(speed, dist))
The downside of the base plotting system is that it’s difficult to
describe or translate a plot to others because there’s no clear
graphical language or grammar that can be used to communicate what
you’ve done. The only real way to describe what you’ve done in a base
plot is to just list the series of commands/functions that you’ve
executed, which is not a particularly compact way of communicating
things. This is one problem that the ggplot2 package
attempts to address.
Base graphics are used most commonly and are a very powerful system for creating data graphics. There are two phases to creating a base plot:
Calling plot(x, y) or hist(x) will launch a
graphics device (if one is not already open) and draw a new plot on the
device. If the arguments to plot are not of some special
class, then the default method for plot is called;
this function has many arguments, letting you set the title, x
axis label, y axis label, etc.
The base graphics system has many global parameters that can
set and tweaked. These parameters are documented in ?par
and are used to control the global behavior of plots, such as the
margins, axis orientation, and other details. It wouldn’t hurt to try to
memorize at least part of this help page!
Another typical base plot is constructed with the following code.
data(cars)
## Create the plot / draw canvas
with(cars, plot(speed, dist))
## Add annotation
title("Speed vs. Stopping distance")
Base plot with title
Here is an example of a simple histogram made using the
hist() function in the graphics package. If
you run this code and your graphics window is not already open, it
should open once you call the hist() function.
library(datasets)
## Draw a new plot on the screen device
hist(airquality$Ozone)
Ozone levels in New York City
Boxplots can be made in R using the boxplot() function,
which takes as its first argument a formula. The formula has
form of y-axis ~ x-axis. Anytime you see a ~
in R, it’s a formula. Here, we are plotting ozone levels in New York
by month, and the right hand side of the ~
indicate the month variable. However, we first have to transform the
month variable in to a factor before we can pass it to
boxplot(), or else boxplot() will treat the
month variable as continuous.
airquality <- transform(airquality, Month = factor(Month))
boxplot(Ozone ~ Month, airquality, xlab = "Month", ylab = "Ozone (ppb)")
Ozone levels by month in New York City
Each boxplot shows the median, 25th and 75th percentiles of the data (the “box”), as well as +/- 1.5 times the interquartile range (IQR) of the data (the “whiskers”). Any data points beyond 1.5 times the IQR of the data are indicated separately with circles.
In this case the monthly boxplots show some interesting features. First, the levels of ozone tend to be highest in July and August. Second, the variability of ozone is also highest in July and August. This phenomenon is common with environmental data where the mean and the variance are often related to each other.
Here is a simple scatterplot made with the plot()
function.
with(airquality, plot(Wind, Ozone))
Scatterplot of wind and ozone in New York City
Generally, the plot() function takes two vectors of
numbers: one for the x-axis coordinates and one for the y-axis
coordinates. However, plot() is what’s called a generic
function in R, which means its behavior can change depending on
what kinds of data are passed to the function.
One thing to note here is that although we did not provide labels for the x- and the y-axis, labels were automatically created from the names of the variables (i.e. “Wind” and “Ozone”). This can be useful when you are making plots quickly, but it demands that you have useful descriptive names for the your variables and R objects.
Many base plotting functions share a set of global parameters. Here are a few key ones:
pch: the plotting symbol (default is open circle)lty: the line type (default is solid line), can be
dashed, dotted, etc.lwd: the line width, specified as an integer
multiplecol: the plotting color, specified as a number, string,
or hex code; the colors() function gives you a vector of
colors by namexlab: character string for the x-axis labelylab: character string for the y-axis labelThe most basic base plotting function is plot(). The
plot() function makes a scatterplot, or other type of plot
depending on the class of the object being plotted. Calling
plot() will draw a plot on the screen device (and open the
screen device if not already open). After that, annotation functions can
be called to add to the already-made plot.
Some key annotation functions are
lines: add lines to a plot, given a vector of
x values and a corresponding vector of y
values (or a 2-column matrix); this function just connects the dotspoints: add points to a plottext: add text labels to a plot using specified x, y
coordinatestitle: add annotations to x, y axis labels, title,
subtitle, outer marginmtext: add arbitrary text to the margins (inner or
outer) of the plotaxis: adding axis ticks/labelsPlot Titles Plot titles can be specified either directly to the plotting functions during the plot creation or by using the title() function (to add titles on an existing plot).
We make the plot with the plot() function and then add a
title to the top of the plot with the title() function.
library(datasets)
## Make the initial plot
with(airquality, plot(Wind, Ozone))
## Add a title
title(main = "Ozone and Wind in New York City")
Base plot with annotation
# Add titles
barplot(c(2,5), main="Main title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub-title",
col.main="red", col.lab="blue", col.sub="black")
# Increase the size of titles
barplot(c(2,5), main="Main title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub-title",
cex.main=2, cex.lab=1.7, cex.sub=1.2)
Graphic parameters are also used to specify text size, font, and
style.
set.seed(1)
# Generate sample data
x <- rnorm(500)
y <- x + rnorm(500)
plot(x, y, main = "My title", sub = "Subtitle",
cex.main = 2, # Title size
cex.sub = 1.5, # Subtitle size
cex.lab = 3, # X-axis and Y-axis labels size
cex.axis = 0.5) # Axis labels size
You can set this argument to 1 for plain text, 2 to bold (default), 3 italic and 4 for bold italic text. This argumento won’t modify the title style.
plot(x, y, font = 2, main = "Bold") # Bold
plot(x, y, font = 3, main = "Italics") # Italics
plot(x, y, font = 4, main = "Bold italics") # Bold italics
You can also specify the style of each of the texts of the plot with the font.main, font.sub, font.axis and font.lab arguments.
plot(x, y,
main = "My title",
sub = "Subtitle",
font.main = 1, # Title font style
font.sub = 2, # Subtitle font style
font.axis = 3, # Axis tick labels font style
font.lab = 4) # Font style of X and Y axis labels
On the one hand, the mtext function in R allows you to add text to all sides of the plot box. There are 12 combinations (3 on each side of the box, as left, center and right align). You just need to change the side and adj to obtain the combination you need.
On the other, the text function allows you to add text or formulas inside the plot at some position setting the coordinates. In the following code block some examples are shown for both functions.
mtext does not support rotation, only horizontal adjustment with las = 1 for the vertical axis and vertical adjustment with las = 3 for the X-axis. If you need to rotate the text you can use text function with srt argument instead.
line, to set the margin line where to set the
text. Default value is 0. adj, to adjust the text in the reading
direction from 0 to 1 (default value is 0.5).
plot(x, y, main = "Main title", cex = 2, col = "blue")
#---------------
# mtext function
#---------------
# Bottom-center
mtext("Bottom text", side = 1)
# Left-center
mtext("Left text", side = 2)
# Top-center
mtext("Top text", side = 3)
# Right-center
mtext("Right text", side = 4)
# Bottom-left
mtext("Bottom-left text", side = 1, adj = 0)
# Top-right
mtext("Top-right text", side = 3, adj = 1)
# Top with separation
mtext("Top higher text", side = 3, line = 2.5)
plot(x, y, main = "Main title", cex = 2, col = "blue")
#--------------
# Text function
#--------------
# Add text at coordinates (-2, 2)
text(-2, 2, "More text")
# Rotate 45 degrees
text(3,2, label = "Text annotation",
srt = 45) # Rotation
# Split the text in several lines
text(3, -2,
label = "Text\n annotation") # Split text
The legend() function can be used. A simplified format is :
x and y : the co-ordinates to be used for the legend. Keywords can also be used for x : “bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right” and “center”. legend : the text of the legend col : colors of lines and points beside the text for legends
legend(x, y=NULL, legend, col)
# Generate some data
x<-1:10; y1=x*x; y2=2*y1
# First line plot
plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y")
# Add a second line
lines(x, y2, pch=18, col="blue", type="b", lty=2)
# Add legends
legend("topleft", legend=c("Line 1", "Line 2"),
col=c("red", "blue"), lty=1:2, cex=0.8)
Point symbols can be changed using the argument pch.
The following arguments can be used to change the
color and the size of the points :
col : color (code or name) to use for the points bg : the background (or fill) color for the open plot symbols. It can be used only when pch = 21:25. cex : the size of pch symbols lwd : the line width for the plotting symbols
x<-c(2.2, 3, 3.8, 4.5, 7, 8.5, 6.7, 5.5)
y<-c(4, 5.5, 4.5, 9, 11, 15.2, 13.3, 10.5)
# Change plotting symbol using pch
plot(x, y, pch = 19, col="blue")
plot(x, y, pch = 18, col="red")
plot(x, y, pch = 24, cex=2, col="blue", bg="red", lwd=2)
Line types can be changed using the graphical
parameter lty. line type (lty) can be specified using either text
(“blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, “twodash”)
or number (0, 1, 2, 3, 4, 5, 6). Note that lty = “solid” is identical to
lty=1.
x=1:10; y=x*x
plot(x, y, type="l") # Solid line (by default)
plot(x, y, type="l", lty="dashed")# Use dashed line type
plot(x, y, type="l", lty="dashed", lwd=3)# Change line width
# Generate a plot of color names which R knows about.
#++++++++++++++++++++++++++++++++++++++++++++
# cl : a vector of colors to plots
# bg: background of the plot
# rot: text rotation angle
#usage=showCols(bg="gray33")
showCols <- function(cl=colors(), bg = "grey",
cex = 0.75, rot = 30) {
m <- ceiling(sqrt(n <-length(cl)))
length(cl) <- m*m; cm <- matrix(cl, m)
require("grid")
grid.newpage(); vp <- viewport(w = .92, h = .92)
grid.rect(gp=gpar(fill=bg))
grid.text(cm, x = col(cm)/m, y = rev(row(cm))/m, rot = rot,
vp=vp, gp=gpar(cex = cex, col = cm))
}
To view all the built-in color names which R knows about (n = 657), use the following R code :
showCols(cl= colors(), bg="gray33", rot=30, cex=0.75)
## Loading required package: grid
# The first sixty color names
showCols(bg="gray20",cl=colors()[1:60], rot=30, cex=0.9)
# Barplot using color names
barplot(c(2,5), col=c("chartreuse", "blue4"))
Source : http://www.visibone.com
barplot(c(2,5), col=c("#009999", "#0000FF"))
#install.packages("RColorBrewer")
library("RColorBrewer")
display.brewer.all()
There are 3 types of palettes : sequential, diverging, and qualitative.
Sequential palettes are suited to ordered data that progress from low to high (gradient). The palettes names are : Blues, BuGn, BuPu, GnBu, Greens, Greys, Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples, RdPu, Reds, YlGn, YlGnBu YlOrBr, YlOrRd.
Diverging palettes put equal emphasis on mid-range critical values and extremes at both ends of the data range. The diverging palettes are : BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral
Qualitative palettes are best suited to representing nominal or categorical data. They not imply magnitude differences between groups. The palettes names are : Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3
# use color names
barplot(c(2,5), col=c("blue", "red"))
# use hexadecimal color code
barplot(c(2,5), col=c("#009999", "#0000FF"))
Paul Murrell (2011). R Graphics, CRC Press.
Hadley Wickham (2009). ggplot2, Springer.
Deepayan Sarkar (2008). Lattice: Multivariate Data Visualization with R, Springer.