# Simple Bar Plot mtcars attach(mtcars) counts <- table(gear) counts barplot(counts, main="Car Distribution", xlab="Number of Gears") barplot(counts, main="Car Distribution", xlab="Number of Gears", border = "red") barplot(counts, main="Car Distribution", col=c("red", "blue", "green"), names.arg=c("3 Gears", "4 Gears", "5 Gears")) # Simple Horizontal Bar Plot with Added Labels barplot(counts, main="Car Distribution", horiz=TRUE, names.arg=c("3 Gears", "4 Gears", "5 Gears")) count1<-counts/32*100 count1 barplot(count1, main="Car Distribution", horiz=TRUE, names.arg=c("3 Gears", "4 Gears", "5 Gears"), xlim=c(0,50)) # Grouped Bar Plot counts <- table(vs, gear) counts barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = rownames(counts), beside=TRUE) # Conditional distribution count1<-counts count1[1,]<-counts[1,]/sum(counts[1,]) count1[2,]<-counts[2,]/sum(counts[2,]) count1 barplot(count1, main="Car Distribution of Gears by VS", xlab="Number of Gears", col=c("darkblue","red"), legend = c("V", "S"), beside=TRUE) # Stacked Bar Plot with Colors and Legend counts <- table(vs, gear) counts barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = rownames(counts)) # Fitting Labels par(las=2) # make label text perpendicular to axis par(mar=c(5,8,4,2)) # increase y-axis margin. counts <- table(gear) barplot(counts, main="Car Distribution", horiz=TRUE, names.arg=c("3 Gears", "4 Gears", "5 Gears"), cex.names=0.8) # Simple Pie Chart slices <- c(10, 12, 4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pie(slices, labels = lbls, main="Pie Chart of Countries") # Pie Chart with Percentages slices <- c(10, 12, 4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pct <- round(slices/sum(slices)*100) lbls <- paste(lbls, pct) # add percents to labels lbls <- paste(lbls,"%",sep="") # ad % to labels pie(slices,labels = lbls, col=rainbow(length(lbls)), main="Pie Chart of Countries") pie(slices,labels = lbls, cex=2, col=rainbow(length(lbls)), main="Pie Chart of Countries", cex.main=2) # Pie Chart from data frame with Appended percentages slices <- c(10, 12, 4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pct <- round(slices/sum(slices)*100) lbls <- paste(lbls, "\n", pct, sep="") lbls <- paste(lbls, "%", sep="") pie(slices, labels = lbls, col=rainbow(length(lbls)), main="Pie Chart of Countries\n (with percentage)") # 3D Exploded Pie Chart install.packages("plotrix") library(plotrix) slices <- c(10, 12, 4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pie3D(slices,labels=lbls,explode=0.1, main="Pie Chart of Countries ") ------------------------------------------------------------------------- doughnut <- function (x, labels = names(x), edges = 200, outer.radius = 0.8, inner.radius=0.6, clockwise = FALSE, init.angle = if (clockwise) 90 else 0, density = NULL, angle = 45, col = NULL, border = FALSE, lty = NULL, main = NULL, ...) { if (!is.numeric(x) || any(is.na(x) | x < 0)) stop("'x' values must be positive.") if (is.null(labels)) labels <- as.character(seq_along(x)) else labels <- as.graphicsAnnot(labels) x <- c(0, cumsum(x)/sum(x)) dx <- diff(x) nx <- length(dx) plot.new() pin <- par("pin") xlim <- ylim <- c(-1, 1) if (pin[1L] > pin[2L]) xlim <- (pin[1L]/pin[2L]) * xlim else ylim <- (pin[2L]/pin[1L]) * ylim plot.window(xlim, ylim, "", asp = 1) if (is.null(col)) col <- if (is.null(density)) palette() else par("fg") col <- rep(col, length.out = nx) border <- rep(border, length.out = nx) lty <- rep(lty, length.out = nx) angle <- rep(angle, length.out = nx) density <- rep(density, length.out = nx) twopi <- if (clockwise) -2 * pi else 2 * pi t2xy <- function(t, radius) { t2p <- twopi * t + init.angle * pi/180 list(x = radius * cos(t2p), y = radius * sin(t2p)) } for (i in 1L:nx) { n <- max(2, floor(edges * dx[i])) P <- t2xy(seq.int(x[i], x[i + 1], length.out = n), outer.radius) polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i], border = border[i], col = col[i], lty = lty[i]) Pout <- t2xy(mean(x[i + 0:1]), outer.radius) lab <- as.character(labels[i]) if (!is.na(lab) && nzchar(lab)) { lines(c(1, 1.05) * Pout$x, c(1, 1.05) * Pout$y) text(1.1 * Pout$x, 1.1 * Pout$y, labels[i], xpd = TRUE, adj = ifelse(Pout$x < 0, 1, 0), ...) } ## Add white disc Pin <- t2xy(seq.int(0, 1, length.out = n*nx), inner.radius) polygon(Pin$x, Pin$y, density = density[i], angle = angle[i], border = border[i], col = "white", lty = lty[i]) } title(main = main, ...) invisible(NULL) } #------------------------------------------------------------------------- slices <- c(10, 12, 4, 16, 8) lbls <- c("US", "UK", "Australia", "Germany", "France") pct <- round(slices/sum(slices)*100) lbls <- paste(lbls, pct) # add percents to labels lbls <- paste(lbls,"%",sep="") # ad % to labels doughnut(slices, lbls, main="Pie Chart of Countries\n (with percentage)") #-------------------------------------------------------------------------- distance <- c(15, 16, 10, 8, 4, 2, 35, 7, 5, 14, 14, 0, 35, 31, 0, 10, 36, 16, 5, 3, 22, 7, 55, 24, 42, 29, 2, 4, 14, 29, 17, 1, 3, 17, 0, 10, 45, 10, 9, 22, 11, 16, 10, 22, 48, 18, 41, 4, 43, 13, 7, 7, 8, 9, 18, 2, 5, 6, 48, 28, 9, 0, 54, 14, 21, 23, 24, 35, 14, 4, 10, 18, 14, 21, 8, 14, 10, 6, 11, 22, 1, 18, 30, 39) distance par(mfrow=c(2,2)) hist(distance, xlab=" ", ylab="frequency", col="gray90") hist(distance, xlab=" ", main="breaks =22", ylab="", breaks=22 ,col="dark green") hist(distance, xlab="distance(m)", ylab="frequency", breaks = seq(0,60,5), col="gray90") hist(distance, xlab="distance(m)", main="breaks =6", ylab="", breaks=6, col="gray90", las=2) par(mfrow=c(1,1)) data(PlantGrowth) head(PlantGrowth) dim(PlantGrowth) attach(PlantGrowth) par(mfrow = c(1, 3)) #--------------------------------------------------------------------- a <- hist(weight[group == 'ctrl'], xlim = c(3, 6.5), ylim = c(0, 4), xlab = '', main = 'control', ylab = 'frequency', col = 'gray90') b <- hist(weight[group == 'trt1'], xlim = c(3, 6.5), ylim = c(0, 4), xlab = 'weight', ylab = '', main = 'treatment 1', col = 'gray90') c <- hist(weight[group == 'trt2'], xlim = c(3, 6.5), ylim = c(0, 4), xlab = '', ylab = '', main = 'treatment 2', col = 'gray90') a #---------------------------------------------------------------------- densa <- density(weight[group == 'ctrl']) densa hist(weight[group == 'ctrl'], xlim=range(densa$x), xlab = '', main = 'control', ylab = 'density', col = 'gray90', probability = TRUE) lines(densa) densb <- density(weight[group == 'trt1']) densb hist(weight[group == 'trt1'], xlim=range(densb$x), xlab = 'weight', ylab = '', main = 'treatment 1', col = 'gray90', probability = TRUE) lines(densb) densc <- density(weight[group == 'trt2']) densc hist(weight[group == 'trt2'], xlim=range(densc$x), xlab = '', ylab = '', main = 'treatment 2', col = 'gray90', probability = TRUE) lines(densc) #-------------------------------------------------------------------- mtcars attach(mtcars) # Simple Histogram hist(mpg) # Colored Histogram with Different Number of Bins hist(mpg, breaks=12, col="red") # Add a Normal Curve (Thanks to Peter Dalgaard) x <- mpg h<-hist(x, breaks=10, col="red", xlab="Miles Per Gallon", main="Histogram with Normal Curve") xfit<-seq(min(x),max(x),length=40) yfit<-dnorm(xfit,mean=mean(x),sd=sd(x)) yfit <- yfit*diff(h$mids[1:2])*length(x) lines(xfit, yfit, col="blue", lwd=2) #--------------------------------------------------------------------- par(mfrow=c(1,1)) mtcars attach(mtcars) boxplot(mpg, horizontal=TRUE, xlab="Miles Per Gallon") boxplot(mpg, horizontal=TRUE, xlab="Miles Per Gallon", col="pink") # Boxplot of MPG by Car Cylinders boxplot(mpg~cyl,data=mtcars, main="Car Milage Data", xlab="Number of Cylinders", ylab="Miles Per Gallon") boxplot(mpg~cyl,data=mtcars, main="Car Milage Data", xlab="Number of Cylinders", ylab="Miles Per Gallon", col="gold") boxplot(mpg~cyl, main="Milage by Car Weight", yaxt="n", xlab="Milage", horizontal=TRUE, col=terrain.colors(3)) legend("topright", inset=.05, title="Number of Cylinders", c("4","6","8"), fill=terrain.colors(3), horiz=TRUE) boxplot(mpg~cyl*am, data=mtcars, col=(c("gold","darkgreen","red")), main="Miles Per Gallon", xlab="cylinders and transmission") #--------------------------------------------------------------------- rad<-read.csv("c:/RData/radiation.csv") rad names(rad) attach(rad) par(mfrow=c(1,2)) qqnorm(rad[,1], ylab="Door closed radiation Quanties", main="Normal Q-Q Plot(door closed)") # Produce a Q-Q plot for radiation data (door closed) qqline(rad[,1]) qqnorm(rad[,2], ylab="Door open radiation Quanties", main="Normal Q-Q Plot(door open)") # Produce a Q-Q plot for radiation data (door open) qqline(rad[,2]) srad<-sqrt(rad) # 將兩變數都作開根號 轉換 qqnorm(srad[,1], ylab="sqrt(door closed radiation) quantiles", main="Normal Q-Q Plot(door closed)") qqline(srad[,1]) qqnorm(srad[,2], ylab="sqrt(door open radiation) quantiles", main="Normal Q-Q Plot(door open)") qqline(srad[,2]) #--------------------------------------------------------------------- # Install iplots install.packages("iplots",dep=TRUE) # Create some linked plots library(iplots) cyl.f <- factor(mtcars$cyl) gear.f <- factor(mtcars$factor) attach(mtcars) ihist(mpg) # histogram ibar(carb) # barchart iplot(mpg, wt) # scatter plot ibox(mtcars[c("qsec","disp","hp")]) # boxplots ipcp(mtcars[c("mpg","wt","hp")]) # parallel coordinates imosaic(cyl.f,gear.f) # mosaic plot #----------------------------------------------------------------------