r course 2014: lecture 8
TRANSCRIPT
-
8/10/2019 R Course 2014: Lecture 8
1/56
Lecture 8: graphing in Rand intro to ggplot
Ben FansonSimeon Lisovski
-
8/10/2019 R Course 2014: Lecture 8
2/56
Lecture Outline1) introduction to R graphics
2) introduction to ggplot
Helpful references
- http://www.cookbook-r.com/Graphs/
- ggplot2: Elegant Graphics for Data Analysis by Hadley Wickham
-
8/10/2019 R Course 2014: Lecture 8
3/56
R graphicsPros
1) You can make almost any graph that you can think of
2) Graphics are publishable quality
3) Combined with the previous programming learned, you can 'every complex graphs to visualize your data and statistical mod
4) You can make lots of graphs easily [e.g. plot for each individua
Cons
1) it takes some effort to learn the language and quirks of the graapproach
-
8/10/2019 R Course 2014: Lecture 8
4/56
Overview of R maingraphics
R graphics
base plot[original R graphics]
- plot()
- hist()
- barplot()
- pairs()
-
8/10/2019 R Course 2014: Lecture 8
5/56
plot(...) image(...) barplot
persp(...) pairs(...)
and
-
8/10/2019 R Course 2014: Lecture 8
6/56
Some advantages of base plot
1) I find it the easiest to build very customized plot since youplots one element at a time
-
8/10/2019 R Course 2014: Lecture 8
7/56
#--- example code to build a plot by each element ---#
plot.new()
points(seq(0,1,0.1),seq(0,1,0.1), pch=1:10)
axis(1,at=c(0.2,0.7))axis(2,at=c(0.1,0.8))
mtext('xlab',1,line=2)
mtext('ylab',2,line=2)
box()
abline(0,1, col='red')mtitle('Title',lr='')
-
8/10/2019 R Course 2014: Lecture 8
8/56
Some advantages of base plot
1) I find it the easiest way to build very customized plot, sincbuild the plots one element at a time
2) being the original, it is the most integrated with packages
-
8/10/2019 R Course 2014: Lecture 8
9/56
base plot and methods
ds
-
8/10/2019 R Course 2014: Lecture 8
10/56
base plot and methods
ds
-
8/10/2019 R Course 2014: Lecture 8
11/56
base plot and methodshow can plot() give you very different results?????????????
ds
-
8/10/2019 R Course 2014: Lecture 8
12/56
plot() is not a single function
How does plot() work?1) plot() looks at the class of the object(s) and then choose a
functione.g. plot( y ~ x )
plot asks what is class(y) and class(x) and since both are numericmakes a scatterplot
-
8/10/2019 R Course 2014: Lecture 8
13/56
plot() is not a single function
How does plot() work?2) plot() looks at the class of the object(s) and then choose
functione.g. plot( lm_mod )
plot asks what is class(lm_mod), and since it is a'lm' class, it runs function plot.lm()which makes
four graphs by default
-
8/10/2019 R Course 2014: Lecture 8
14/56
methods(plot)
base plot and methods
O i f R i
-
8/10/2019 R Course 2014: Lecture 8
15/56
Overview of R maingraphics
R graphics
base plot[original R graphics]
- plot()
- hist()- barplot()
- pairs()
O i f R i
-
8/10/2019 R Course 2014: Lecture 8
16/56
Overview of R maingraphics
R graphics
grid graphics[ alternative framework]
base plot[original R graphics]
- plot()
- hist()- barplot()
- pairs()
O i f R i
-
8/10/2019 R Course 2014: Lecture 8
17/56
Overview of R maingraphics
R graphics
grid graphics[ alternative framework]
base plot[original R graphics]
lat
- plot()
- hist()- barplot()
- pairs()
- xyp
- bar
- wir
( k ll )
-
8/10/2019 R Course 2014: Lecture 8
18/56
xyplot(...)
Faceting (aka Trellising)
-
8/10/2019 R Course 2014: Lecture 8
19/56
barchart(...)
Lattice can also do most things are bas
wireframe(...)
Overview of R main
-
8/10/2019 R Course 2014: Lecture 8
20/56
Overview of R maingraphics
R graphics
grid graphics[ alternative framework]
base plot[original R graphics]
lat
- plot()
- hist()- barplot()
- pairs()
- xyp
- bar
- wir
Overview of R main
-
8/10/2019 R Course 2014: Lecture 8
21/56
Overview of R maingraphics
R graphics
grid graphics[ alternative framework]
base plot[original R graphics]
latggplot2
- plot()
- hist()- barplot()
- pairs()
- ggplot() + geom_line()
- ggplot() + geom_point()
- xyp
- bar
- wir
-
8/10/2019 R Course 2014: Lecture 8
22/56
http://mandymejia.wordpress.com/2013/11/13/10-reasons-to-switch-to-ggplot-7/
-
8/10/2019 R Course 2014: Lecture 8
23/56
ggplot(...) + geom_point(...) + facet_wrap(...)
ggmap(...) + geom_tiles()
-
8/10/2019 R Course 2014: Lecture 8
24/56
why I use ggplot?
1) I like the faceting and grouping...makes it easy to make qucomplex graphs for data exploration
2) I found it easier to add a new layer
3) I liked the grouping options and colour schemes in ggplot
4) You can make up your own 'theme' that you can use over again
5) Lots of active development in the area
-
8/10/2019 R Course 2014: Lecture 8
25/56
cons of ggplot...
1) I find working with grid graphics more difficult than base pmakes it harder to do some of those final touches on the g[Note- ggplot2 community is active, so can often find the aget help easy enough]
2) no 3d plotting
3) Customising axis labels for facetted graphs can be annoyin
4) cannot do double axesa) Hadley Wickham refuses to add this feature due to philosophica
b) though I have heard of a workaround for it
-
8/10/2019 R Course 2014: Lecture 8
26/56
Saving a graph
jpeg(filename, height=, width=, units=,res= )
jpeg('figures/test1.jpg', height=6, width=6, units='cm', plot(....)
dev.off()
pdf(filename, height=, width=)pdf('figures/test1.pdf', height=6, width=6)
plot(....)
dev.off()
-
8/10/2019 R Course 2014: Lecture 8
27/56
Raster vs. vector graphics
Raster images- method: based on a grid of dots (pixels). Each pixel is assig
colour.
- file formats: jpg, tiff, bitmap, psd
- use: best for photographs
-
8/10/2019 R Course 2014: Lecture 8
28/56
Raster vs. vector graphics
Vector images- method: based on mathematical equations to redraw the
- file formats: eps, ps, pdf, ai
- use:best for drawings, logos, graphics. Much easier to doprocessing revisions
-
8/10/2019 R Course 2014: Lecture 8
29/56
Raster vs. vector graphics
-
8/10/2019 R Course 2014: Lecture 8
30/56
Adobe illustrator for post-process
Illustrator is great for minor little touches to the graphs or colmultiple graphs into a single page.
>
-
8/10/2019 R Course 2014: Lecture 8
31/56
Short introduction to ggplo
-
8/10/2019 R Course 2014: Lecture 8
32/56
geomsgeometric objects [think of as plot type]
e.g. scatterplot, line graph, histogram
ggplot jargon
geom_point()geom_line()
geom_bar
-
8/10/2019 R Course 2014: Lecture 8
33/56
geomsgeometric objects [think of as plot type]
e.g. scatterplot, line graph, histogram
aesaesthetics are the attributes associated with each geomobject
ggplot jargon
-
8/10/2019 R Course 2014: Lecture 8
34/56
aesthetics
x-value = 2.4
y-value = 0.4
shape = dot
colour = black
transparency = opaque
h i
-
8/10/2019 R Course 2014: Lecture 8
35/56
aesthetics
x-value = c(1.7,2.4,2.7...)
y-value = c(-0.5, 0.4,0.6...)
line type = solid
colour = black
transparency = opaque
l t j
-
8/10/2019 R Course 2014: Lecture 8
36/56
geomsgeometric objects [think of as plot type]
e.g. scatterplot, line graph, histogram
aesaesthetics are the attributes associated with each geomobject
scalesattributes of the x-axis and y-axis [and any z-axis]
ggplot jargon
l
-
8/10/2019 R Course 2014: Lecture 8
37/56
scales
continuous
ranges from -1.5 to 2.1
ticks marks at every 0.5
scales
-
8/10/2019 R Course 2014: Lecture 8
38/56
scales
co
ra
tic
set.seed=100
ds
-
8/10/2019 R Course 2014: Lecture 8
39/56
geomsgeometric objects [think of as plot type]
e.g. scatterplot, line graph, histogram
aesaesthetics are the attributes associated with each geomobject
scalesattributes of the x-axis and y-axis [and any z-axis]
facetsmaking separate plots broken up by one or two varia
ggplot jargon
facets
-
8/10/2019 R Course 2014: Lecture 8
40/56
facets
set.seed=100
ds
-
8/10/2019 R Course 2014: Lecture 8
41/56
similar to dplyr grammar, think of it as a sentence that you ar
'specify dataset' + # ggplot(ds,...)
ggplot grammar
l t
-
8/10/2019 R Course 2014: Lecture 8
42/56
similar to dplyr grammar, think of it as a sentence that you ar
'specify dataset' +
'specify x, y, grouping variables' +
ggplot grammar
# aes(x=,y=,col=, sh
-
8/10/2019 R Course 2014: Lecture 8
43/56
ggplot grammar
-
8/10/2019 R Course 2014: Lecture 8
44/56
similar to dplyr grammar, think of it as a sentence that you ar
'specify dataset' +
'specify x, y, grouping variables' +
'specify plot layers (e.g. point, line, stat function)' +
'specify if you want faceting' +
ggplot grammar
# facet_grid()
ggplot grammar
-
8/10/2019 R Course 2014: Lecture 8
45/56
similar to dplyr grammar, think of it as a sentence that you ar
'specify dataset' +
'specify x, y, grouping variables' +
'specify plot layers (e.g. point, line, stat function)' +
'specify if you want faceting' +
'specify minor details/options [labels, position of legen
ggplot grammar
# scale_name(), th
-
8/10/2019 R Course 2014: Lecture 8
46/56
example dataset
Bird_id Sex Treatment Growth_rate
1 male t1 12.3
2 male t2 10.3
3 male t3 14.5
4 female t1 14.35 female t2 9.3
6 female t3 15.6
scatterplot of data
-
8/10/2019 R Course 2014: Lecture 8
47/56
ggplot( ds ) + geom_point( aes(x=sex, y= growth_rate ) )
scatterplot of data
scatterplot of data
-
8/10/2019 R Course 2014: Lecture 8
48/56
ggplot( ds ) + geom_point( aes(x=trt, y= growth_rate ) )
scatterplot of data
scatterplot of data
co
-
8/10/2019 R Course 2014: Lecture 8
49/56
ggplot( ds ) + geom_point( aes(x=trt, y= growth_rate, col=sex) )
scatte p ot o data
coby sex
-
8/10/2019 R Course 2014: Lecture 8
50/56
you can move aes() t
-
8/10/2019 R Course 2014: Lecture 8
51/56
ggplot( ds, aes(x=trt, y= growth_rate, col=sex, group=sex )) +
geom_point( ) +geom_line( )
y ()ggplot()
adding a facet
-
8/10/2019 R Course 2014: Lecture 8
52/56
ggplot( ds, aes(x=trt, y= growth_rate, col=sex, group=sex ) ) +
geom_point( ) +geom_line( ) +
facet_grid(.~sex)
adding a facet
row ~ column
['.' just means no grouping variable]
key points so far
-
8/10/2019 R Course 2014: Lecture 8
53/56
1) I have not had to specify the dataset anymore
2) all the geom adopt the same scales (no specifying x-range
3) grouping by colour, shape, fill, etc. is easy
4) faceting is quick
5) a common language to everything (i.e. not a bunch of seppackages for different plot types)
key points so far
What's next
-
8/10/2019 R Course 2014: Lecture 8
54/56
Learning about base plot
- introducing basics of plot()- overlaying plots and customizing your plots
- discuss some more advanced plotting functions
-
8/10/2019 R Course 2014: Lecture 8
55/56
Lecture 8: Hands on Sectio
Lecture 8 files
-
8/10/2019 R Course 2014: Lecture 8
56/56
1) get Lecture8.Rfrom github
2) make sure that you have data/lecture7/ [same files as last w
3) open up Lecture8.Rin Rcourse_proj.Rpoj
4) start working through the example and then try the exercis