Visualization and Statistics
Type | R code | Plot |
---|---|---|
line | plot(c~b, type = "l") | ![]() |
box plot | boxplot(a,b,c,col=cm.colors(8)) | ![]() |
scatter plot | plot(a~b) | ![]() |
pairwise scatter plot | pairs(x) | ![]() |
trend line with error envelop | library(ggplot2); CC <- read.table("http://www.sr.bham.ac.uk/~ajrs/papers/sanderson06/mean_Tprofile-CC.txt", header=TRUE) nCC <- read.table("http://www.sr.bham.ac.uk/~ajrs/papers/sanderson06/mean_Tprofile-nCC.txt", header=TRUE) CC$type <- "Cool core"; nCC$type <- "Non-cool core"; A <- rbind(CC, nCC) ggplot(data=A, aes(x=r.r500, y=sckT, ymin=sckT.lo, ymax=sckT.up, fill=type, linetype=type)) + geom_line() + geom_ribbon(alpha=0.5) + scale_x_log10() + scale_y_log10() ref http://www.r-graph-gallery.com/104-plot-lines-with-error-envelopes-ggplot2/ | ![]() |
violin plot | library(vioplot); with(data , vioplot( value[treatment=="A"] , value[treatment=="B"], col=rgb(0.1,0.4,0.7,0.7) , names=c("A","B") )) | ![]() |
bubble plot | symbols(aa[,1], aa[,6], circles=aa[,10]/5, inches=0.10, fg="white", bg=" darkorchid4") | ![]() |
two y axes | par(mar=c(5,4,4,5)+.1) plot(x,y1,type="l",col="red") par(new=TRUE) plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="") axis(4) mtext("y2",side=4,line=3) legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2")) | ![]() |
circular tree plot | library(dendextend) library(circlize) hc <- hclust(dist(datasets::mtcars)) dend <- as.dendrogram(hc) par(mar = rep(0,4)) circlize_dendrogram(dend, labels_track_height = NA, dend_track_height = .4) | ![]() |
heatmap | library(gplots);library(RColorBrewer); hc <- hclust(dist(datasets::mtcars)) heatmap.2(datasets::mtcars,hclustfun = function(x)hclust(x,method = "average"), dendrogram = 'both') | ![]() |
density | library(rattle); library(ggplot2) cities <- c("Canberra", "Melbourne", "Sydney") ds <- subset(weatherAUS, Location cities & ! is.na(MaxTemp)) p <- ggplot(ds, aes(MaxTemp, colour=Location, fill=Location)) p <- p + geom_density(alpha=0.55) p ref http://www.r-graph-gallery.com/21-distribution-plot-using-ggplot2/ | ![]() |
centipede | set.seed(9001) p.value <- vector() dataf <- as.data.frame(matrix(rep(NA, 2000), nrow=100)) names(dataf) <- c(paste("V",1:19, sep=""), "y") for(i in 1:20) { dataf[,i] <- rnorm(20, 50, 10) } require (plotrix) seg<-get.segs(dataf) [[<<]centipede.plot(seg, main= "A centipede plot",vgrid=c(10)) grid(nx = 10, ny = 19, col = "gray50") ref http://rgraphgallery.blogspot.com/search/label/centepede%20plot | ![]() |
pairwise comparison | library(multcompView) model=lm( data$value ~ data$treatment ) ANOVA=aov(model) TUKEY <- TukeyHSD(x=ANOVA, 'data$treatment', conf.level=0.95) plot(TUKEY , las=1 , col="brown" ) ref http://www.r-graph-gallery.com/84-tukey-test/ | ![]() |
butterfly plot | source("/panfs/pan1.be-md.ncbi.nlm.nih.gov/devdcode/di/resource/buttfly.R") p1 = butterfly(100, 1000, title="100 x 1000") ref http://www.r-bloggers.com/the-butterfly-curve-2/ | ![]() |
3-D surface (matlab) | [X,Y] = meshgrid(-2:.2:2); Z = X .* exp(-X.^2 - Y.^2); surf(X,Y,Z) | ![]() |
polar axis | ggplot(aa,aes(x = legend.var, y = score, fill = Indicator))+ geom_bar(width=1,alpha= 0.5,stat="identity") + scale_y_continuous() + coord_polar() + theme(axis.ticks = element_blank()) | ![]() |
multiple panels | p1 <- ggplot(DF,aes(y=motif,x=y,colour=factor(group))) + geom_point() p2 <- ggplot(DF,aes(x=y,colour=factor(group),fill=factor(group))) + scale_y_continuous() + coord_polar() + geom_density(alpha=0.5) p3 <- ggplot(DF,aes(x=motif,colour=factor(group),fill=factor(group))) + geom_density(alpha=0.5) + coord_flip() grid.arrange(arrangeGrob(p2,ncol=2,widths=c(3,1)), arrangeGrob(p1,p3,ncol=2,widths=c(3,1)), heights=c(1,3)) | ![]() |
boxplot with data points | df <- ToothGrowth df$dose <- as.factor(df$dose) p <- ggplot(df, aes(x=dose, y=len)) + geom_dotplot(binaxis='y', stackdir='center') + stat_summary(fun.data="mean_sdl", mult=1, geom="crossbar", width=0.5) | ![]() |
scatterplot with error bar | ggplot(data = df,aes(x = x,y = y)) + geom_point() + geom_errorbar(aes(ymin = ymin,ymax = ymax)) + geom_errorbarh(aes(xmin = xmin,xmax = xmax)) ref http://stackoverflow.com/questions/9231702/ggplot2-adding-two-errorbars-to-each-point-in-scatterplot | ![]() |
diverging stacked bar | require(likert) data(pisaitems)data(pisaitems) items24 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST24Q'] [<<]]l24 <- likert(items24) [<<]]plot(l24) ref https://github.com/jbryer/likert/blob/master/demo/likert.R | ![]() |
scatterplot error bar polar coordinator | ggplot(data = df,aes(x = x,y = y)) + geom_point() + geom_errorbar(aes(ymin = ymin,ymax = ymax)) + coord_polar() | ![]() |
stack bar polar coordinator | ggplot(mdfr, aes(category, value, fill = variable)) + geom_bar(position="fill",width=0.8,alpha= 0.5,stat="identity") + coord_polar() + theme(axis.ticks = element_blank()) | ![]() |
two y axes II | df <- data.frame(stolpec1 = 10 * runif(10), stolpec2 = 100+10 * runif(10)) barplot(df$stolpec1,ylim=c(0,30)) par(new=TRUE) plot(1:10, df$stolpec2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="",ylim=c(80,120)) axis(4) mtext("y2",side=4,line=3) legend("topleft",col=c("grey","blue"),lty=1,legend=c("y1","y2")) | ![]() |
3D scatter plot | library("plot3D") scatter3D_fancy(x, y, z, pch = 16, ticktype = "detailed", theta = 15, d = 2, [<<]]main = "Iris data", clab = c("Petal", "Width (cm)") ) ref http://www.sthda.com/english/wiki/impressive-package-for-3d-and-4d-graph-r-software-and-data-visualization | ![]() |
3D scatter plot II | (MATLAB) stem3(z,x,y,'.','marksize',30) grid on; box on | ![]() |
3D surface with project lines | (MATLAB) surfc(X,Y,Z) grid on; box on | ![]() |