32
ggplot2 Jeff Allen Quantitative Biomedical Research Center 3.18.13 Slides adapted/stolen from Garret Grolemund/RStudio’s Data Visualization Course

Tech talk ggplot2

  • Upload
    jalle6

  • View
    118

  • Download
    3

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Tech talk   ggplot2

ggplot2

Jeff AllenQuantitative Biomedical Research Center

3.18.13

Slides adapted/stolen from Garret Grolemund/RStudio’s Data Visualization Course

Page 2: Tech talk   ggplot2

Getting Started

install.packages(c("ggplot2", "hexbin","ggmap", "maps", "RColorBrewer",

"scales","ReadImages"))library(ggplot2)library(hexbin)library(ggmap)library(maps)library(RColorBrewer)library(scales)library(ReadImages)

Page 3: Tech talk   ggplot2

Grammar of Graphics

• Framework for describing visualized data– Mapping data onto a coordinate system

• Created by Leland Wilkinson

Page 4: Tech talk   ggplot2
Page 5: Tech talk   ggplot2
Page 6: Tech talk   ggplot2
Page 7: Tech talk   ggplot2
Page 8: Tech talk   ggplot2

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

Page 9: Tech talk   ggplot2

Your First ggplot2 Plot

ggplot(data=mpg, aes(x=cty, y=hwy))

+ geom_point()

Page 10: Tech talk   ggplot2

Your First ggplot2 Plot

ggplot(data=mpg, aes(x=cty, y=hwy))

+ geom_point()

The data.frame

to plot

Page 11: Tech talk   ggplot2

Your First ggplot2 Plot

ggplot(data=mpg, aes(x=cty, y=hwy))

+ geom_point()

Aesthetic Mappings

The data.frame

to plot

Page 12: Tech talk   ggplot2

Your First ggplot2 Plot

ggplot(data=mpg, aes(x=cty, y=hwy))

+ geom_point()

Aesthetic Mappings

The data.frame

to plot

What geom to

use in plotting

Page 13: Tech talk   ggplot2

Your First ggplot2 Plot

library(ggplot2)ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()

Page 14: Tech talk   ggplot2

Improvements

Problem: verbosity

ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point()

ggplot(mpg, aes(cty, hwy)) + geom_point()

Page 15: Tech talk   ggplot2

Improvements

Problem: OverplottingSolution: Different geoms

ggplot(mpg, aes(cty, hwy)) + geom_jitter()

Page 16: Tech talk   ggplot2

Improvements

Problem: OverplottingSolution: Different geoms

ggplot(mpg, aes(cty, hwy)) + geom_bin2d()

Page 17: Tech talk   ggplot2

Colors & Groups

ggplot(mpg, aes(cty, hwy, color=drv)) + geom_jitter()

Page 18: Tech talk   ggplot2

Colors & Groups

ggplot(mpg,aes(drv, hwy)) + geom_boxplot()

Page 19: Tech talk   ggplot2

Adding Layers

ggplot(mpg,aes(drv, hwy)) + geom_jitter() + geom_boxplot()

Page 20: Tech talk   ggplot2

Adding Layers

ggplot(mpg, aes(cty, hwy)) + geom_jitter() + geom_smooth()

Page 21: Tech talk   ggplot2

Adding Layers

ggplot(data, aes(TIME, Y, color=GROUP)) + geom_line() + geom_smooth()

Page 22: Tech talk   ggplot2

Faceting

ggplot(mpg, aes(cty, hwy)) + geom_jitter()+ facet_grid(drv ~ class)

Page 23: Tech talk   ggplot2

Refining Plots

ggplot(mpg, aes(cty, hwy)) + geom_jitter() + xlab("City MPG") + ylab("Hwy MPG") + ggtitle("City vs Highway MPG")

Page 24: Tech talk   ggplot2

Etc.

Page 25: Tech talk   ggplot2

Etc.

Page 26: Tech talk   ggplot2

Etc.

Page 27: Tech talk   ggplot2

Etc.

Page 28: Tech talk   ggplot2

Etc.

Page 29: Tech talk   ggplot2

Etc.

Page 30: Tech talk   ggplot2

Etc.

Page 31: Tech talk   ggplot2

Etc.