R Markdown

1. Working Directory

getwd() # print the current working directory
## [1] "E:/Binus University/Semester 2/Data Mining and Visualization/Lab"
#ls() # list the objects in the current workspace
#setwd(mydirectory) # change to mydirectory
#setwd("E:/Binus University")

2. Previous Commands

#history()
#loadhistory(file="Lab_Session1.Rhistory")

history() # display last 25 commands history(max.show=Inf) # display all previous commands loadhistory(file=“myfile) # default is”.Rhistory” ## 3. Save your command history

savehistory(file=“myfile”) # default is “.Rhistory”

B. Plotting Systems

1. The Base Plotting System

The base plotting system is the original 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))

2. Base Graphics

3. Simple Base Graphics

Histogram

library(datasets)

## Draw a new plot on the screen device
hist(airquality$Ozone)

Boxplot

airquality <- transform(airquality, Month = factor(Month))
boxplot(Ozone ~ Month, airquality, xlab = "Months", ylab = "Ozone (ppb)")

Scatterplot

with(airquality, plot(Wind, Ozone))

4. Some Important Base Graphics Parameters

Base Plotting Functions

1. Add Titles

library(datasets)

## Make the initial plot
with(airquality, plot(Wind, Ozone))

## Add a title
title(main = "Ozone and Wind in New York City")

# 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)

2. Add Text(Text characteristics)

set.seed(1)

# Generate sample data
#x <- rnorm(500)
#y <- x + rnorm(500)

x <- airquality$Ozone
y <- airquality$Wind
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

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

plot(x, y,
     main = "My title",
     sub = "Subtitle",
     font.main = 1, # Title font style
     font.sub = 2, # Subtitle dont style
     font.axis = 3, # Axis tick labels font style
     font.lab = 4) # font style of X and Y axis labels

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)