Slides adapted/stolen from Garret Grolemund/RStudio’s                    Data Visualization Courseggplot2                 ...
Getting Startedinstall.packages(c("ggplot2", "hexbin",    "ggmap", "maps", "RColorBrewer",    "scales","ReadImages"))libra...
Grammar of Graphics• Framework for describing visualized data  – Mapping data onto a coordinate system• Created by Leland ...
Your First ggplot2 Plot> library(ggplot2)> head(mpg)  manufacturer model displ year cyl      trans drv cty hwy fl class1  ...
Your First ggplot2 Plotggplot(data=mpg, aes(x=cty, y=hwy))     + geom_point()
Your First ggplot2 Plot       Thedata.frame    to plotggplot(data=mpg, aes(x=cty, y=hwy))     + geom_point()
Your First ggplot2 Plot       Thedata.frame                 Aesthetic    to plot                Mappingsggplot(data=mpg, a...
Your First ggplot2 Plot       Thedata.frame                 Aesthetic    to plot                Mappingsggplot(data=mpg, a...
Your First ggplot2 Plotlibrary(ggplot2)ggplot(data=mpg,  aes(x=cty, y=hwy))  + geom_point()
ImprovementsProblem: verbosityggplot(data=mpg,  aes(x=cty, y=hwy))  + geom_point()ggplot(mpg, aes(cty, hwy))  + geom_point()
ImprovementsProblem: OverplottingSolution: Different geomsggplot(mpg, aes(cty, hwy))  + geom_jitter()
ImprovementsProblem: OverplottingSolution: Different geomsggplot(mpg, aes(cty, hwy))  + geom_bin2d()
Colors & Groupsggplot(mpg, aes(cty, hwy,  color=drv))  + geom_jitter()
Colors & Groupsggplot(mpg,aes(drv, hwy))  + geom_boxplot()
Adding Layersggplot(mpg,aes(drv, hwy))  + geom_jitter()  + geom_boxplot()
Adding Layersggplot(mpg, aes(cty, hwy))  + geom_jitter()  + geom_smooth()
Adding Layersggplot(data, aes(TIME, Y, color=GROUP)) +        geom_line() + geom_smooth()
Facetingggplot(mpg, aes(cty, hwy)) + geom_jitter()        + facet_grid(drv ~ class)
Refining Plotsggplot(mpg, aes(cty, hwy))  + geom_jitter()  + xlab("City MPG")  + ylab("Hwy MPG")  + ggtitle("City vsHighwa...
Etc.
Etc.
Etc.
Etc.
Etc.
Etc.
Etc.
Etc.
Resources• http://docs.ggplot2.org/• Google Group• ggplot2 Book
Tech talk   ggplot2
Tech talk   ggplot2
Tech talk   ggplot2
Tech talk   ggplot2
Upcoming SlideShare
Loading in …5
×

Tech talk ggplot2

1,715
-1

Published on

A quick introduction to the ggplot2 R package by Hadley Wickham which implements the Grammar of Graphics paradigm in the R language.

Published in: Entertainment & Humor

Tech talk ggplot2

  1. 1. Slides adapted/stolen from Garret Grolemund/RStudio’s Data Visualization Courseggplot2 Jeff Allen Quantitative Biomedical Research Center 3.18.13
  2. 2. Getting Startedinstall.packages(c("ggplot2", "hexbin", "ggmap", "maps", "RColorBrewer", "scales","ReadImages"))library(ggplot2)library(hexbin)library(ggmap)library(maps)library(RColorBrewer)library(scales)library(ReadImages)
  3. 3. Grammar of Graphics• Framework for describing visualized data – Mapping data onto a coordinate system• Created by Leland Wilkinson
  4. 4. Your First ggplot2 Plot> library(ggplot2)> head(mpg) manufacturer model displ year cyl trans drv cty hwy fl class1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
  5. 5. Your First ggplot2 Plotggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()
  6. 6. Your First ggplot2 Plot Thedata.frame to plotggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()
  7. 7. Your First ggplot2 Plot Thedata.frame Aesthetic to plot Mappingsggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()
  8. 8. Your First ggplot2 Plot Thedata.frame Aesthetic to plot Mappingsggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()What geom to use in plotting
  9. 9. Your First ggplot2 Plotlibrary(ggplot2)ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()
  10. 10. ImprovementsProblem: verbosityggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()ggplot(mpg, aes(cty, hwy)) + geom_point()
  11. 11. ImprovementsProblem: OverplottingSolution: Different geomsggplot(mpg, aes(cty, hwy)) + geom_jitter()
  12. 12. ImprovementsProblem: OverplottingSolution: Different geomsggplot(mpg, aes(cty, hwy)) + geom_bin2d()
  13. 13. Colors & Groupsggplot(mpg, aes(cty, hwy, color=drv)) + geom_jitter()
  14. 14. Colors & Groupsggplot(mpg,aes(drv, hwy)) + geom_boxplot()
  15. 15. Adding Layersggplot(mpg,aes(drv, hwy)) + geom_jitter() + geom_boxplot()
  16. 16. Adding Layersggplot(mpg, aes(cty, hwy)) + geom_jitter() + geom_smooth()
  17. 17. Adding Layersggplot(data, aes(TIME, Y, color=GROUP)) + geom_line() + geom_smooth()
  18. 18. Facetingggplot(mpg, aes(cty, hwy)) + geom_jitter() + facet_grid(drv ~ class)
  19. 19. Refining Plotsggplot(mpg, aes(cty, hwy)) + geom_jitter() + xlab("City MPG") + ylab("Hwy MPG") + ggtitle("City vsHighway MPG")
  20. 20. Etc.
  21. 21. Etc.
  22. 22. Etc.
  23. 23. Etc.
  24. 24. Etc.
  25. 25. Etc.
  26. 26. Etc.
  27. 27. Etc.
  28. 28. Resources• http://docs.ggplot2.org/• Google Group• ggplot2 Book
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×