# scatter plot
x <- rnorm(25)
y <- rnorm(25)
# default scatterplot, x is not sorted so type is set to "p"
plt(x, y)
# compare to standard R plot
plot(x, y)
# scatterplot, with ellipse and extended axes to accommodate the ellipse
plt(x, y, ellipse=TRUE, xlim=c(-3,3), ylim=c(-3,3))
# scatterplot, with loess line
plt(x, y, fit.line="loess")
# increase span (smoothing) from default of .75
plt(x, y, fit.line="loess", span=1)
# custom scatter plot
plt(x, y, col.point="darkred", col.fill="plum")
# bubble plot of simulated Likert data, 1 to 7 scale
# size of each plotted point (bubble) depends on its joint frequency
# triggered by default when < 10 unique values for each variable
x1 <- sample(1:7, size=100, replace=TRUE)
x2 <- sample(1:7, size=100, replace=TRUE)
plt(x1,x2)
# compare to usual scatterplot of Likert data
plt(x1,x2, kind="regular")
# plot Likert data and get sunflower plot with loess line
plt(x1,x2, kind="sunflower.freq", fit.line="loess")
# scatterplot of continuous Y against categorical X, a factor
Pain <- sample(c("None", "Some", "Much", "Massive"), size=25, replace=TRUE)
Pain <- factor(Pain, levels=c("None", "Some", "Much", "Massive"), ordered=TRUE)
Cost <- round(rnorm(25,1000,100),2)
plt(Pain, Cost)
# for this purpose, improved version of standard R stripchart
stripchart(Cost ~ Pain, vertical=TRUE)
# function curve
x <- seq(10,500,by=1)
y <- 18/sqrt(x)
# x is sorted with equal intervals so type set to "l" for line
plt(x, y)
# custom function plot
plt(x, y, ylab="My Y", xlab="My X", col.line="blue",
col.bg="snow", col.area="lightsteelblue", col.grid="lightsalmon")
# generate data randomly varying about a constant mean
y <- rnorm(25)
# default run chart
plt(y)
# compare to standard R plot
plot(y, type="l")
# customize run chart, pch=24: filled triangle point-up,
plt(y, lwd=2, col.point="sienna3", pch=24,
col.bg="mintcream", ylim=c(-3.5,3.5), center.line="median")
# generate steadily increasing values
y <- sort(rexp(50))
# default line chart
plt(y)
# line chart with border around plotted values
plt(y, col.area="transparent")
# time series chart, i.e., with dates, and filled area
# with option label for the x-axis
plt(y, time.start="2005/09/01", time.by="month")
# time series chart from a time series object
y.ts <- ts(y, start=c(2005, 9), frequency=12)
plt(y.ts)
# modern art
n <- sample(2:30, size=1)
x <- rnorm(n)
y <- rnorm(n)
clr <- colors()
color1 <- clr[sample(1:length(clr), size=1)]
color2 <- clr[sample(1:length(clr), size=1)]
plt(x, y, type="l", lty="dashed", lwd=3, col.area=color1,
col.line=color2, xy.ticks=FALSE, main="Modern Art",
cex.main=2, col.main="lightsteelblue", kind="regular")
# --------------------------------------------
# plots for data frames and multiple variables
# --------------------------------------------
# create data frame, mydata, to mimic reading data with rad function
# mydata contains both numeric and non-numeric data
mydata <- data.frame(rnorm(100), rnorm(100), rnorm(100), rep(c("A","B"),50))
names(mydata) <- c("X","Y","Z","C")
# although data not attached, access the variable directly by its name
plt(X)
plt(X,Y)
# variable of interest is in a data frame which is not the default mydata
# access the breaks and wool variables in the R provided warpbreaks data set
# wool is categorical with two levels, breaks is numeric
# although data not attached, access the variable directly by its name
data(warpbreaks)
plt(wool, breaks, dframe=warpbreaks)
Run the code above in your browser using DataLab