Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ggplot2 A cool way for creating plots in R Maria Novosolov.

Similar presentations


Presentation on theme: "Ggplot2 A cool way for creating plots in R Maria Novosolov."— Presentation transcript:

1 ggplot2 A cool way for creating plots in R Maria Novosolov

2 Your scientific graphic options

3 Why not only Excel? Excel is relatively limited in its support of scientific graphing It’s options provide limited control over the output Limited selection of graph types Limited number of data points that can be plotted

4 Why ggplot2? Helps creating good looking graphs for publishing The package format allows manipulation on every step of the way Create new graphics tailored for your problem Easy to create complex plots

5 The idea behind ggplot2 Combining all the good and leaving out all the bad of all R packages for plotting

6 Some examples

7 Attention!! It doesn’t suggest what graphics you should use to answer the questions you are interested in

8 The basics of ggplot2 Works as a layer by layer system Based on the Grammar of Graphics (Wilkinson, 2005) The plots can be edited after they are ready

9 How it works Two options of working with the package 1. Easy – using qplot() (=quick plot) 2. Complicated – using ggplot

10 qplot Basically very similar to the function plot() in R The first two arguments to qplot() are x and y An optional data argument (recommended) Exp: qplot(x, y, data = mydata)

11 qplot You can change color, size, shape and transparency (all of this is called aesthetics) This can either be set to the default aesthetics or manually using the function I(). You can decide on the type of the plot (scatterplot, box-plot, histogram) by using “geom” function.

12 ggplot

13 Works by creating the plot layer by layer A layer is composed of four parts: data and aesthetic mapping a statistical transformation (stat) a geometric object (geom) and a position adjustment.

14 How it works A plot is made up of multiple layers. A layer consists of data, a set of mappings between variables and aesthetics, a geometric object and a statistical transformation. Scales control the details of the mapping. All components are independent and reusable.

15 Layers Iteratively update a plot Change a single feature at a time Think about the high level aspects of the plot in isolation Instead of choosing a static type of plot, create new types of plots on the fly Cure against immobility Developers can easily develop new layers without affecting other layers

16 Lets dive into the syntax

17 Anatomy of a plot 1. Data – the data name that you want to present 2. Aesthetics – the visualization of the plot 3. Geom – geometric objects. Represent what you see in the plot: points, lines, polygon etc. 4. Themes – control presentation of non- data elements 5. Stat – statistical transformations 6. Scale – intersect between the data and the aesthetics.

18 Anatomy of a plot 7. Coord – coordinate system. describes how data coordinates are mapped to the plane of the graphic. 8. Position adjustments 9. Facet - describes how to break up the data into subsets and how to display those subsets as small multiples.

19 Anatomy of a plot 1. Data * 2. Aesthetic mapping * 3. Geometric Object * 4. Statistical transformation 5. Scales 6. Facets 7. Themes 8. Position adjustments * Mandatory layers

20 1. Data In ggplot2, we always explicitly specify the data qplot(displ, hwy, data = mpg)

21 2. Aesthetic Mapping In ggplot land aesthetic means "something you can see“ Includes: Position (i.e., on the x and y axes) Color ("outside" color) Fill ("inside" color) Shape (of points) Line type Size Aesthetic mappings are set with the aes() function

22 3. Geometric Object A geom can only display certain aesthetics A plot must have at least one geom; there is no upper limit

23

24

25 Example ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point() Aesthetic Mappings The data.frame to plot What geom to use in plotting

26 4. Statistical Transformations Each geom has a default statistic, but these can be changed Some plot types (such as scatterplots) do not require transformations–each point is plotted at x and y coordinates equal to the original value Other plots, such as boxplots, histograms, prediction lines etc. require statistical transformations

27 5. Scales Control mapping from data to aesthetic attributes One scale per aesthetic

28 5. Scales In ggplot2 scales include: position color and fill size shape line type Modified with scale_ _

29 5. Scales Common Scale Arguments: name: the first argument gives the axis or legend title limits: the minimum and maximum of the scale breaks: the points along the scale where labels should appear labels: the labels that appear at each break

30 5. Scales Partial combination matrix of available scales

31 6. Faceting Lay out multiple plots on a page Split data into subsets Plot subsets into different panels

32 6. Faceting Faceting is ggplot2 parlance for small multiples The idea is to create separate graphs for subsets of data ggplot2 offers two functions for creating small multiples: facet_wrap() : define subsets as the levels of a single grouping variable facet_grid() : define subsets as the crossing of two grouping variables Facilitates comparison among plots, not just of geoms within a plot

33 Facet Types 2D grid of panels: 1D ribbon of panels wrapped into 2D:

34 Faceting Formula

35 Scales in Facets

36 7. Themes The ggplot2 theme system handles non-data plot elements such as Axis labels Plot background Facet label background Legend appearance Specific theme elements can be overridden using theme()

37 8. Position adjustments Tweak positioning of geometric objects Avoid overlaps

38 Exp: position_jitter Avoid overplotting by jittering points ggplot(overplotted, aes(x,y)) + geom_point(position=position_jitter (w=0.1, h=0.1))

39 ggplot Creating a plot -Adding data -Mapping aesthetics Adding layers* * In order to start seeing the plot you need to first declare a geom Exp: p <- ggplot(diamonds, aes(carat, price, colour = cut)) Exp: p <- p + layer(geom = "point")

40 Summary Allows creating high quality plots Many options Many blogs and webpages explaining how to do different plots in ggplot Disadvantage – takes time to learn the grammar There is a book: ggplot2: Elegant Graphics for Data Analysis

41 Useful links http://ggplot2.org http://wiki.stdout.org/rcookbook/Graphs/ http://www.ceb-institute.org/bbs/wp- content/uploads/2011/09/handout_ggplot2. pdf http://www.ceb-institute.org/bbs/wp- content/uploads/2011/09/handout_ggplot2. pdf http://yeroon.net/ggplot2/


Download ppt "Ggplot2 A cool way for creating plots in R Maria Novosolov."

Similar presentations


Ads by Google