psych
cor.plot()
car
, ellipse
, lattice
, mvtnorm
, rgl
c("car", "ellipse", "lattice", "mvtnorm", "rgl")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
c(1, 3)
mu <- matrix(c(1, 0.6, 0.6, 1), nrow=2)
sigma <- 2.5
rng <- 50
N <- seq(mu[1]-rng*sigma[1, 1], mu[1]+rng*sigma[1, 1], length.out=N)
X <- seq(mu[2]-rng*sigma[2, 2], mu[2]+rng*sigma[2, 2], length.out=N) Y <-
set.seed(123)
library(mvtnorm)
function(x, y) { dmvnorm(cbind(x, y), mu, sigma) }
genZ <- outer(X, Y, FUN="genZ") matZ <-
contour(X, Y, matZ, main="Contours for 2D-normal density")
filled.contour(X, Y, matZ, main="Colored contours for 2D-normal density")
10
N <- rnorm(N, 30, 8)
age <- abs(-0.25*age + rnorm(N, 60, 40))
sport <- -0.3*age -0.4*sport + 100 + rnorm(N, 0, 3)
weight <- (weight-min(weight)) * (0.8 / abs(diff(range(weight)))) + 0.2
wScale <-symbols(age, sport, circles=wScale, inch=0.6, fg=NULL, bg=rainbow(N),
main="Weight against age and sport")
See sunflowerplot()
and stars()
for altenative approaches.
par(cex.main=1.4, mar=c(2, 2, 4, 2) + 0.1)
persp(X, Y, matZ, xlab="x", ylab="y", zlab="Density", theta=5, phi=35,
main="2D-normal probability density")
library(rgl)
rep(seq(-10, 10, length.out=10), times=10)
vecX <- rep(seq(-10, 10, length.out=10), each=10)
vecY <- vecX*vecY
vecZ <-plot3d(vecX, vecY, vecZ, main="3D Scatterplot",
col="blue", type="h", aspect=TRUE)
spheres3d(vecX, vecY, vecZ, col="red", radius=2)
grid3d(c("x", "y+", "z"))
demo(rgl)
example(persp3d)
# not shown
25
Njk <- 2
P <- 2
Q <- rnorm(P*Q*Njk, mean=100, sd=15)
IQ <- rnorm(P*Q*Njk, mean=175, sd=7)
height <- factor(rep(c("control", "treatment"), each=Q*Njk))
IV1 <- factor(rep(c("f", "m"), times=P*Njk))
IV2 <- data.frame(IV1, IV2, IQ, height)
myDf <-coplot(IQ ~ height | IV1*IV2, pch=16, data=myDf)
library(lattice)
histogram(IQ ~ height | IV1*IV2, data=myDf,
res <-main="Histograms per group")
print(res)
20
N <- 2
P <- rep(c("CG", "T"), each=N/P)
IV <- sample(18:35, N, replace=TRUE)
age <- round(rnorm(N, mean=rep(c(100, 115), each=N/P), sd=15))
IQ <- round(0.4*IQ - 30 + rnorm(N, 0, 10), 1)
rating <- round(-0.3*IQ + 0.7*age + rnorm(N, 0, 8), 1)
score <- data.frame(IV, age, IQ, rating, score) mvDf <-
pairs(mvDf[c("age", "IQ", "rating", "score")], main="Scatter plot matrix",
pch=16, col=c("red", "blue")[unclass(mvDf$IV)])
function(x, ...) { par(new=TRUE); hist(x, ..., main="") }
myHist <- function(x, y, nSegments=100, rad=1, ...) {
myEll <- split(data.frame(x, y), mvDf$IV)
splLL <- data.matrix(splLL$CG)
CG <- data.matrix(splLL$T)
TT <-
library(car)
dataEllipse(CG, level=0.5, col="red", center.pch=4,
plot.points=FALSE, add=TRUE)
dataEllipse(TT, level=0.5, col="blue", center.pch=4,
plot.points=FALSE, add=TRUE)
}
pairs(mvDf[c("age", "IQ", "rating", "score")], diag.panel=myHist,
upper.panel=myEll, main="Scatter plot matrix", pch=16,
col=c("red", "blue")[unclass(mvDf$IV)])
Illustrating the correlation matrix of several variables.
library(mvtnorm)
200
N <- 8
P <- 2
Q <- matrix(round(runif(P*Q, min=-0.9, max=0.9), 1), nrow=P)
Lambda <- rmvnorm(N, mean=c(0, 0), sigma=diag(Q))
FF <- rmvnorm(N, mean=rep(0, P), sigma=diag(P)*0.3)
E <- FF %*% t(Lambda) + E X <-
cor(X)
corMat <-rownames(corMat) <- paste("X", 1:P, sep="")
colnames(corMat) <- paste("X", 1:P, sep="")
round(corMat, 2)
X1 X2 X3 X4 X5 X6 X7 X8
X1 1.00 -0.37 0.17 0.11 -0.11 -0.34 -0.04 -0.43
X2 -0.37 1.00 -0.13 0.11 -0.05 0.57 -0.17 0.39
X3 0.17 -0.13 1.00 0.63 -0.62 -0.03 -0.51 -0.44
X4 0.11 0.11 0.63 1.00 -0.79 0.13 -0.64 -0.40
X5 -0.11 -0.05 -0.62 -0.79 1.00 -0.12 0.64 0.45
X6 -0.34 0.57 -0.03 0.13 -0.12 1.00 -0.28 0.38
X7 -0.04 -0.17 -0.51 -0.64 0.64 -0.28 1.00 0.23
X8 -0.43 0.39 -0.44 -0.40 0.45 0.38 0.23 1.00
image(corMat, axes=FALSE, main=paste("Correlation matrix of", P, "variables"))
axis(side=1, at=seq(0, 1, length.out=P), labels=rownames(corMat))
axis(side=2, at=seq(0, 1, length.out=P), labels=colnames(corMat))
See heatmap()
for a heatmap including dendograms added to the plot sides and correlation for an alternative approach to visualize correlation matrices.
library(ellipse)
plotcorr(corMat, type="lower", diag=FALSE, main="Bivariate correlations")
tourr
for an alternative to visualizing high-dimensional data.ggplot2
and lattice
provide their own graphics system and many functions for multi-panel plots.rggobi
, and playwith
also create interactive diagrams.googleVis
, ggvis
, and rCharts
create online interactive diagrams based on JavaScript libraries.try(detach(package:car))
try(detach(package:ellipse))
try(detach(package:mvtnorm))
try(detach(package:rgl))
try(detach(package:lattice))
R markdown - markdown - R code - all posts