# Separation of statistcs and geometric elements ----------------------------- p <- ggplot(diamonds, aes(x=price)) p + geom_histogram() p + stat_bin(geom="area") p + stat_bin(geom="point") p + stat_bin(geom="line") p + geom_histogram(aes(fill = clarity)) p + geom_histogram(aes(y = ..density..)) ##################### d = read.csv("hsb.csv") wd = d[,c(1,7:11)] # we'll need the long format as well... library("reshape2") long.d = melt(wd,id.vars="id") # Let's try to make some graphs: library(ggplot2) g = ggplot(d,aes(y=write,x=factor(female))) g+geom_boxplot() g+geom_boxplot(fill="red") # you might want to use the color/fill to split the groups, in which case you need to create a dummy x axis: g = ggplot(d,aes(y = write, x=1,fill=factor(female))) g+geom_boxplot() # you may want to supress some of the default x-axis properties in this case: g+geom_boxplot()+scale_x_continuous("groups",breaks=NULL) # now, what if we wanted to compare the distribution of write and read and science? # apparently they are different variables.... what would be the y axis? and the grouping? # The answer: LONG FORMAT g = ggplot(long.d,aes(y=value,x=variable)) g1 = g+geom_boxplot() g1 # if we want to fill, we can add an aesthetic layer dynamically g1+aes(fill=variable) # but we don't want the legend this time, so we can turn it off: g1+aes(fill=variable)+guides(fill=FALSE) # we can dynamically change the "theme" of the graph. there are several builtin themes g1+theme_bw() g1+theme_minimal() ################ # contour # Generate data library(reshape2) # for melt volcano3d <- melt(volcano) names(volcano3d) <- c("x", "y", "z") # Basic plot v <- ggplot(volcano3d, aes(x, y, z = z)) v + stat_contour() v + geom_tile(aes(fill = z)) + stat_contour() v + geom_tile(aes(fill = z)) + scale_fill_gradient(low="green",high="brown") # polar plots ggplot(diamonds,aes(x = "", fill=clarity)) + geom_bar(width = 1) + coord_polar (theta="y") ggplot(diamonds,aes(x = "", fill=clarity)) + geom_bar(width = 1) + coord_polar (theta="x") ########################## # lots of other examples and a description of all elements and functions http://docs.ggplot2.org/current/