############################################## ## Une nouvelle génération de graphiques : ## ## introduction à ggplot2 ## ## Jérôme SUEUR ## ## Muséum national d'Histoire naturelle ## ## semin-R, 20/05/2010 ## ## http://rug.mnhn.fr/semin-r ## ############################################## ################################################### ### chunk number 2: diamonds ################################################### library(ggplot2) data(diamonds) class(diamonds) diamonds <- diamonds[sample(nrow(diamonds), 500), ] ################################################### ### chunk number 4: carat ################################################### print(qplot(carat, price, data= diamonds)) ################################################### ### chunk number 6: carat-col-size-shape ################################################### print(qplot(carat, price, data= diamonds, colour = I("red"), size=I(2), shape = I(15), alpha = I(1/5))) ################################################### ### chunk number 8: boxplot ################################################### print(qplot(color, price/carat, data=diamonds, geom="boxplot")) ################################################### ### chunk number 10: geom-compose1 ################################################### print(qplot(carat, price, data = diamonds, geom = c("point", "smooth"))) ################################################### ### chunk number 12: geom-compose2 ################################################### data(movies) print(qplot(mpaa, rating, data=movies, geom=c("jitter", "boxplot"))) ################################################### ### chunk number 14: carat-cat-col-point ################################################### print(qplot(carat, price, data = diamonds, colour = color)) ################################################### ### chunk number 16: carat-cat-col-hist ################################################### print(qplot(carat, data = diamonds, geom = "histogram", fill = color)) ################################################### ### chunk number 18: carat-cat-col-dens ################################################### print(qplot(carat, data = diamonds, geom = "density", fill = color, alpha=I(1/5))) ################################################### ### chunk number 20: carat-cat-forme-point ################################################### print(qplot(carat, price, data = diamonds, shape = cut)) ################################################### ### chunk number 22: facets-ex1 ################################################### print(qplot(carat, data = diamonds, facets = color ~ ., geom = "histogram")) ################################################### ### chunk number 24: facets-ex2 ################################################### print(qplot(carat, data = diamonds, facets = . ~ color, geom = "histogram")) ################################################### ### chunk number 25: ggplot1 ################################################### p <- ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) summary(p) ################################################### ### chunk number 27: ggplot2 ################################################### print(p + geom_point() + geom_smooth(method="lm")) ################################################### ### chunk number 28: geom-line ################################################### data(mtcars) coef <- coef(lm(mpg ~ wt, data = mtcars)) ################################################### ### chunk number 30: geom-line ################################################### p <- ggplot(mtcars, aes(wt, mpg)) print(p + geom_point() + geom_abline(intercept = coef[1], slope = coef[2], colour="red", size=1.25, linetype=5)) ################################################### ### chunk number 32: annotation ################################################### p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() print(p + geom_text(aes(wt,mpg, label=rownames(mtcars)), hjust=-0.1, size=2.5, colour=alpha("blue",0.5)) + xlab("Poids") + ylab("Consommation")) ################################################### ### chunk number 34: position1 ################################################### print(ggplot(diamonds, aes(x=color, fill=cut)) + geom_bar(position="dodge")) ################################################### ### chunk number 36: facets2 ################################################### print(qplot(mpg, wt, data=mtcars) + facet_grid(cyl ~ vs, margins=TRUE)) ################################################### ### chunk number 38: facets2b ################################################### print(qplot(mpg, wt, data=mtcars) + geom_smooth(aes(colour=factor(am)), method="lm") + facet_grid(cyl ~ .))