Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created April 16, 2012 17:46
Show Gist options
  • Save geofferyzh/2400262 to your computer and use it in GitHub Desktop.
Save geofferyzh/2400262 to your computer and use it in GitHub Desktop.
RinAction - R Graph Parameter Basics
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
# R in Action - Working with graphs #
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
setwd <- "C:/RinAction/data/workspace"
setwd <- "C:\RinAction\data\workspace"
mtcars <- read.table("C:/RinAction/data/mtcars.csv", header=TRUE, sep=",")
# pause after each graph
par(ask = TRUE)
install.packages("Hmisc")
########################################
# Saving Graphs #
########################################
pdf("C:/RinAction/data/workspace/mygraph.pdf")
attach(mtcars)
plot(wt,mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
detach(mtcars)
dev.off()
########################################
# Simple Examples #
########################################
help(plot)
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type = "b")
######################################################
# Saving & Restoring Current Graph Parameters #
######################################################
oldpar <- par(no.readonly = TRUE)
oldpar
help(par)
par(lty = 2, pch = 17)
plot(dose, drugA, type = "b")
par(oldpar)
######################################################
# Symbols and lines #
# pch --> symbol type #
# cex --> symbol size #
# lty --> line type #
# lwd --> line width #
######################################################
plot(dose, drugA, type = "b", lty = 2, pch = 17)
plot(dose, drugA, type = "b", lyt = 3, lwd = 3, pch = 15, cex = 2)
######################################################
# --------Colors----------- #
# col default plotting color #
# col.axis color for axis text #
# col.lab color for axis labels #
# col.main color for titles #
# col.sub color for subtitles #
# fg foreground color #
# bg background color #
######################################################
# You can specify colors in R by index, name, hexadecimal, RGB, or HSV
# col=1, col="white", col="#FFFFFF", col=rgb(1,1,1), and col=hsv(0,0,1)
# http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.pdf
# Functions that's used to create vectors of contiguous colors: rainbow(), heat.colors(),
# terrain.colors(), # topo.colors(), cm.colors()
n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels = mycolors, col = mycolors)
mygrays <- gray(0:n/n)
pie(rep(1, n), labels = mygrays, col = mygrays)
#################################################################
# Text characteristics #
# cex --> number indicating the amount by which
# plotted text should be scaled relative
# to the default #
# cex.axis --> magnification of axis text relative to cex #
# cex.lab --> magnification of axis labels relative to cex#
# cex.main --> magnification of titles relative to cex #
# cex.sub --> magnification of subtitlesls relative to cex#
# font #
# font.axis #
# font.lab #
# font.main #
# font.sub #
# ps -----> Font point size(roughly 1/72 inch) #
# -----> Text size = ps*cex #
# family ---> font family for drawing text #
#################################################################
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly = TRUE)
par(pin = c(4, 6))
par(lwd = 2, cex = 1.5)
par(cex.axis = 0.3, font.axis = 3)
plot(dose, drugA, type = "b", pch = 19, lty = 2, col = "red")
plot(dose, drugB, type = "b", pch = 23, lty = 6, col = "blue", bg = "green")
par(opar)
# Font family
opar <- par(no.readonly = TRUE)
par(cex = 1.5)
plot(1:7, 1:7, type = "n")
text(3, 3, "Example of default text")
text(4, 4, family = "mono", "Example of mono-spaced text")
text(5, 5, family = "serif", "Example of serif text")
par(opar)
######################################################
# --------Titles/Axis/Lengends--------- #
# main title #
# subtitle subtitle #
# xlab x-axis label #
# ylab y-axis label #
# xlim x-axis range #
# ylim y-axis range #
######################################################
# Title
plot(dose, drugA, type = "b", col = "red", lty = 2,
pch = 2, lwd = 2, main = "Clinical Trials for Drug A",
sub = "This is hypothetical data",
xlab = "Dosage", ylab = "Drug Response", xlim = c(0, 60),
ylim = c(0, 70))
title(main="My title", col.main="red",
sub="My Sub-title", col.sub="blue"
xlab="My X Label", ylab="My Y label"
col.lab="green", cex.lab=0.75)
# Custom Axes
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly = TRUE)
par(mar = c(5, 4, 4, 8) + 0.1)
plot(x, y, type = "b", pch = 21, col = "red", yaxt = "n", lty = 3)
plot(x, y, type = "b", pch = 21, col = "red", yaxt = "n", lty = 3, ann = FALSE)
lines(x, z, type = "b", pch = 22, col = "blue", lty = 2)
axis(2, at = x, labels = x, col.axis = "red", las = 2)
axis(4, at = z, labels = round(z, digits = 2), col.axis = "blue", las = 2, cex.axis = 0.7, tck = -0.01)
mtext("y=1/x", side = 4, line = 3, cex.lab = 1, las = 2, col = "blue")
title("An Example of Creative Axes", xlab = "X values", ylab = "Y=X")
par(opar)
# Referece Line
# Lengend
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly = TRUE)
par(lwd = 2, cex = 1.5, font.lab = 2)
plot(dose, drugA, type = "b", pch = 15, lty = 1, col = "red",
ylim = c(0, 60), main = "Drug A vs. Drug B", xlab = "Drug Dosage",
ylab = "Drug Response")
lines(dose, drugB, type = "b", pch = 17, lty = 2,col = "blue")
abline(h = c(30), lwd = 1.5, lty = 2, col = "grey")
library(Hmisc)
minor.tick(nx = 3, ny = 3, tick.ratio = 0.5)
legend("topleft", inset = 0.5, title = "Drug Type",
c("A", "B"), lty = c(1, 2), pch = c(15, 17), col = c("red","blue"))
par(opar)
# Labeling points
attach(mtcars)
plot(wt, mpg, main = "Milage vs. Car Weight", xlab = "Weight",
ylab = "Mileage", pch = 18, col = "blue")
text(wt, mpg, row.names(mtcars), cex = 0.6, pos = 4,
col = "red")
detach(mtcars)
# View font families
opar <- par(no.readonly = TRUE)
par(cex = 1.5)
plot(1:7, 1:7, type = "n")
text(3, 3, "Example of default text")
text(4, 4, family = "mono", "Example of mono-spaced text")
text(5, 5, family = "serif", "Example of serif text")
par(opar)
###################################
## combining graphs ##
###################################
# Combine Example 1
attach(mtcars)
opar <- par(no.readonly = TRUE)
par(mfrow = c(2, 2))
plot(wt, mpg, main = "Scatterplot of wt vs. mpg")
plot(wt, disp, main = "Scatterplot of wt vs disp")
hist(wt, main = "Histogram of wt")
boxplot(wt, main = "Boxplot of wt")
par(opar)
detach(mtcars)
# Combine Example 2
attach(mtcars)
opar <- par(no.readonly = TRUE)
par(mfrow = c(3, 1))
hist(wt)
hist(mpg)
hist(disp)
par(opar)
detach(mtcars)
# Multi-Chart Layout
attach(mtcars)
layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)
# Multi-Chart Layout control
attach(mtcars)
layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE),
widths = c(3, 1), heights = c(1, 2))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)
# Fine placement of figures in a graph
opar <- par(no.readonly = TRUE)
par(fig = c(0, 0.8, 0, 0.8))
plot(mtcars$wt, mtcars$mpg, xlab = "Miles Per Gallon",
ylab = "Car Weight")
par(fig = c(0, 0.8, 0.55, 1), new = TRUE)
boxplot(mtcars$wt, horizontal = TRUE, axes = FALSE)
par(fig = c(0.65, 1, 0, 0.8), new = TRUE)
boxplot(mtcars$mpg, axes = FALSE)
mtext("Enhanced Scatterplot", side = 3, outer = TRUE,
line = -3)
par(opar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment