# # # Sources include: # http://www.r-bloggers.com/ggplot2-version-of-figures-in-%E2%80%9C25-recipes-for-getting-started-with-r%E2%80%9D/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+RBloggers+%28R+bloggers%29 # Generally used without permission but withthe hope of futrher sharing the examples. plot(cars) library(ggplot2) ggplot(cars,aes(speed,dist))+geom_point() # Bar Chart heights <- tapply(airquality$Temp, airquality$Month, mean) par(mfrow=c(1,2)) barplot(heights) barplot(heights, main="Mean Temp. by Month", names.arg=c("May", "Jun", "Jul", "Aug", "Sep"), ylab="Temp (deg. F)") library(gridExtra) heights=ddply(airquality,.(Month), mean) heights$Month=as.character(heights$Month) p1 <- ggplot(heights, aes(x=Month,weight=Temp))+ geom_bar() p2 <- ggplot(heights, aes(x=factor(heights$Month, labels=c("May", "Jun", "Jul", "Aug", "Sep")), weight=Temp))+ geom_bar()+ opts(title="Mean Temp. By Month") + xlab("") + ylab("Temp (deg. F)") grid.arrange(p1,p2, ncol=2) # Box Plot y <- c(-5, rnorm(100), 5) boxplot(y) ggplot()+geom_boxplot(aes(x=factor(1),y=y))+xlab("")+ylab("") # Creating a Histogram data(Cars93, package="MASS") par(mfrow=c(1,2)) hist(Cars93$MPG.city) hist(Cars93$MPG.city, 20) p <- ggplot(Cars93, aes(MPG.city)) p1 <- p + geom_histogram(binwidth=diff(range(Cars93$MPG.city))/5) p2 <- p + geom_histogram(binwidth=diff(range(Cars93$MPG.city))/20) grid.arrange(p1,p2, ncol=2) # Diagnosing a Linear Regression data(iris) m = lm( Sepal.Length ~ Sepal.Width, data=iris) par(mfrow=c(2,2)) plot(m) r <- residuals(m) yh <- predict(m) scatterplot <- function(x,y, title="", xlab="", ylab="") { d <- data.frame(x=x,y=y) p <- ggplot(d, aes(x=x,y=y)) + geom_point() + opts(title=title) + xlab(xlab) + ylab(ylab) return(p) } p1 <- scatterplot(yh,r, title="Residuals vs Fitted", xlab="Fitted values", ylab="Residuals") p1 <- p1 +geom_hline(yintercept=0)+geom_smooth() s <- sqrt(deviance(m)/df.residual(m)) rs <- r/s qqplot <- function(y, distribution=qnorm, title="Normal Q-Q", xlab="Theretical Quantiles", ylab="Sample Quantiles") { require(ggplot2) x <- distribution(ppoints(y)) d <- data.frame(x=x, y=sort(y)) p <- ggplot(d, aes(x=x, y=y)) + geom_point() + geom_line(aes(x=x, y=x)) + opts(title=title) + xlab(xlab) + ylab(ylab) return(p) } p2 <- qqplot(rs, ylab="Standardized residuals") sqrt.rs <- sqrt(abs(rs)) p3 <- scatterplot(yh,sqrt.rs, title="Scale-Location", xlab="Fitted values", ylab=expression(sqrt("Standardized residuals"))) p3 <- p3 + geom_smooth() hii <- lm.influence(m, do.coef = FALSE)$hat p4 <- scatterplot(hii,rs) p4 <- p4+ geom_hline(yintercept=0)+ geom_smooth() + geom_text(aes(x=min(hii)+diff(range(hii))*0.3, y=min(rs)+diff(range(rs))*0.04, label="-- Cook's distance", size=3))+ opts(legend.position="none") grid.arrange(p1,p2,p3,p4, ncol=2) par(mfrow=c(1,1)) ######################################################################## # Star and Network Plot require(network) # network require(sna) # plot.network require(Hmisc) # largest.empty # Create some data. con <- textConnection("name,age,jobyrs,inc,saved,int,chld,addryrs Adam,21,1,46,110,3.9,2.62,16.46 Ben,32,6,82,110,3.9,2.875,17.02 Chris,22,2,38,93,3.85,2.32,18.61 David,46,16,158,110,3.08,3.215,19.44 Elvin,58,4,110,175,3.15,3.44,17.02 Fred,18,1,25,105,2.76,3.46,20.22 Greg,23,1,60,245,3.21,3.57,15.84 Henry,44,12,46,62,3.69,3.19,20 Ivan,32,2,40,95,3.92,3.15,22.9 James,29,5,67,123,3.92,3.44,18.3 Kevin,27,10,67,123,3.92,3.44,18.9 Luke,46,18,75,180,3.07,4.07,17.4") nodelist <-read.csv(con) close( con ) con <- textConnection("from,to Adam,Ben Adam,Chris Chris,David Chris,Elvin David,Fred Fred,Greg Greg,Henry Henry,James Henry,Ivan James,Kevin Ivan,Kevin Kevin,Luke") edgelist <- read.csv(con) close( con ) nodes <- levels(as.factor(nodelist[[1]])) # Create a matrix to represent the network. m <- matrix(data = 0, nrow=length(nodes), ncol=length(nodes)) rownames(m) <- colnames(m) <- nodes apply(edgelist, 1, function(x) m[x[[1]], x[[2]]] <<- 1) graph <- network(m, matrix.type="adjacency") # Now plot the network, without the nodes. par(xpd=TRUE) xy <- plot(graph, vertex.cex=5, vertex.col="white", vertex.border=0) # Get the some other data from the nodes and generate a plot for # each node and place them onto the network. Include a Key at some # empty space. kl <- largest.empty(xy[,1], xy[,2], 2, 2) stars(nodelist[-1], labels=nodelist[[1]], locations=xy, draw.segments=TRUE, key.loc=c(kl$x, kl$y), add=TRUE) #------------------------------------------------------------------------ # Now plot the network but with a hist at each node. par(xpd=TRUE) xy <- plot(graph, vertex.cex=5, vertex.col="white", vertex.border=0) for (i in 1:nrow(xy)) { subplot(hist(rnorm(100), col=rainbow(10), main="", axes=FALSE, xlab="", ylab=""), xy[i, 1], xy[i, 2], size=c(0.4,0.4)) text(xy[i, 1]+0.8, xy[i, 2]+0.8, nodelist[[1]][i]) }