Last active
August 15, 2017 08:45
-
-
Save mw55309/7faddf5b0804f70dbc4f7cecb63fd202 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## functions | |
plot.histograms <- function(cname="", flip=FALSE, xlim) { | |
dse <- density(setosa[, cname]) | |
dve <- density(versicolor[, cname]) | |
dvi <- density(virginica[, cname]) | |
ymax <- max(c(dse$y, dve$y, dvi$y)) | |
ymin <- min(c(dse$y, dve$y, dvi$y)) | |
ylim <- c(ymin,ymax) | |
mar <- c(0,5,2,0) | |
if (flip==TRUE) { | |
temp <- dse$x | |
dse$x <- dse$y | |
dse$y <- temp | |
temp <- dvi$x | |
dvi$x <- dvi$y | |
dvi$y <- temp | |
temp <- dve$x | |
dve$x <- dve$y | |
dve$y <- temp | |
temp <- xlim | |
xlim <- ylim | |
ylim <- temp | |
mar <- c(5,0,0,2) | |
} | |
par(mar=mar) | |
plot(1,1, pch="", xlim=xlim, ylim=ylim, bty="n", xaxt="n", yaxt="n", xlab="", ylab="") | |
lines(dse$x, dse$y) | |
polygon(dse$x, dse$y, col=t_col(cse)) | |
lines(dve$x, dve$y) | |
polygon(dve$x, dve$y, col=t_col(cve)) | |
lines(dvi$x, dvi$y) | |
polygon(dvi$x, dvi$y, col=t_col(cvi)) | |
} | |
t_col <- function(color, percent = 50) { | |
# color = color name | |
# percent = % transparency | |
# name = an optional name for the color | |
## Get RGB values for named color | |
rgb.val <- col2rgb(color) | |
## Make new color using input color as base and alpha set by transparency | |
t.col <- rgb(rgb.val[1], rgb.val[2], rgb.val[3], | |
max = 255, | |
alpha = (100-percent)*255/100) | |
return(t.col) | |
} | |
# plotting code | |
# split for convenience | |
setosa <- iris[iris$Species=="setosa",] | |
versicolor <- iris[iris$Species=="versicolor",] | |
virginica <- iris[iris$Species=="virginica",] | |
# colours | |
cse <- "pink2" | |
cve <- "palegreen3" | |
cvi <- "dodgerblue1" | |
# scatter colours | |
scat.cols <- c(rep(cse, nrow(setosa)), rep(cve, nrow(versicolor)), rep(cvi, nrow(virginica))) | |
# min and max | |
minsl <- min(iris$Sepal.Length) | |
minsw <- min(iris$Sepal.Width) | |
maxsl <- max(iris$Sepal.Length) | |
maxsw <- max(iris$Sepal.Width) | |
png("test.png", height=1200, width=1200) | |
# layout | |
layout(matrix(c(1:4), nrow=2, byrow=TRUE), widths=c(0.80,0.20), heights=c(0.20,0.80)) | |
# top histogram | |
plot.histograms("Sepal.Length", xlim=c(minsl, maxsl)) | |
# empty | |
plot(1:10,1:10,xlim=xlim, ylim=ylim, bty="n", xaxt="n", yaxt="n", xlab="", ylab="") | |
# main scatterplot | |
par(mar=c(5,5,0,0), xaxs="r") | |
plot(iris$Sepal.Length, iris$Sepal.Width, xlim=c(minsl,maxsl), ylim=c(minsw, maxsw), col=scat.cols, pch=16, cex=4, xlab="Sepal.Length", ylab="Sepal.Width", cex.axis=2, cex.lab=2) | |
# right histogram | |
plot.histograms("Sepal.Width", xlim=c(minsw, maxsw), flip=TRUE) | |
# close PNG device | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment