R的plot 既能夠繪製點,也能夠繪製直方圖,還能夠繪製箱線圖,感受的確很智能。html
> b <- c("P", "P", "P" ,"Q", "Q", "Q") > c <- c("TQ","AQ","CQ","BQ","XQ", "XQ") > a <- c("A","B","C","D","E","F") > ct <- c(6,7,3,6,1,9) > ca <- data.frame(a,b,c, ct) > plot(ca$a, ca$b, yaxt="n", axes=F) > plot(ca$b, ca$c, yaxt="n", axes=F) > plot(ca$b, ca$ct, yaxt="n") > plot(ca$a, ca$ct, yaxt="n") ## 第二個例子 > r a b 1 1.0 1.00 2 1.5 1.25 3 2.0 1.50 4 2.5 1.75 5 3.0 2.00 6 3.5 2.25 7 4.0 2.50 > barplot(as.matrix(r), col=rainbow(9)) > barplot(as.matrix(cumsum(prop.table(r)))) > barplot(as.matrix(prop.table(r))) > mr <- as.matrix(r) > prop.table( mr, margin =2) a b [1,] 0.05714286 0.08163265 [2,] 0.08571429 0.10204082 [3,] 0.11428571 0.12244898 [4,] 0.14285714 0.14285714 [5,] 0.17142857 0.16326531 [6,] 0.20000000 0.18367347 [7,] 0.22857143 0.20408163 > barplot(prop.table( mr, margin =2)) ## 第三個例子 > data <- read.table("output.txt", header=T, row.names = 1) > mt <- t(as.matrix(data)) > head(mt) 0-0.025 0.025-0.05 0.05-0.075 0.075-0.12 0.12-0.2 0.2-0.5 0.5-1.5 1.5-3 A0.6 357616 597 77 18 0 0 0 0 A0.4 189028 1089 101 38 2 0 0 0 A0.25 255902 2422 269 50 5 4 0 0 A0.1 130322 1554 192 34 10 0 0 0 A0 0 0 0 0 0 0 0 0 3-5 5-10 A0.6 0 0 A0.4 0 0 A0.25 0 0 A0.1 0 0 A0 0 0 > pdf("TT1.pdf") > barplot(prop.table( mt, margin = 2), col=heat.colors(5)) > dev.off() null device 1
另外發現一個繪製熱圖的 functionide
# ----- Define a function for plotting a matrix ----- # myImagePlot <- function(x, ...){ min <- min(x) max <- max(x) yLabels <- rownames(x) xLabels <- colnames(x) title <-c() # check for additional function arguments if( length(list(...)) ){ Lst <- list(...) if( !is.null(Lst$zlim) ){ min <- Lst$zlim[1] max <- Lst$zlim[2] } if( !is.null(Lst$yLabels) ){ yLabels <- c(Lst$yLabels) } if( !is.null(Lst$xLabels) ){ xLabels <- c(Lst$xLabels) } if( !is.null(Lst$title) ){ title <- Lst$title } } # check for null values if( is.null(xLabels) ){ xLabels <- c(1:ncol(x)) } if( is.null(yLabels) ){ yLabels <- c(1:nrow(x)) } layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(1,1)) # Red and green range from 0 to 1 while Blue ranges from 1 to 0 ColorRamp <- rgb( seq(0,1,length=256), # Red seq(0,1,length=256), # Green seq(1,0,length=256)) # Blue ColorLevels <- seq(min, max, length=length(ColorRamp)) # Reverse Y axis reverse <- nrow(x) : 1 yLabels <- yLabels[reverse] x <- x[reverse,] # Data Map par(mar = c(3,5,2.5,2)) p_w_picpath(1:length(xLabels), 1:length(yLabels), t(x), col=ColorRamp, xlab="", ylab="", axes=FALSE, zlim=c(min,max)) if( !is.null(title) ){ title(main=title) } axis(BELOW<-1, at=1:length(xLabels), labels=xLabels, cex.axis=0.7) axis(LEFT <-2, at=1:length(yLabels), labels=yLabels, las= HORIZONTAL<-1, cex.axis=0.7) # Color Scale par(mar = c(3,2.5,2.5,2)) p_w_picpath(1, ColorLevels, matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1), col=ColorRamp, xlab="",ylab="", xaxt="n") layout(1) } # ----- END plot function ----- #
ref:http://www.phaget4.org/R/p_w_picpath_matrix.htmlhtm