├── .gitignore
├── README.md
├── beamerthemeAmsterdam.sty
├── climate.csv
├── ggplot.R
├── ggplot.Rnw
├── ggplot.pdf
├── ggplot.tex
├── images
├── chang_book.png
├── color_palette.png
├── color_schemes.png
├── git_repo.png
└── hadley.png
├── make.sh
└── slides.png
/.gitignore:
--------------------------------------------------------------------------------
1 | *.log
2 | *.nav
3 | *.out
4 | *.snm
5 | *.tex
6 | *.toc
7 | *.vrb
8 | *.aux
9 | *.sh
10 | .DS_Store
11 | figure/
12 | local.R
13 | cache/*
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Introduction to ggplot2
2 |
3 | Author: [Karthik Ram](mailto:karthik.ram+ggplot2@gmail.com)
4 |
5 | Here are my [slides](https://github.com/karthikram/ggplot-lecture/blob/master/ggplot.pdf?raw=true) for the Spring 2013 R seminar in Integrative Biology at UC Berkeley. The source file is `ggplot.Rnw`. Feel free to modify and reuse as necessary.
6 |
7 | [](https://github.com/karthikram/ggplot-lecture/blob/master/ggplot.pdf?raw=true)
8 |
9 | To generate the pdf presentation locally:
10 |
11 | ```r
12 | knitr('ggplot.Rnw')
13 | system("pdflatex ggplot.tex")
14 | ```
15 |
16 | * [R code from the deck](https://github.com/karthikram/ggplot-lecture/blob/master/ggplot.R)
17 |
18 | ## Suggested readings
19 | * [Elegant graphics for data analysis](http://www.amazon.com/ggplot2-Ele gant-Graphics-Data-Analysis/dp/0387981403/)
20 | * [R Graphics Cookbook](http://www.amazon.com/R-Graphics-Cookbook-Winston-Chang/dp/1449316956)
21 |
22 | # License
23 | Creative Commons Attribution 2.0 Generic License.
24 |
--------------------------------------------------------------------------------
/beamerthemeAmsterdam.sty:
--------------------------------------------------------------------------------
1 | \DeclareOptionBeamer{compress}{\beamer@compresstrue}
2 | \ProcessOptionsBeamer
3 |
4 | \mode
5 |
6 | \useoutertheme[footline=authortitle]{miniframes}
7 | \useinnertheme{circles}
8 | \usecolortheme{whale}
9 | \usecolortheme{orchid}
10 |
11 | \definecolor{beamer@blendedblue}{rgb}{0.137,0.466,0.741}
12 |
13 | \setbeamercolor{structure}{fg=beamer@blendedblue}
14 | \setbeamercolor{titlelike}{parent=structure}
15 | \setbeamercolor{frametitle}{fg=black}
16 | \setbeamercolor{title}{fg=black}
17 | \setbeamercolor{item}{fg=black}
18 |
19 | \mode
20 |
21 |
--------------------------------------------------------------------------------
/climate.csv:
--------------------------------------------------------------------------------
1 | "","Source","Year","Anomaly1y","Anomaly5y","Anomaly10y","Unc10y"
2 | "102","Berkeley",1901,NA,NA,-0.162,0.109
3 | "103","Berkeley",1902,NA,NA,-0.177,0.108
4 | "104","Berkeley",1903,NA,NA,-0.199,0.104
5 | "105","Berkeley",1904,NA,NA,-0.223,0.105
6 | "106","Berkeley",1905,NA,NA,-0.241,0.107
7 | "107","Berkeley",1906,NA,NA,-0.294,0.106
8 | "108","Berkeley",1907,NA,NA,-0.312,0.105
9 | "109","Berkeley",1908,NA,NA,-0.328,0.103
10 | "110","Berkeley",1909,NA,NA,-0.281,0.101
11 | "111","Berkeley",1910,NA,NA,-0.247,0.099
12 | "112","Berkeley",1911,NA,NA,-0.243,0.097
13 | "113","Berkeley",1912,NA,NA,-0.257,0.1
14 | "114","Berkeley",1913,NA,NA,-0.268,0.1
15 | "115","Berkeley",1914,NA,NA,-0.257,0.097
16 | "116","Berkeley",1915,NA,NA,-0.249,0.095
17 | "117","Berkeley",1916,NA,NA,-0.214,0.096
18 | "118","Berkeley",1917,NA,NA,-0.201,0.096
19 | "119","Berkeley",1918,NA,NA,-0.176,0.096
20 | "120","Berkeley",1919,NA,NA,-0.182,0.097
21 | "121","Berkeley",1920,NA,NA,-0.193,0.097
22 | "122","Berkeley",1921,NA,NA,-0.167,0.098
23 | "123","Berkeley",1922,NA,NA,-0.128,0.096
24 | "124","Berkeley",1923,NA,NA,-0.075,0.097
25 | "125","Berkeley",1924,NA,NA,-0.064,0.098
26 | "126","Berkeley",1925,NA,NA,-0.065,0.1
27 | "127","Berkeley",1926,NA,NA,-0.05,0.1
28 | "128","Berkeley",1927,NA,NA,-0.02,0.099
29 | "129","Berkeley",1928,NA,NA,-0.018,0.099
30 | "130","Berkeley",1929,NA,NA,-0.026,0.1
31 | "131","Berkeley",1930,NA,NA,-0.014,0.101
32 | "132","Berkeley",1931,NA,NA,-0.047,0.098
33 | "133","Berkeley",1932,NA,NA,-0.035,0.096
34 | "134","Berkeley",1933,NA,NA,-0.017,0.093
35 | "135","Berkeley",1934,NA,NA,0.02,0.092
36 | "136","Berkeley",1935,NA,NA,0.053,0.089
37 | "137","Berkeley",1936,NA,NA,0.063,0.085
38 | "138","Berkeley",1937,NA,NA,0.048,0.081
39 | "139","Berkeley",1938,NA,NA,0.073,0.079
40 | "140","Berkeley",1939,NA,NA,0.113,0.076
41 | "141","Berkeley",1940,NA,NA,0.113,0.072
42 | "142","Berkeley",1941,NA,NA,0.134,0.071
43 | "143","Berkeley",1942,NA,NA,0.134,0.069
44 | "144","Berkeley",1943,NA,NA,0.127,0.07
45 | "145","Berkeley",1944,NA,NA,0.111,0.068
46 | "146","Berkeley",1945,NA,NA,0.072,0.066
47 | "147","Berkeley",1946,NA,NA,0.035,0.066
48 | "148","Berkeley",1947,NA,NA,0.042,0.064
49 | "149","Berkeley",1948,NA,NA,0.045,0.063
50 | "150","Berkeley",1949,NA,NA,0.013,0.062
51 | "151","Berkeley",1950,NA,NA,0.01,0.058
52 | "152","Berkeley",1951,NA,NA,-0.017,0.054
53 | "153","Berkeley",1952,NA,NA,-0.04,0.047
54 | "154","Berkeley",1953,NA,NA,-0.04,0.043
55 | "155","Berkeley",1954,NA,NA,-0.032,0.038
56 | "156","Berkeley",1955,NA,NA,-0.022,0.035
57 | "157","Berkeley",1956,NA,NA,0.012,0.031
58 | "158","Berkeley",1957,NA,NA,0.007,0.028
59 | "159","Berkeley",1958,NA,NA,0.002,0.027
60 | "160","Berkeley",1959,NA,NA,0.002,0.026
61 | "161","Berkeley",1960,NA,NA,-0.019,0.026
62 | "162","Berkeley",1961,NA,NA,-0.001,0.021
63 | "163","Berkeley",1962,NA,NA,0.017,0.018
64 | "164","Berkeley",1963,NA,NA,0.004,0.016
65 | "165","Berkeley",1964,NA,NA,-0.028,0.018
66 | "166","Berkeley",1965,NA,NA,-0.006,0.017
67 | "167","Berkeley",1966,NA,NA,-0.024,0.017
68 | "168","Berkeley",1967,NA,NA,-0.041,0.019
69 | "169","Berkeley",1968,NA,NA,-0.025,0.02
70 | "170","Berkeley",1969,NA,NA,-0.019,0.024
71 | "171","Berkeley",1970,NA,NA,0.01,0.026
72 | "172","Berkeley",1971,NA,NA,0.007,0.022
73 | "173","Berkeley",1972,NA,NA,0.015,0.015
74 | "174","Berkeley",1973,NA,NA,0.028,0.012
75 | "175","Berkeley",1974,NA,NA,0.049,0.014
76 | "176","Berkeley",1975,NA,NA,0.068,0.012
77 | "177","Berkeley",1976,NA,NA,0.128,0.011
78 | "178","Berkeley",1977,NA,NA,0.158,0.012
79 | "179","Berkeley",1978,NA,NA,0.167,0.013
80 | "180","Berkeley",1979,NA,NA,0.193,0.012
81 | "181","Berkeley",1980,NA,NA,0.186,0.016
82 | "182","Berkeley",1981,NA,NA,0.217,0.016
83 | "183","Berkeley",1982,NA,NA,0.235,0.014
84 | "184","Berkeley",1983,NA,NA,0.27,0.014
85 | "185","Berkeley",1984,NA,NA,0.318,0.014
86 | "186","Berkeley",1985,NA,NA,0.344,0.013
87 | "187","Berkeley",1986,NA,NA,0.352,0.012
88 | "188","Berkeley",1987,NA,NA,0.38,0.011
89 | "189","Berkeley",1988,NA,NA,0.37,0.013
90 | "190","Berkeley",1989,NA,NA,0.366,0.017
91 | "191","Berkeley",1990,NA,NA,0.433,0.019
92 | "192","Berkeley",1991,NA,NA,0.467,0.018
93 | "193","Berkeley",1992,NA,NA,0.496,0.017
94 | "194","Berkeley",1993,NA,NA,0.526,0.019
95 | "195","Berkeley",1994,NA,NA,0.554,0.02
96 | "196","Berkeley",1995,NA,NA,0.563,0.019
97 | "197","Berkeley",1996,NA,NA,0.565,0.022
98 | "198","Berkeley",1997,NA,NA,0.618,0.022
99 | "199","Berkeley",1998,NA,NA,0.68,0.023
100 | "200","Berkeley",1999,NA,NA,0.734,0.025
101 | "201","Berkeley",2000,NA,NA,0.748,0.026
102 | "202","Berkeley",2001,NA,NA,0.793,0.027
103 | "203","Berkeley",2002,NA,NA,0.856,0.028
104 | "204","Berkeley",2003,NA,NA,0.869,0.028
105 | "205","Berkeley",2004,NA,NA,0.884,0.029
106 |
--------------------------------------------------------------------------------
/ggplot.R:
--------------------------------------------------------------------------------
1 |
2 |
3 | opts_chunk$set(cache=TRUE, message=FALSE)
4 | # smaller font size for chunks
5 | opts_chunk$set(size = 'footnotesize')
6 | options(width = 60)
7 |
8 |
9 |
10 | ## install.packages("ggplot2", dependencies = TRUE)
11 |
12 |
13 |
14 | head(iris)
15 |
16 |
17 |
18 | ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
19 | geom_point()
20 |
21 |
22 |
23 | ## ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
24 | ## + geom_point()
25 | ## myplot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
26 | ## myplot + geom_point()
27 |
28 |
29 |
30 | ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
31 | geom_point(size = 3)
32 |
33 |
34 |
35 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
36 | geom_point(size = 3)
37 |
38 |
39 |
40 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
41 | geom_point(aes(shape = Species), size = 3)
42 |
43 |
44 |
45 | ## # Make a small sample of the diamonds dataset
46 | ## d2 <- diamonds[sample(1:dim(diamonds)[1], 1000), ]
47 |
48 | # Efficient and more readable random sample
49 | # d2 <- diamonds[sample(nrow(diamonds), 1000), ]
50 |
51 | # Robust and more readable random sample
52 | # d2 <- diamonds[sample(1:nrow(diamonds), 1000, replace = FALSE), ]
53 |
54 |
55 | d2 <- diamonds[sample(1:dim(diamonds)[1], 1000), ]
56 | ggplot(d2, aes(carat, price, color = color)) + geom_point() + theme_gray()
57 |
58 |
59 |
60 | library(MASS)
61 | ggplot(birthwt, aes(factor(race), bwt)) + geom_boxplot()
62 |
63 |
64 |
65 | h <- ggplot(faithful, aes(x = waiting))
66 | h + geom_histogram(binwidth = 30, colour = "black")
67 |
68 |
69 |
70 | h <- ggplot(faithful, aes(x = waiting))
71 | h + geom_histogram(binwidth = 8, fill = "steelblue",
72 | colour = "black")
73 |
74 |
75 |
76 | setwd('~/Github/ggplot2-lecture/')
77 |
78 |
79 |
80 | climate <- read.csv("climate.csv", header = T)
81 | ggplot(climate, aes(Year, Anomaly10y)) +
82 | geom_line()
83 |
84 |
85 |
86 | ggplot(climate, aes(Year, Anomaly10y)) +
87 | geom_ribbon(aes(ymin = Anomaly10y - Unc10y,
88 | ymax = Anomaly10y + Unc10y),
89 | fill = "blue", alpha = .1) +
90 | geom_line(color = "steelblue")
91 |
92 |
93 |
94 | cplot <- ggplot(climate, aes(Year, Anomaly10y))
95 | cplot <- cplot + geom_line(size = 0.7, color = "black")
96 | cplot <- cplot + geom_line(aes(Year, Anomaly10y + Unc10y), linetype = "dashed", size = 0.7, color = "red")
97 | cplot <- cplot + geom_line(aes(Year, Anomaly10y - Unc10y), linetype = "dashed", size = 0.7, color = "red")
98 | cplot + theme_gray()
99 |
100 |
101 |
102 | ggplot(iris, aes(Species, Sepal.Length)) +
103 | geom_bar(stat = "identity")
104 |
105 |
106 |
107 | df <- melt(iris, id.vars = "Species")
108 | ggplot(df, aes(Species, value, fill = variable)) +
109 | geom_bar(stat = "identity")
110 |
111 |
112 |
113 | iris[1:2, ]
114 | df <- melt(iris, id.vars = "Species")
115 | df[1:2, ]
116 |
117 |
118 |
119 | ggplot(df, aes(Species, value, fill = variable)) +
120 | geom_bar(stat = "identity", position = "dodge")
121 |
122 |
123 |
124 | ggplot(d2, aes(clarity, fill = cut)) +
125 | geom_bar(position = "dodge",stat = "bin") + theme_gray()
126 |
127 |
128 |
129 | clim <- read.csv('climate.csv', header = TRUE)
130 | clim$sign <- ifelse(clim$Anomaly10y<0, FALSE, TRUE)
131 | # or as simple as
132 | # clim$sign <- clim$Anomaly10y < 0
133 | ggplot(clim, aes(Year, Anomaly10y)) + geom_bar(stat = "identity", aes(fill = sign)) + theme_gray()
134 |
135 |
136 |
137 | ggplot(faithful, aes(waiting)) + geom_density()
138 |
139 |
140 |
141 | ggplot(faithful, aes(waiting)) +
142 | geom_density(fill = "blue", alpha = 0.1)
143 |
144 |
145 |
146 | ggplot(faithful, aes(waiting)) +
147 | geom_line(stat = "density")
148 |
149 |
150 |
151 | ## aes(color = variable)
152 | ## aes(color = "black")
153 | ## # Or add it as a scale
154 | ## scale_fill_manual(values = c("color1", "color2"))
155 |
156 |
157 |
158 | ## library(RColorBrewer)
159 | ## display.brewer.all()
160 |
161 |
162 |
163 | df <- melt(iris, id.vars = "Species")
164 | ggplot(df, aes(Species, value, fill = variable)) +
165 | geom_bar(stat = "identity", position = "dodge") +
166 | scale_fill_brewer(palette = "Set1")
167 |
168 |
169 |
170 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
171 | geom_point() +
172 | facet_grid(Species ~ .) +
173 | scale_color_manual(values = c("red", "green", "blue"))
174 |
175 |
176 |
177 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
178 | geom_point() +
179 | facet_grid(Species ~ .)
180 |
181 |
182 |
183 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
184 | geom_point() +
185 | facet_grid(. ~ Species)
186 |
187 |
188 |
189 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
190 | geom_point() +
191 | facet_wrap( ~ Species)
192 |
193 |
194 |
195 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
196 | geom_point(aes(shape = Species), size = 3) +
197 | geom_smooth(method = "lm")
198 |
199 |
200 |
201 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
202 | geom_point(aes(shape = Species), size = 3) +
203 | geom_smooth(method = "lm") +
204 | facet_grid(. ~ Species)
205 |
206 |
207 |
208 | ## + theme()
209 | ## # see ?theme() for more options
210 |
211 |
212 |
213 | ## ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
214 | ## geom_point(size = 1.2, shape = 16) +
215 | ## facet_wrap( ~ Species) +
216 | ## theme(legend.key = element_rect(fill = NA),
217 | ## legend.position = "bottom",
218 | ## strip.background = element_rect(fill = NA),
219 | ## axis.title.y = element_text(angle = 0))
220 |
221 |
222 |
223 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
224 | geom_point(size = 1.2, shape = 16) +
225 | facet_wrap( ~ Species) +
226 | theme(legend.key = element_rect(fill = NA),
227 | legend.position = "bottom",
228 | strip.background = element_rect(fill = NA),
229 | axis.title.y = element_text(angle = 0))
230 |
231 |
232 |
233 | ## install.packages('ggthemes')
234 | ## library(ggthemes)
235 | ## # Then add one of these themes to your plot
236 | ## + theme_stata()
237 | ## + theme_excel()
238 | ## + theme_wsj()
239 | ## + theme_solarized()
240 |
241 |
242 |
243 | ## my_custom_plot <- function(df, title = "", ...) {
244 | ## ggplot(df, ...) +
245 | ## ggtitle(title) +
246 | ## whatever geoms() +
247 | ## theme(...)
248 | ## }
249 |
250 |
251 |
252 | ## plot1 <- my_custom_plot(dataset1, title = "Figure 1")
253 |
254 |
255 |
256 | ## scale_fill_discrete(), scale_colour_discrete()
257 | ## scale_fill_hue(), scale_color_hue()
258 | ## scale_fill_manual(), scale_color_manual()
259 | ## scale_fill_brewer(), scale_color_brewer()
260 | ## scale_linetype(), scale_shape_manual()
261 |
262 |
263 |
264 | library(MASS)
265 | ggplot(birthwt, aes(factor(race), bwt)) +
266 | geom_boxplot(width = .2) +
267 | scale_y_continuous(labels = (paste0(1:4, " Kg")),
268 | breaks = seq(1000, 4000, by = 1000))
269 |
270 |
271 |
272 | ## # Assign the plot to an object
273 | ## dd <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
274 | ## geom_point(size = 4, shape = 16) +
275 | ## facet_grid(. ~Species)
276 | ## # Now add a scale
277 | ## dd +
278 | ## scale_y_continuous(breaks = seq(2, 8, by = 1),
279 | ## labels = paste0(2:8, " cm"))
280 |
281 |
282 |
283 | h + geom_histogram( aes(fill = ..count..), color="black") +
284 | scale_fill_gradient(low="green", high="red")
285 |
286 |
287 |
288 | ## ggsave('~/path/to/figure/filename.png')
289 |
290 |
291 |
292 | ## ggsave(plot1, file = "~/path/to/figure/filename.png")
293 |
294 |
295 |
296 | ## ggsave(file = "/path/to/figure/filename.png", width = 6,
297 | ## height =4)
298 |
299 |
300 |
301 | ## ggsave(file = "/path/to/figure/filename.eps")
302 | ## ggsave(file = "/path/to/figure/filename.jpg")
303 | ## ggsave(file = "/path/to/figure/filename.pdf")
304 |
305 |
306 |
--------------------------------------------------------------------------------
/ggplot.Rnw:
--------------------------------------------------------------------------------
1 | % Introduction to ggplot2
2 | % Author: Karthik Ram, karthik.ram@gmail.com
3 | % Licence, CC-BY
4 | \documentclass{beamer}
5 | \usepackage{listings}
6 | \usepackage{inconsolata}
7 | \setbeamertemplate{frametitle}[default][center]
8 | \usepackage{url}
9 | \setcounter{secnumdepth}{-1}
10 | \usetheme{Amsterdam}
11 | % --------------------------------------------------------------
12 | % Setting up some knitr options
13 | <>=
14 | opts_chunk$set(cache=TRUE, message=FALSE)
15 | # smaller font size for chunks
16 | opts_chunk$set(size = 'footnotesize')
17 | options(width = 60)
18 | knit_hooks$set(inline = function(x) {
19 | if (is.numeric(x)) return(knitr:::format_sci(x, 'latex'))
20 | knitr:::hi_latex(x)
21 | })
22 | @
23 |
24 |
25 | % --------------------------------------------------------------
26 | \begin{document}
27 | \title{Data Visualization with R \& ggplot2}
28 | \author{Karthik Ram}
29 | \maketitle
30 |
31 | % --------------------------------------------------------------
32 | \begin{frame}[fragile]
33 | \frametitle{Download this PDF}
34 | \begingroup
35 | \fontsize{12pt}{12pt}\selectfont
36 | \href{http://github.com/karthikram/ggplot-lecture}{github.com/karthikram/ggplot-lecture}\\
37 | \href{https://speakerdeck.com/karthik/}{https://speakerdeck.com/karthik/}
38 | \endgroup
39 | \includegraphics[scale=.31]{images/git_repo.png}
40 | \end{frame}
41 |
42 | % --------------------------------------------------------------
43 | \begin{frame}[fragile]
44 | \frametitle{Some housekeeping}
45 | Install some packages (make sure you also have recent copies of reshape2 and plyr)
46 | <>=
47 | install.packages("ggplot2", dependencies = TRUE)
48 | @
49 | \end{frame}
50 |
51 |
52 |
53 | % --------------------------------------------------------------
54 | \begin{frame}[fragile]
55 | \frametitle{Base graphics}
56 | \begin{itemize}
57 | \item Ugly, laborious, and verbose\\
58 | \item There are better ways to describe statistical visualizations.\\
59 | \end{itemize}
60 | \end{frame}
61 |
62 | \begin{frame}[fragile]
63 | \frametitle{Why \texttt{ggplot2}?}
64 | \begin{itemize}
65 | \item Follows a grammar, just like any language.
66 | \item It defines basic components that make up a sentence. In this case, the grammar defines components in a plot.
67 | \item Grammar of graphics originally coined by Lee Wilkinson
68 | \end{itemize}
69 | \end{frame}
70 |
71 |
72 | % --------------------------------------------------------------
73 | \begin{frame}[fragile]
74 | \frametitle{Why \texttt{ggplot2}?}
75 | \begin{itemize}
76 | \item Supports a continuum of expertise.
77 | \item Get started right away but with practice you can effortless build complex, publication quality figures.
78 | \end{itemize}
79 | \end{frame}
80 |
81 | % --------------------------------------------------------------
82 | \section*{Basics}
83 | \frame{\sectionpage}
84 |
85 |
86 | \begin{frame}[fragile]
87 | \frametitle{Some terminology}
88 | \begin{itemize}
89 | \item \textbf{ggplot} - The main function where you specify the dataset and variables to plot\\
90 | \item \textbf{geoms} - geometric objects
91 | \begin{itemize}
92 | \item geom\_point(), geom\_bar(), geom\_density(), geom\_line(), geom\_area()
93 | \end{itemize}
94 | \item \textbf{aes} - aesthetics
95 | \begin{itemize}
96 | \item shape, transparency (alpha), color, fill, linetype.
97 | \end{itemize}
98 | \item \textbf{scales} Define how your data will be plotted
99 | \begin{itemize}
100 | \item \emph{continuous}, \emph{discrete}, \emph{log}
101 | \end{itemize}
102 | \end{itemize}
103 | \end{frame}
104 |
105 |
106 | % --------------------------------------------------------------
107 | \section*{Assembling your first ggplot}
108 | \frame{\sectionpage}
109 |
110 | % --------------------------------------------------------------
111 | \begin{frame}[fragile]
112 | \frametitle{The iris dataset}
113 | <>=
114 | head(iris)
115 | @
116 | \end{frame}
117 |
118 | % --------------------------------------------------------------
119 | \begin{frame}[fragile]
120 | \frametitle{Let's try an example}
121 | <>=
122 | ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
123 | geom_point()
124 | @
125 | \end{frame}
126 |
127 | % --------------------------------------------------------------
128 | \begin{frame}[fragile]
129 | \frametitle{Basic structure}
130 | <>=
131 | ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
132 | + geom_point()
133 | myplot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
134 | myplot + geom_point()
135 | @
136 | \begin{itemize}
137 | \item Specify the data and variables inside the \texttt{ggplot} function.
138 | \item Anything else that goes in here becomes a global setting.
139 | \item Then add layers of geometric objects, statistical models, and panels.
140 | \end{itemize}
141 | \end{frame}
142 |
143 | % --------------------------------------------------------------
144 | \begin{frame}[fragile]
145 | \frametitle{Quick note}
146 | \begin{itemize}
147 | \item Never use \texttt{qplot} - short for quick plot.
148 | \item You'll end up unlearning and relearning a good bit.
149 | \end{itemize}
150 |
151 | \end{frame}
152 |
153 |
154 | % --------------------------------------------------------------
155 | \begin{frame}[fragile]
156 | \frametitle{Increase the size of points}
157 | <>=
158 | ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
159 | geom_point(size = 3)
160 | @
161 | \end{frame}
162 |
163 | % --------------------------------------------------------------
164 | \begin{frame}[fragile]
165 | \frametitle{Add some color}
166 | <>=
167 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
168 | geom_point(size = 3)
169 | @
170 | \end{frame}
171 |
172 | % --------------------------------------------------------------
173 | \begin{frame}[fragile]
174 | \frametitle{Differentiate points by shape}
175 | <>=
176 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
177 | geom_point(aes(shape = Species), size = 3)
178 | @
179 | \end{frame}
180 |
181 | % --------------------------------------------------------------
182 | \begin{frame}[fragile]
183 | \frametitle{Exercise 1}
184 | <>=
185 | # Make a small sample of the diamonds dataset
186 | d2 <- diamonds[sample(1:dim(diamonds)[1], 1000), ]
187 | @
188 | Then generate this plot below.
189 |
190 | <>=
191 | d2 <- diamonds[sample(1:dim(diamonds)[1], 1000), ]
192 | ggplot(d2, aes(carat, price, color = color)) + geom_point() + theme_gray()
193 | @
194 | \end{frame}
195 | % --------------------------------------------------------------
196 | \section*{Box plots}
197 | \frame{\sectionpage}
198 |
199 | \begin{frame}[fragile]
200 | See \texttt{?geom\_boxplot} for list of options
201 | <>=
202 | library(MASS)
203 | ggplot(birthwt, aes(factor(race), bwt)) + geom_boxplot()
204 | @
205 | \end{frame}
206 |
207 |
208 | % --------------------------------------------------------------
209 | \section*{Histograms}
210 | \frame{\sectionpage}
211 |
212 | % --------------------------------------------------------------
213 | \begin{frame}[fragile]
214 | See \texttt{?geom\_histogram} for list of options
215 | <>=
216 | h <- ggplot(faithful, aes(x = waiting))
217 | h + geom_histogram(binwidth = 30, colour = "black")
218 | @
219 | \end{frame}
220 |
221 | % --------------------------------------------------------------
222 | \begin{frame}[fragile]
223 | <>=
224 | h <- ggplot(faithful, aes(x = waiting))
225 | h + geom_histogram(binwidth = 8, fill = "steelblue",
226 | colour = "black")
227 | @
228 | \end{frame}
229 |
230 | % --------------------------------------------------------------
231 | \section*{Line plots}
232 | \frame{\sectionpage}
233 |
234 | \begin{frame}[fragile]
235 | <>=
236 | setwd('~/Github/ggplot2-lecture/')
237 | @
238 | % climate <- read.csv(text = RCurl::getURL('https://raw.github.com/karthikram/ggplot-lecture/master/climate.csv'))
239 | <>=
240 | climate <- read.csv("climate.csv", header = T)
241 | ggplot(climate, aes(Year, Anomaly10y)) +
242 | geom_line()
243 | @
244 | \begin{flushright}
245 | \begingroup
246 | \fontsize{6pt}{12pt}\selectfont
247 | \begin{verbatim}
248 | climate <- read.csv(text =
249 | RCurl::getURL('https://raw.github.com/karthikram/ggplot-lecture/master/climate.csv'))
250 | \end{verbatim}
251 | \endgroup
252 | \end{flushright}
253 | \end{frame}
254 |
255 | \begin{frame}[fragile]
256 | We can also plot confidence regions
257 | <>=
258 | ggplot(climate, aes(Year, Anomaly10y)) +
259 | geom_ribbon(aes(ymin = Anomaly10y - Unc10y,
260 | ymax = Anomaly10y + Unc10y),
261 | fill = "blue", alpha = .1) +
262 | geom_line(color = "steelblue")
263 | @
264 | \end{frame}
265 |
266 | % --------------------------------------------------------------
267 | \begin{frame}[fragile]
268 | \frametitle{Exercise 2}
269 | \begin{itemize}
270 | \item Modify the previous plot and change it such that there are three lines instead of one with a confidence band.
271 | <>=
272 | cplot <- ggplot(climate, aes(Year, Anomaly10y))
273 | cplot <- cplot + geom_line(size = 0.7, color = "black")
274 | cplot <- cplot + geom_line(aes(Year, Anomaly10y + Unc10y), linetype = "dashed", size = 0.7, color = "red")
275 | cplot <- cplot + geom_line(aes(Year, Anomaly10y - Unc10y), linetype = "dashed", size = 0.7, color = "red")
276 | cplot + theme_gray()
277 | @
278 |
279 | \end{itemize}
280 | \end{frame}
281 |
282 |
283 | % --------------------------------------------------------------
284 | \section*{Bar plots}
285 | \frame{\sectionpage}
286 |
287 | % --------------------------------------------------------------
288 | \begin{frame}[fragile]
289 | <>=
290 | ggplot(iris, aes(Species, Sepal.Length)) +
291 | geom_bar(stat = "identity")
292 | @
293 | \end{frame}
294 |
295 | % --------------------------------------------------------------
296 | \begin{frame}[fragile]
297 | <>=
298 | df <- melt(iris, id.vars = "Species")
299 | ggplot(df, aes(Species, value, fill = variable)) +
300 | geom_bar(stat = "identity")
301 | @
302 | \end{frame}
303 |
304 |
305 |
306 | % --------------------------------------------------------------
307 | \section*{plyr and reshape are key for using \texttt{R}}
308 | \frame{\sectionpage}
309 |
310 | \begin{frame}[fragile]
311 | \frametitle{plyr and reshape}
312 | These two packages are the swiss army knives of R.
313 | \begin{itemize}
314 | \item \texttt{plyr}
315 | \begin{enumerate}
316 | \item ddply
317 | \item llply
318 | \item join
319 | \end{enumerate}
320 | \item \texttt{reshape}.
321 | \begin{enumerate}
322 | \item melt
323 | \item dcast
324 | \item acast
325 | \end{enumerate}
326 | \end{itemize}
327 | \end{frame}
328 |
329 |
330 | % --------------------------------------------------------------
331 | \begin{frame}[fragile]
332 | <>=
333 | iris[1:2, ]
334 | df <- melt(iris, id.vars = "Species")
335 | df[1:2, ]
336 | @
337 | \end{frame}
338 |
339 |
340 | % --------------------------------------------------------------
341 | \begin{frame}[fragile]
342 | <>=
343 | ggplot(df, aes(Species, value, fill = variable)) +
344 | geom_bar(stat = "identity", position = "dodge")
345 | @
346 | \end{frame}
347 |
348 | % --------------------------------------------------------------
349 | \begin{frame}[fragile]
350 | \frametitle{Exercise 3}
351 | Using the d2 dataset you created earlier, generate this plot below. Take a quick look at the data first to see if it needs to be binned.
352 | <>=
353 | ggplot(d2, aes(clarity, fill = cut)) +
354 | geom_bar(position = "dodge",stat = "bin") + theme_gray()
355 | @
356 | \end{frame}
357 |
358 | % --------------------------------------------------------------
359 | \begin{frame}[fragile]
360 | \frametitle{Exercise 4}
361 | \begin{itemize}
362 | \item Using the climate dataset, create a new variable called sign. Make it logical (true/false) based on the sign of Anomaly10y.
363 | \item Plot a bar plot and use \texttt{sign} variable as the fill.\\
364 | <>=
365 | clim <- read.csv('climate.csv', header = TRUE)
366 | clim$sign <- ifelse(clim$Anomaly10y<0, FALSE, TRUE)
367 | # or as simple as
368 | # clim$sign <- clim$Anomaly10y < 0
369 | ggplot(clim, aes(Year, Anomaly10y)) + geom_bar(stat = "identity", aes(fill = sign)) + theme_gray()
370 | @
371 |
372 | \end{itemize}
373 | \end{frame}
374 |
375 |
376 | % --------------------------------------------------------------
377 | \section*{Density Plots}
378 | \frame{\sectionpage}
379 |
380 | \begin{frame}[fragile]
381 | \frametitle{Density plots}
382 | <>=
383 | ggplot(faithful, aes(waiting)) + geom_density()
384 | @
385 | \end{frame}
386 |
387 | % --------------------------------------------------------------
388 | \begin{frame}[fragile]
389 | \frametitle{Density plots}
390 | <>=
391 | ggplot(faithful, aes(waiting)) +
392 | geom_density(fill = "blue", alpha = 0.1)
393 | @
394 | \end{frame}
395 |
396 |
397 |
398 | % --------------------------------------------------------------
399 | \begin{frame}[fragile]
400 | <>=
401 | ggplot(faithful, aes(waiting)) +
402 | geom_line(stat = "density")
403 | @
404 | \end{frame}
405 |
406 |
407 | % --------------------------------------------------------------
408 | \section*{Mapping Variables to colors}
409 | \frame{\sectionpage}
410 |
411 |
412 | % --------------------------------------------------------------
413 | \begin{frame}[fragile]
414 | \frametitle{Colors}
415 | <>=
416 | aes(color = variable)
417 | aes(color = "black")
418 | # Or add it as a scale
419 | scale_fill_manual(values = c("color1", "color2"))
420 | @
421 | \end{frame}
422 |
423 |
424 | % --------------------------------------------------------------
425 | \begin{frame}[fragile]
426 | \frametitle{The RColorBrewer package}
427 | <>=
428 | library(RColorBrewer)
429 | display.brewer.all()
430 | @
431 | \includegraphics[scale=0.25]{images/color_palette.png}
432 | \end{frame}
433 |
434 | % --------------------------------------------------------------
435 | \begin{frame}[fragile]
436 | \frametitle{Using a color brewer palette}
437 | <>=
438 | df <- melt(iris, id.vars = "Species")
439 | ggplot(df, aes(Species, value, fill = variable)) +
440 | geom_bar(stat = "identity", position = "dodge") +
441 | scale_fill_brewer(palette = "Set1")
442 | @
443 | \end{frame}
444 |
445 | % --------------------------------------------------------------
446 | \begin{frame}[fragile]
447 | \frametitle{Manual color scale}
448 | <>=
449 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
450 | geom_point() +
451 | facet_grid(Species ~ .) +
452 | scale_color_manual(values = c("red", "green", "blue"))
453 | @
454 | \end{frame}
455 |
456 | % --------------------------------------------------------------
457 | \begin{frame}[fragile]
458 | \frametitle{Refer to a color chart for beautful visualizations}
459 | \url{http://tools.medialab.sciences-po.fr/iwanthue/}
460 | \includegraphics[scale=0.25]{images/color_schemes.png}
461 | \end{frame}
462 |
463 | % -----------------------------------
464 |
465 | \section*{Faceting}
466 | \frame{\sectionpage}
467 |
468 | \begin{frame}[fragile]
469 | \frametitle{Faceting along columns}
470 | <>=
471 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
472 | geom_point() +
473 | facet_grid(Species ~ .)
474 | @
475 | \end{frame}
476 |
477 | % --------------------------------------------------------------
478 | \begin{frame}[fragile]
479 | \frametitle{and along rows}
480 | <>=
481 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
482 | geom_point() +
483 | facet_grid(. ~ Species)
484 | @
485 | \end{frame}
486 |
487 | % --------------------------------------------------------------
488 | \begin{frame}[fragile]
489 | \frametitle{or just wrap your panels}
490 | <>=
491 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
492 | geom_point() +
493 | facet_wrap( ~ Species)
494 | @
495 | \end{frame}
496 |
497 | % --------------------------------------------------------------
498 | \section*{Adding smoothers}
499 | \frame{\sectionpage}
500 |
501 |
502 | % --------------------------------------------------------------
503 | \begin{frame}[fragile]
504 | <>=
505 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
506 | geom_point(aes(shape = Species), size = 3) +
507 | geom_smooth(method = "lm")
508 | @
509 | \end{frame}
510 |
511 | % --------------------------------------------------------------
512 | \begin{frame}[fragile]
513 | <>=
514 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
515 | geom_point(aes(shape = Species), size = 3) +
516 | geom_smooth(method = "lm") +
517 | facet_grid(. ~ Species)
518 | @
519 | \end{frame}
520 |
521 | % --------------------------------------------------------------
522 | \section*{Themes}
523 | \frame{\sectionpage}
524 |
525 | % --------------------------------------------------------------
526 | \begin{frame}[fragile]
527 | \frametitle{Adding themes}
528 | Themes are a great way to define custom plots.
529 | <>=
530 | + theme()
531 | # see ?theme() for more options
532 | @
533 |
534 | \end{frame}
535 |
536 |
537 | % --------------------------------------------------------------
538 | \begin{frame}[fragile]
539 | \frametitle{A themed plot}
540 | <>=
541 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
542 | geom_point(size = 1.2, shape = 16) +
543 | facet_wrap( ~ Species) +
544 | theme(legend.key = element_rect(fill = NA),
545 | legend.position = "bottom",
546 | strip.background = element_rect(fill = NA),
547 | axis.title.y = element_text(angle = 0))
548 | @
549 | \end{frame}
550 |
551 | % --------------------------------------------------------------
552 | \begin{frame}[fragile]
553 | \frametitle{Adding themes}
554 | <>=
555 | ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
556 | geom_point(size = 1.2, shape = 16) +
557 | facet_wrap( ~ Species) +
558 | theme(legend.key = element_rect(fill = NA),
559 | legend.position = "bottom",
560 | strip.background = element_rect(fill = NA),
561 | axis.title.y = element_text(angle = 0))
562 | @
563 | \end{frame}
564 |
565 | % --------------------------------------------------------------
566 | \begin{frame}[fragile]
567 | \frametitle{ggthemes library}
568 | <>=
569 | install.packages('ggthemes')
570 | library(ggthemes)
571 | # Then add one of these themes to your plot
572 | + theme_stata()
573 | + theme_excel()
574 | + theme_wsj()
575 | + theme_solarized()
576 | @
577 | \end{frame}
578 |
579 |
580 | % --------------------------------------------------------------
581 | \section*{Create functions to automate your plotting}
582 | \frame{\sectionpage}
583 |
584 | % --------------------------------------------------------------
585 | \begin{frame}[fragile]
586 | \frametitle{Write functions for day to day plots}
587 | <>=
588 | my_custom_plot <- function(df, title = "", ...) {
589 | ggplot(df, ...) +
590 | ggtitle(title) +
591 | whatever_geoms() +
592 | theme(...)
593 | }
594 | @
595 |
596 | Then just call your function to generate a plot.
597 | It's a lot easier to fix one function that do it over and over for many plots
598 | <>=
599 | plot1 <- my_custom_plot(dataset1, title = "Figure 1")
600 | @
601 |
602 |
603 | \end{frame}
604 |
605 | % --------------------------------------------------------------
606 | \section*{Scales}
607 | \frame{\sectionpage}
608 |
609 | % --------------------------------------------------------------
610 | \begin{frame}[fragile]
611 | \frametitle{Commonly used scales}
612 | <>=
613 | scale_fill_discrete(); scale_colour_discrete()
614 | scale_fill_hue(); scale_color_hue()
615 | scale_fill_manual(); scale_color_manual()
616 | scale_fill_brewer(); scale_color_brewer()
617 | scale_linetype(); scale_shape_manual()
618 | @
619 | \end{frame}
620 |
621 | % --------------------------------------------------------------
622 | \begin{frame}[fragile]
623 | \frametitle{Adding a continuous scale}
624 | <>=
625 | library(MASS)
626 | ggplot(birthwt, aes(factor(race), bwt)) +
627 | geom_boxplot(width = .2) +
628 | scale_y_continuous(labels = (paste0(1:4, " Kg")),
629 | breaks = seq(1000, 4000, by = 1000))
630 | @
631 | \end{frame}
632 |
633 |
634 | % --------------------------------------------------------------
635 | \begin{frame}[fragile]
636 | \frametitle{Another continuous scale with custom labels}
637 | <>=
638 | # Assign the plot to an object
639 | dd <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
640 | geom_point(size = 4, shape = 16) +
641 | facet_grid(. ~Species)
642 | # Now add a scale
643 | dd +
644 | scale_y_continuous(breaks = seq(2, 8, by = 1),
645 | labels = paste0(2:8, " cm"))
646 | @
647 | \end{frame}
648 |
649 |
650 | % --------------------------------------------------------------
651 | \begin{frame}[fragile]
652 | \frametitle{gradients}
653 | <>=
654 | h + geom_histogram( aes(fill = ..count..), color="black") +
655 | scale_fill_gradient(low="green", high="red")
656 | @
657 | \end{frame}
658 |
659 |
660 |
661 | % --------------------------------------------------------------
662 | \section*{Publication quality figures}
663 | \frame{\sectionpage}
664 |
665 | % How to save your plots
666 | % --------------------------------------------------------------
667 | \begin{frame}[fragile]
668 | \begin{itemize}
669 | \item If the plot is on your screen
670 | <>=
671 | ggsave('~/path/to/figure/filename.png')
672 | @
673 | \item If your plot is assigned to an object
674 | <>=
675 | ggsave(plot1, file = "~/path/to/figure/filename.png")
676 | @
677 |
678 | \item Specify a size
679 | <>=
680 | ggsave(file = "/path/to/figure/filename.png", width = 6,
681 | height =4)
682 | @
683 | \item or any format (pdf, png, eps, svg, jpg)
684 | <>=
685 | ggsave(file = "/path/to/figure/filename.eps")
686 | ggsave(file = "/path/to/figure/filename.jpg")
687 | ggsave(file = "/path/to/figure/filename.pdf")
688 | @
689 | \end{itemize}
690 | \end{frame}
691 |
692 | % --------------------------------------------------------------
693 | \begin{frame}[fragile]
694 | \frametitle{Further help}
695 | \begin{itemize}
696 | \item You've just scratched the surface with ggplot2.
697 | \item Practice
698 | \item Read the docs (either locally in \texttt{R} or at \url{http://docs.ggplot2.org/current/})
699 | \item Work together
700 | \end{itemize}
701 | \includegraphics[scale=.15]{images/chang_book.png}
702 | \includegraphics[scale=.15]{images/hadley.png}
703 | \end{frame}
704 |
705 | % --------------------------------------------------------------
706 | % end, hope it was useful.
707 | \end{document}
708 |
--------------------------------------------------------------------------------
/ggplot.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karthik/ggplot-lecture/b2980ab6df3dc2053d3e7c46db44616d3db9c0c7/ggplot.pdf
--------------------------------------------------------------------------------
/ggplot.tex:
--------------------------------------------------------------------------------
1 | % Introduction to ggplot2
2 | % Author: Karthik Ram, karthik.ram@gmail.com
3 | % Licence, CC-BY
4 | \documentclass{beamer}\usepackage[]{graphicx}\usepackage[]{color}
5 | %% maxwidth is the original width if it is less than linewidth
6 | %% otherwise use linewidth (to make sure the graphics do not exceed the margin)
7 | \makeatletter
8 | \def\maxwidth{ %
9 | \ifdim\Gin@nat@width>\linewidth
10 | \linewidth
11 | \else
12 | \Gin@nat@width
13 | \fi
14 | }
15 | \makeatother
16 |
17 | \definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
18 | \newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
19 | \newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
20 | \newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
21 | \newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
22 | \newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
23 | \newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
24 | \newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
25 | \newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
26 | \newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
27 |
28 | \usepackage{framed}
29 | \makeatletter
30 | \newenvironment{kframe}{%
31 | \def\at@end@of@kframe{}%
32 | \ifinner\ifhmode%
33 | \def\at@end@of@kframe{\end{minipage}}%
34 | \begin{minipage}{\columnwidth}%
35 | \fi\fi%
36 | \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
37 | \colorbox{shadecolor}{##1}\hskip-\fboxsep
38 | % There is no \\@totalrightmargin, so:
39 | \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
40 | \MakeFramed {\advance\hsize-\width
41 | \@totalleftmargin\z@ \linewidth\hsize
42 | \@setminipage}}%
43 | {\par\unskip\endMakeFramed%
44 | \at@end@of@kframe}
45 | \makeatother
46 |
47 | \definecolor{shadecolor}{rgb}{.97, .97, .97}
48 | \definecolor{messagecolor}{rgb}{0, 0, 0}
49 | \definecolor{warningcolor}{rgb}{1, 0, 1}
50 | \definecolor{errorcolor}{rgb}{1, 0, 0}
51 | \newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
52 |
53 | \usepackage{alltt}
54 | \usepackage{listings}
55 | \usepackage{inconsolata}
56 | \setbeamertemplate{frametitle}[default][center]
57 | \usepackage{url}
58 | \setcounter{secnumdepth}{-1}
59 | \usetheme{Amsterdam}
60 | % --------------------------------------------------------------
61 | % Setting up some knitr options
62 |
63 |
64 |
65 |
66 | % --------------------------------------------------------------
67 | \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
68 | \begin{document}
69 | \title{Data Visualization with R \& ggplot2}
70 | \author{Karthik Ram}
71 | \maketitle
72 |
73 | % --------------------------------------------------------------
74 | \begin{frame}[fragile]
75 | \frametitle{Download this PDF}
76 | \begingroup
77 | \fontsize{12pt}{12pt}\selectfont
78 | \href{http://github.com/karthikram/ggplot-lecture}{github.com/karthikram/ggplot-lecture}\\
79 | \href{https://speakerdeck.com/karthik/}{https://speakerdeck.com/karthik/}
80 | \endgroup
81 | \includegraphics[scale=.31]{images/git_repo.png}
82 | \end{frame}
83 |
84 | % --------------------------------------------------------------
85 | \begin{frame}[fragile]
86 | \frametitle{Some housekeeping}
87 | Install some packages (make sure you also have recent copies of reshape2 and plyr)
88 | \begin{knitrout}\footnotesize
89 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
90 | \begin{alltt}
91 | \hlkwd{install.packages}\hlstd{(}\hlstr{"ggplot2"}\hlstd{,} \hlkwc{dependencies} \hlstd{=} \hlnum{TRUE}\hlstd{)}
92 | \end{alltt}
93 | \end{kframe}
94 | \end{knitrout}
95 |
96 | \end{frame}
97 |
98 |
99 |
100 | % --------------------------------------------------------------
101 | \begin{frame}[fragile]
102 | \frametitle{Base graphics}
103 | \begin{itemize}
104 | \item Ugly, laborious, and verbose\\
105 | \item There are better ways to describe statistical visualizations.\\
106 | \end{itemize}
107 | \end{frame}
108 |
109 | \begin{frame}[fragile]
110 | \frametitle{Why \texttt{ggplot2}?}
111 | \begin{itemize}
112 | \item Follows a grammar, just like any language.
113 | \item It defines basic components that make up a sentence. In this case, the grammar defines components in a plot.
114 | \item Grammar of graphics originally coined by Lee Wilkinson
115 | \end{itemize}
116 | \end{frame}
117 |
118 |
119 | % --------------------------------------------------------------
120 | \begin{frame}[fragile]
121 | \frametitle{Why \texttt{ggplot2}?}
122 | \begin{itemize}
123 | \item Supports a continuum of expertise.
124 | \item Get started right away but with practice you can effortless build complex, publication quality figures.
125 | \end{itemize}
126 | \end{frame}
127 |
128 | % --------------------------------------------------------------
129 | \section*{Basics}
130 | \frame{\sectionpage}
131 |
132 |
133 | \begin{frame}[fragile]
134 | \frametitle{Some terminology}
135 | \begin{itemize}
136 | \item \textbf{ggplot} - The main function where you specify the dataset and variables to plot\\
137 | \item \textbf{geoms} - geometric objects
138 | \begin{itemize}
139 | \item geom\_point(), geom\_bar(), geom\_density(), geom\_line(), geom\_area()
140 | \end{itemize}
141 | \item \textbf{aes} - aesthetics
142 | \begin{itemize}
143 | \item shape, transparency (alpha), color, fill, linetype.
144 | \end{itemize}
145 | \item \textbf{scales} Define how your data will be plotted
146 | \begin{itemize}
147 | \item \emph{continuous}, \emph{discrete}, \emph{log}
148 | \end{itemize}
149 | \end{itemize}
150 | \end{frame}
151 |
152 |
153 | % --------------------------------------------------------------
154 | \section*{Assembling your first ggplot}
155 | \frame{\sectionpage}
156 |
157 | % --------------------------------------------------------------
158 | \begin{frame}[fragile]
159 | \frametitle{The iris dataset}
160 | \begin{knitrout}\footnotesize
161 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
162 | \begin{alltt}
163 | \hlkwd{head}\hlstd{(iris)}
164 | \end{alltt}
165 | \begin{verbatim}
166 | ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
167 | ## 1 5.1 3.5 1.4 0.2 setosa
168 | ## 2 4.9 3.0 1.4 0.2 setosa
169 | ## 3 4.7 3.2 1.3 0.2 setosa
170 | ## 4 4.6 3.1 1.5 0.2 setosa
171 | ## 5 5.0 3.6 1.4 0.2 setosa
172 | ## 6 5.4 3.9 1.7 0.4 setosa
173 | \end{verbatim}
174 | \end{kframe}
175 | \end{knitrout}
176 |
177 | \end{frame}
178 |
179 | % --------------------------------------------------------------
180 | \begin{frame}[fragile]
181 | \frametitle{Let's try an example}
182 | \begin{knitrout}\footnotesize
183 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
184 | \begin{alltt}
185 | \hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= iris,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= Sepal.Length,} \hlkwc{y} \hlstd{= Sepal.Width))} \hlopt{+}
186 | \hlkwd{geom_point}\hlstd{()}
187 | \end{alltt}
188 | \end{kframe}
189 | \includegraphics[width=.75\linewidth]{figure/first_plot_}
190 |
191 | \end{knitrout}
192 |
193 | \end{frame}
194 |
195 | % --------------------------------------------------------------
196 | \begin{frame}[fragile]
197 | \frametitle{Basic structure}
198 | \begin{knitrout}\footnotesize
199 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
200 | \begin{alltt}
201 | \hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= iris,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= Sepal.Length,} \hlkwc{y} \hlstd{= Sepal.Width))}
202 | \hlopt{+} \hlkwd{geom_point}\hlstd{()}
203 | \hlstd{myplot} \hlkwb{<-} \hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= iris,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= Sepal.Length,} \hlkwc{y} \hlstd{= Sepal.Width))}
204 | \hlstd{myplot} \hlopt{+} \hlkwd{geom_point}\hlstd{()}
205 | \end{alltt}
206 | \end{kframe}
207 | \end{knitrout}
208 |
209 | \begin{itemize}
210 | \item Specify the data and variables inside the \texttt{ggplot} function.
211 | \item Anything else that goes in here becomes a global setting.
212 | \item Then add layers of geometric objects, statistical models, and panels.
213 | \end{itemize}
214 | \end{frame}
215 |
216 | % --------------------------------------------------------------
217 | \begin{frame}[fragile]
218 | \frametitle{Quick note}
219 | \begin{itemize}
220 | \item Never use \texttt{qplot} - short for quick plot.
221 | \item You'll end up unlearning and relearning a good bit.
222 | \end{itemize}
223 |
224 | \end{frame}
225 |
226 |
227 | % --------------------------------------------------------------
228 | \begin{frame}[fragile]
229 | \frametitle{Increase the size of points}
230 | \begin{knitrout}\footnotesize
231 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
232 | \begin{alltt}
233 | \hlkwd{ggplot}\hlstd{(}\hlkwc{data} \hlstd{= iris,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= Sepal.Length,} \hlkwc{y} \hlstd{= Sepal.Width))} \hlopt{+}
234 | \hlkwd{geom_point}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)}
235 | \end{alltt}
236 | \end{kframe}
237 | \includegraphics[width=.75\linewidth]{figure/first_plot_size_}
238 |
239 | \end{knitrout}
240 |
241 | \end{frame}
242 |
243 | % --------------------------------------------------------------
244 | \begin{frame}[fragile]
245 | \frametitle{Add some color}
246 | \begin{knitrout}\footnotesize
247 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
248 | \begin{alltt}
249 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
250 | \hlkwd{geom_point}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)}
251 | \end{alltt}
252 | \end{kframe}
253 | \includegraphics[width=.75\linewidth]{figure/first_plot_color_}
254 |
255 | \end{knitrout}
256 |
257 | \end{frame}
258 |
259 | % --------------------------------------------------------------
260 | \begin{frame}[fragile]
261 | \frametitle{Differentiate points by shape}
262 | \begin{knitrout}\footnotesize
263 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
264 | \begin{alltt}
265 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
266 | \hlkwd{geom_point}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{shape} \hlstd{= Species),} \hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)}
267 | \end{alltt}
268 | \end{kframe}
269 | \includegraphics[width=.75\linewidth]{figure/first_plot_shape_}
270 |
271 | \end{knitrout}
272 |
273 | \end{frame}
274 |
275 | % --------------------------------------------------------------
276 | \begin{frame}[fragile]
277 | \frametitle{Exercise 1}
278 | \begin{knitrout}\footnotesize
279 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
280 | \begin{alltt}
281 | \hlcom{# Make a small sample of the diamonds dataset}
282 | \hlstd{d2} \hlkwb{<-} \hlstd{diamonds[}\hlkwd{sample}\hlstd{(}\hlnum{1}\hlopt{:}\hlkwd{dim}\hlstd{(diamonds)[}\hlnum{1}\hlstd{],} \hlnum{1000}\hlstd{), ]}
283 | \end{alltt}
284 | \end{kframe}
285 | \end{knitrout}
286 |
287 | Then generate this plot below.
288 |
289 | \begin{knitrout}\footnotesize
290 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
291 | \includegraphics[width=.75\linewidth]{figure/ex1}
292 |
293 | \end{knitrout}
294 |
295 | \end{frame}
296 | % --------------------------------------------------------------
297 | \section*{Box plots}
298 | \frame{\sectionpage}
299 |
300 | \begin{frame}[fragile]
301 | See \texttt{?geom\_boxplot} for list of options
302 | \begin{knitrout}\footnotesize
303 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
304 | \begin{alltt}
305 | \hlkwd{library}\hlstd{(MASS)}
306 | \hlkwd{ggplot}\hlstd{(birthwt,} \hlkwd{aes}\hlstd{(}\hlkwd{factor}\hlstd{(race), bwt))} \hlopt{+} \hlkwd{geom_boxplot}\hlstd{()}
307 | \end{alltt}
308 | \end{kframe}
309 | \includegraphics[width=.75\linewidth]{figure/boxplots1_}
310 |
311 | \end{knitrout}
312 |
313 | \end{frame}
314 |
315 |
316 | % --------------------------------------------------------------
317 | \section*{Histograms}
318 | \frame{\sectionpage}
319 |
320 | % --------------------------------------------------------------
321 | \begin{frame}[fragile]
322 | See \texttt{?geom\_histogram} for list of options
323 | \begin{knitrout}\footnotesize
324 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
325 | \begin{alltt}
326 | \hlstd{h} \hlkwb{<-} \hlkwd{ggplot}\hlstd{(faithful,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= waiting))}
327 | \hlstd{h} \hlopt{+} \hlkwd{geom_histogram}\hlstd{(}\hlkwc{binwidth} \hlstd{=} \hlnum{30}\hlstd{,} \hlkwc{colour} \hlstd{=} \hlstr{"black"}\hlstd{)}
328 | \end{alltt}
329 | \end{kframe}
330 | \includegraphics[width=.75\linewidth]{figure/histogr_}
331 |
332 | \end{knitrout}
333 |
334 | \end{frame}
335 |
336 | % --------------------------------------------------------------
337 | \begin{frame}[fragile]
338 | \begin{knitrout}\footnotesize
339 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
340 | \begin{alltt}
341 | \hlstd{h} \hlkwb{<-} \hlkwd{ggplot}\hlstd{(faithful,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= waiting))}
342 | \hlstd{h} \hlopt{+} \hlkwd{geom_histogram}\hlstd{(}\hlkwc{binwidth} \hlstd{=} \hlnum{8}\hlstd{,} \hlkwc{fill} \hlstd{=} \hlstr{"steelblue"}\hlstd{,}
343 | \hlkwc{colour} \hlstd{=} \hlstr{"black"}\hlstd{)}
344 | \end{alltt}
345 | \end{kframe}
346 | \includegraphics[width=.75\linewidth]{figure/histogra_}
347 |
348 | \end{knitrout}
349 |
350 | \end{frame}
351 |
352 | % --------------------------------------------------------------
353 | \section*{Line plots}
354 | \frame{\sectionpage}
355 |
356 | \begin{frame}[fragile]
357 |
358 |
359 | % climate <- read.csv(text = RCurl::getURL('https://raw.github.com/karthikram/ggplot-lecture/master/climate.csv'))
360 | \begin{knitrout}\footnotesize
361 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
362 | \begin{alltt}
363 | \hlstd{climate} \hlkwb{<-} \hlkwd{read.csv}\hlstd{(}\hlstr{"climate.csv"}\hlstd{,} \hlkwc{header} \hlstd{= T)}
364 | \hlkwd{ggplot}\hlstd{(climate,} \hlkwd{aes}\hlstd{(Year, Anomaly10y))} \hlopt{+}
365 | \hlkwd{geom_line}\hlstd{()}
366 | \end{alltt}
367 | \end{kframe}
368 | \includegraphics[width=.75\linewidth]{figure/linea_}
369 |
370 | \end{knitrout}
371 |
372 | \begin{flushright}
373 | \begingroup
374 | \fontsize{6pt}{12pt}\selectfont
375 | \begin{verbatim}
376 | climate <- read.csv(text =
377 | RCurl::getURL('https://raw.github.com/karthikram/ggplot-lecture/master/climate.csv'))
378 | \end{verbatim}
379 | \endgroup
380 | \end{flushright}
381 | \end{frame}
382 |
383 | \begin{frame}[fragile]
384 | We can also plot confidence regions
385 | \begin{knitrout}\footnotesize
386 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
387 | \begin{alltt}
388 | \hlkwd{ggplot}\hlstd{(climate,} \hlkwd{aes}\hlstd{(Year, Anomaly10y))} \hlopt{+}
389 | \hlkwd{geom_ribbon}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{ymin} \hlstd{= Anomaly10y} \hlopt{-} \hlstd{Unc10y,}
390 | \hlkwc{ymax} \hlstd{= Anomaly10y} \hlopt{+} \hlstd{Unc10y),}
391 | \hlkwc{fill} \hlstd{=} \hlstr{"blue"}\hlstd{,} \hlkwc{alpha} \hlstd{=} \hlnum{.1}\hlstd{)} \hlopt{+}
392 | \hlkwd{geom_line}\hlstd{(}\hlkwc{color} \hlstd{=} \hlstr{"steelblue"}\hlstd{)}
393 | \end{alltt}
394 | \end{kframe}
395 | \includegraphics[width=.75\linewidth]{figure/lineb_}
396 |
397 | \end{knitrout}
398 |
399 | \end{frame}
400 |
401 | % --------------------------------------------------------------
402 | \begin{frame}[fragile]
403 | \frametitle{Exercise 2}
404 | \begin{itemize}
405 | \item Modify the previous plot and change it such that there are three lines instead of one with a confidence band.
406 | \begin{knitrout}\footnotesize
407 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
408 | \includegraphics[width=.75\linewidth]{figure/ex2}
409 |
410 | \end{knitrout}
411 |
412 |
413 | \end{itemize}
414 | \end{frame}
415 |
416 |
417 | % --------------------------------------------------------------
418 | \section*{Bar plots}
419 | \frame{\sectionpage}
420 |
421 | % --------------------------------------------------------------
422 | \begin{frame}[fragile]
423 | \begin{knitrout}\footnotesize
424 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
425 | \begin{alltt}
426 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Species, Sepal.Length))} \hlopt{+}
427 | \hlkwd{geom_bar}\hlstd{(}\hlkwc{stat} \hlstd{=} \hlstr{"identity"}\hlstd{)}
428 | \end{alltt}
429 | \end{kframe}
430 | \includegraphics[width=.75\linewidth]{figure/barone_}
431 |
432 | \end{knitrout}
433 |
434 | \end{frame}
435 |
436 | % --------------------------------------------------------------
437 | \begin{frame}[fragile]
438 | \begin{knitrout}\footnotesize
439 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
440 | \begin{alltt}
441 | \hlstd{df} \hlkwb{<-} \hlkwd{melt}\hlstd{(iris,} \hlkwc{id.vars} \hlstd{=} \hlstr{"Species"}\hlstd{)}
442 | \hlkwd{ggplot}\hlstd{(df,} \hlkwd{aes}\hlstd{(Species, value,} \hlkwc{fill} \hlstd{= variable))} \hlopt{+}
443 | \hlkwd{geom_bar}\hlstd{(}\hlkwc{stat} \hlstd{=} \hlstr{"identity"}\hlstd{)}
444 | \end{alltt}
445 | \end{kframe}
446 | \includegraphics[width=.75\linewidth]{figure/bartwo_}
447 |
448 | \end{knitrout}
449 |
450 | \end{frame}
451 |
452 |
453 |
454 | % --------------------------------------------------------------
455 | \section*{plyr and reshape are key for using \texttt{R}}
456 | \frame{\sectionpage}
457 |
458 | \begin{frame}[fragile]
459 | \frametitle{plyr and reshape}
460 | These two packages are the swiss army knives of R.
461 | \begin{itemize}
462 | \item \texttt{plyr}
463 | \begin{enumerate}
464 | \item ddply
465 | \item llply
466 | \item join
467 | \end{enumerate}
468 | \item \texttt{reshape}.
469 | \begin{enumerate}
470 | \item melt
471 | \item dcast
472 | \item acast
473 | \end{enumerate}
474 | \end{itemize}
475 | \end{frame}
476 |
477 |
478 | % --------------------------------------------------------------
479 | \begin{frame}[fragile]
480 | \begin{knitrout}\footnotesize
481 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
482 | \begin{alltt}
483 | \hlstd{iris[}\hlnum{1}\hlopt{:}\hlnum{2}\hlstd{, ]}
484 | \end{alltt}
485 | \begin{verbatim}
486 | ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
487 | ## 1 5.1 3.5 1.4 0.2 setosa
488 | ## 2 4.9 3.0 1.4 0.2 setosa
489 | \end{verbatim}
490 | \begin{alltt}
491 | \hlstd{df} \hlkwb{<-} \hlkwd{melt}\hlstd{(iris,} \hlkwc{id.vars} \hlstd{=} \hlstr{"Species"}\hlstd{)}
492 | \hlstd{df[}\hlnum{1}\hlopt{:}\hlnum{2}\hlstd{, ]}
493 | \end{alltt}
494 | \begin{verbatim}
495 | ## Species variable value
496 | ## 1 setosa Sepal.Length 5.1
497 | ## 2 setosa Sepal.Length 4.9
498 | \end{verbatim}
499 | \end{kframe}
500 | \end{knitrout}
501 |
502 | \end{frame}
503 |
504 |
505 | % --------------------------------------------------------------
506 | \begin{frame}[fragile]
507 | \begin{knitrout}\footnotesize
508 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
509 | \begin{alltt}
510 | \hlkwd{ggplot}\hlstd{(df,} \hlkwd{aes}\hlstd{(Species, value,} \hlkwc{fill} \hlstd{= variable))} \hlopt{+}
511 | \hlkwd{geom_bar}\hlstd{(}\hlkwc{stat} \hlstd{=} \hlstr{"identity"}\hlstd{,} \hlkwc{position} \hlstd{=} \hlstr{"dodge"}\hlstd{)}
512 | \end{alltt}
513 | \end{kframe}
514 | \includegraphics[width=.75\linewidth]{figure/barthree_}
515 |
516 | \end{knitrout}
517 |
518 | \end{frame}
519 |
520 | % --------------------------------------------------------------
521 | \begin{frame}[fragile]
522 | \frametitle{Exercise 3}
523 | Using the d2 dataset you created earlier, generate this plot below. Take a quick look at the data first to see if it needs to be binned.
524 | \begin{knitrout}\footnotesize
525 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
526 | \includegraphics[width=.75\linewidth]{figure/ex3}
527 |
528 | \end{knitrout}
529 |
530 | \end{frame}
531 |
532 | % --------------------------------------------------------------
533 | \begin{frame}[fragile]
534 | \frametitle{Exercise 4}
535 | \begin{itemize}
536 | \item Using the climate dataset, create a new variable called sign. Make it logical (true/false) based on the sign of Anomaly10y.
537 | \item Plot a bar plot and use \texttt{sign} variable as the fill.\\
538 | \begin{knitrout}\footnotesize
539 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
540 | \includegraphics[width=.75\linewidth]{figure/ex4}
541 |
542 | \end{knitrout}
543 |
544 |
545 | \end{itemize}
546 | \end{frame}
547 |
548 |
549 | % --------------------------------------------------------------
550 | \section*{Density Plots}
551 | \frame{\sectionpage}
552 |
553 | \begin{frame}[fragile]
554 | \frametitle{Density plots}
555 | \begin{knitrout}\footnotesize
556 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
557 | \begin{alltt}
558 | \hlkwd{ggplot}\hlstd{(faithful,} \hlkwd{aes}\hlstd{(waiting))} \hlopt{+} \hlkwd{geom_density}\hlstd{()}
559 | \end{alltt}
560 | \end{kframe}
561 | \includegraphics[width=.75\linewidth]{figure/densityone_}
562 |
563 | \end{knitrout}
564 |
565 | \end{frame}
566 |
567 | % --------------------------------------------------------------
568 | \begin{frame}[fragile]
569 | \frametitle{Density plots}
570 | \begin{knitrout}\footnotesize
571 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
572 | \begin{alltt}
573 | \hlkwd{ggplot}\hlstd{(faithful,} \hlkwd{aes}\hlstd{(waiting))} \hlopt{+}
574 | \hlkwd{geom_density}\hlstd{(}\hlkwc{fill} \hlstd{=} \hlstr{"blue"}\hlstd{,} \hlkwc{alpha} \hlstd{=} \hlnum{0.1}\hlstd{)}
575 | \end{alltt}
576 | \end{kframe}
577 | \includegraphics[width=.75\linewidth]{figure/densityonefove_}
578 |
579 | \end{knitrout}
580 |
581 | \end{frame}
582 |
583 |
584 |
585 | % --------------------------------------------------------------
586 | \begin{frame}[fragile]
587 | \begin{knitrout}\footnotesize
588 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
589 | \begin{alltt}
590 | \hlkwd{ggplot}\hlstd{(faithful,} \hlkwd{aes}\hlstd{(waiting))} \hlopt{+}
591 | \hlkwd{geom_line}\hlstd{(}\hlkwc{stat} \hlstd{=} \hlstr{"density"}\hlstd{)}
592 | \end{alltt}
593 | \end{kframe}
594 | \includegraphics[width=.75\linewidth]{figure/densitytwo___}
595 |
596 | \end{knitrout}
597 |
598 | \end{frame}
599 |
600 |
601 | % --------------------------------------------------------------
602 | \section*{Mapping Variables to colors}
603 | \frame{\sectionpage}
604 |
605 |
606 | % --------------------------------------------------------------
607 | \begin{frame}[fragile]
608 | \frametitle{Colors}
609 | \begin{knitrout}\footnotesize
610 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
611 | \begin{alltt}
612 | \hlkwd{aes}\hlstd{(}\hlkwc{color} \hlstd{= variable)}
613 | \hlkwd{aes}\hlstd{(}\hlkwc{color} \hlstd{=} \hlstr{"black"}\hlstd{)}
614 | \hlcom{# Or add it as a scale}
615 | \hlkwd{scale_fill_manual}\hlstd{(}\hlkwc{values} \hlstd{=} \hlkwd{c}\hlstd{(}\hlstr{"color1"}\hlstd{,} \hlstr{"color2"}\hlstd{))}
616 | \end{alltt}
617 | \end{kframe}
618 | \end{knitrout}
619 |
620 | \end{frame}
621 |
622 |
623 | % --------------------------------------------------------------
624 | \begin{frame}[fragile]
625 | \frametitle{The RColorBrewer package}
626 | \begin{knitrout}\footnotesize
627 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
628 | \begin{alltt}
629 | \hlkwd{library}\hlstd{(RColorBrewer)}
630 | \hlkwd{display.brewer.all}\hlstd{()}
631 | \end{alltt}
632 | \end{kframe}
633 | \end{knitrout}
634 |
635 | \includegraphics[scale=0.25]{images/color_palette.png}
636 | \end{frame}
637 |
638 | % --------------------------------------------------------------
639 | \begin{frame}[fragile]
640 | \frametitle{Using a color brewer palette}
641 | \begin{knitrout}\footnotesize
642 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
643 | \begin{alltt}
644 | \hlstd{df} \hlkwb{<-} \hlkwd{melt}\hlstd{(iris,} \hlkwc{id.vars} \hlstd{=} \hlstr{"Species"}\hlstd{)}
645 | \hlkwd{ggplot}\hlstd{(df,} \hlkwd{aes}\hlstd{(Species, value,} \hlkwc{fill} \hlstd{= variable))} \hlopt{+}
646 | \hlkwd{geom_bar}\hlstd{(}\hlkwc{stat} \hlstd{=} \hlstr{"identity"}\hlstd{,} \hlkwc{position} \hlstd{=} \hlstr{"dodge"}\hlstd{)} \hlopt{+}
647 | \hlkwd{scale_fill_brewer}\hlstd{(}\hlkwc{palette} \hlstd{=} \hlstr{"Set1"}\hlstd{)}
648 | \end{alltt}
649 | \end{kframe}
650 | \includegraphics[width=.75\linewidth]{figure/barcolors}
651 |
652 | \end{knitrout}
653 |
654 | \end{frame}
655 |
656 | % --------------------------------------------------------------
657 | \begin{frame}[fragile]
658 | \frametitle{Manual color scale}
659 | \begin{knitrout}\footnotesize
660 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
661 | \begin{alltt}
662 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
663 | \hlkwd{geom_point}\hlstd{()} \hlopt{+}
664 | \hlkwd{facet_grid}\hlstd{(Species} \hlopt{~} \hlstd{.)} \hlopt{+}
665 | \hlkwd{scale_color_manual}\hlstd{(}\hlkwc{values} \hlstd{=} \hlkwd{c}\hlstd{(}\hlstr{"red"}\hlstd{,} \hlstr{"green"}\hlstd{,} \hlstr{"blue"}\hlstd{))}
666 | \end{alltt}
667 | \end{kframe}
668 | \includegraphics[width=.75\linewidth]{figure/facetgridcolors}
669 |
670 | \end{knitrout}
671 |
672 | \end{frame}
673 |
674 | % --------------------------------------------------------------
675 | \begin{frame}[fragile]
676 | \frametitle{Refer to a color chart for beautful visualizations}
677 | \url{http://tools.medialab.sciences-po.fr/iwanthue/}
678 | \includegraphics[scale=0.25]{images/color_schemes.png}
679 | \end{frame}
680 |
681 | % -----------------------------------
682 |
683 | \section*{Faceting}
684 | \frame{\sectionpage}
685 |
686 | \begin{frame}[fragile]
687 | \frametitle{Faceting along columns}
688 | \begin{knitrout}\footnotesize
689 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
690 | \begin{alltt}
691 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
692 | \hlkwd{geom_point}\hlstd{()} \hlopt{+}
693 | \hlkwd{facet_grid}\hlstd{(Species} \hlopt{~} \hlstd{.)}
694 | \end{alltt}
695 | \end{kframe}
696 | \includegraphics[width=.75\linewidth]{figure/facetgrid1}
697 |
698 | \end{knitrout}
699 |
700 | \end{frame}
701 |
702 | % --------------------------------------------------------------
703 | \begin{frame}[fragile]
704 | \frametitle{and along rows}
705 | \begin{knitrout}\footnotesize
706 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
707 | \begin{alltt}
708 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
709 | \hlkwd{geom_point}\hlstd{()} \hlopt{+}
710 | \hlkwd{facet_grid}\hlstd{(.} \hlopt{~} \hlstd{Species)}
711 | \end{alltt}
712 | \end{kframe}
713 | \includegraphics[width=.75\linewidth]{figure/facet_grid2}
714 |
715 | \end{knitrout}
716 |
717 | \end{frame}
718 |
719 | % --------------------------------------------------------------
720 | \begin{frame}[fragile]
721 | \frametitle{or just wrap your panels}
722 | \begin{knitrout}\footnotesize
723 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
724 | \begin{alltt}
725 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
726 | \hlkwd{geom_point}\hlstd{()} \hlopt{+}
727 | \hlkwd{facet_wrap}\hlstd{(} \hlopt{~} \hlstd{Species)}
728 | \end{alltt}
729 | \end{kframe}
730 | \includegraphics[width=.75\linewidth]{figure/facet_wrap}
731 |
732 | \end{knitrout}
733 |
734 | \end{frame}
735 |
736 | % --------------------------------------------------------------
737 | \section*{Adding smoothers}
738 | \frame{\sectionpage}
739 |
740 |
741 | % --------------------------------------------------------------
742 | \begin{frame}[fragile]
743 | \begin{knitrout}\footnotesize
744 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
745 | \begin{alltt}
746 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
747 | \hlkwd{geom_point}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{shape} \hlstd{= Species),} \hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)} \hlopt{+}
748 | \hlkwd{geom_smooth}\hlstd{(}\hlkwc{method} \hlstd{=} \hlstr{"lm"}\hlstd{)}
749 | \end{alltt}
750 | \end{kframe}
751 | \includegraphics[width=.75\linewidth]{figure/adding_stats_}
752 |
753 | \end{knitrout}
754 |
755 | \end{frame}
756 |
757 | % --------------------------------------------------------------
758 | \begin{frame}[fragile]
759 | \begin{knitrout}\footnotesize
760 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
761 | \begin{alltt}
762 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
763 | \hlkwd{geom_point}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{shape} \hlstd{= Species),} \hlkwc{size} \hlstd{=} \hlnum{3}\hlstd{)} \hlopt{+}
764 | \hlkwd{geom_smooth}\hlstd{(}\hlkwc{method} \hlstd{=} \hlstr{"lm"}\hlstd{)} \hlopt{+}
765 | \hlkwd{facet_grid}\hlstd{(.} \hlopt{~} \hlstd{Species)}
766 | \end{alltt}
767 | \end{kframe}
768 | \includegraphics[width=.75\linewidth]{figure/adding_stats2_}
769 |
770 | \end{knitrout}
771 |
772 | \end{frame}
773 |
774 | % --------------------------------------------------------------
775 | \section*{Themes}
776 | \frame{\sectionpage}
777 |
778 | % --------------------------------------------------------------
779 | \begin{frame}[fragile]
780 | \frametitle{Adding themes}
781 | Themes are a great way to define custom plots.
782 | \begin{knitrout}\footnotesize
783 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
784 | \begin{alltt}
785 | \hlopt{+}\hlkwd{theme}\hlstd{()}
786 | \hlcom{# see ?theme() for more options}
787 | \end{alltt}
788 | \end{kframe}
789 | \end{knitrout}
790 |
791 |
792 | \end{frame}
793 |
794 |
795 | % --------------------------------------------------------------
796 | \begin{frame}[fragile]
797 | \frametitle{A themed plot}
798 | \begin{knitrout}\footnotesize
799 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
800 | \begin{alltt}
801 | \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
802 | \hlkwd{geom_point}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{1.2}\hlstd{,} \hlkwc{shape} \hlstd{=} \hlnum{16}\hlstd{)} \hlopt{+}
803 | \hlkwd{facet_wrap}\hlstd{(} \hlopt{~} \hlstd{Species)} \hlopt{+}
804 | \hlkwd{theme}\hlstd{(}\hlkwc{legend.key} \hlstd{=} \hlkwd{element_rect}\hlstd{(}\hlkwc{fill} \hlstd{=} \hlnum{NA}\hlstd{),}
805 | \hlkwc{legend.position} \hlstd{=} \hlstr{"bottom"}\hlstd{,}
806 | \hlkwc{strip.background} \hlstd{=} \hlkwd{element_rect}\hlstd{(}\hlkwc{fill} \hlstd{=} \hlnum{NA}\hlstd{),}
807 | \hlkwc{axis.title.y} \hlstd{=} \hlkwd{element_text}\hlstd{(}\hlkwc{angle} \hlstd{=} \hlnum{0}\hlstd{))}
808 | \end{alltt}
809 | \end{kframe}
810 | \end{knitrout}
811 |
812 | \end{frame}
813 |
814 | % --------------------------------------------------------------
815 | \begin{frame}[fragile]
816 | \frametitle{Adding themes}
817 | \begin{knitrout}\footnotesize
818 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
819 | \includegraphics[width=.75\linewidth]{figure/facet_wrap_theme_execc}
820 |
821 | \end{knitrout}
822 |
823 | \end{frame}
824 |
825 | % --------------------------------------------------------------
826 | \begin{frame}[fragile]
827 | \frametitle{ggthemes library}
828 | \begin{knitrout}\footnotesize
829 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
830 | \begin{alltt}
831 | \hlkwd{install.packages}\hlstd{(}\hlstr{"ggthemes"}\hlstd{)}
832 | \hlkwd{library}\hlstd{(ggthemes)}
833 | \hlcom{# Then add one of these themes to your plot}
834 | \hlopt{+}\hlkwd{theme_stata}\hlstd{()}
835 | \hlopt{+}\hlkwd{theme_excel}\hlstd{()}
836 | \hlopt{+}\hlkwd{theme_wsj}\hlstd{()}
837 | \hlopt{+}\hlkwd{theme_solarized}\hlstd{()}
838 | \end{alltt}
839 | \end{kframe}
840 | \end{knitrout}
841 |
842 | \end{frame}
843 |
844 |
845 | % --------------------------------------------------------------
846 | \section*{Create functions to automate your plotting}
847 | \frame{\sectionpage}
848 |
849 | % --------------------------------------------------------------
850 | \begin{frame}[fragile]
851 | \frametitle{Write functions for day to day plots}
852 | \begin{knitrout}\footnotesize
853 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
854 | \begin{alltt}
855 | \hlstd{my_custom_plot} \hlkwb{<-} \hlkwa{function}\hlstd{(}\hlkwc{df}\hlstd{,} \hlkwc{title} \hlstd{=} \hlstr{""}\hlstd{,} \hlkwc{...}\hlstd{) \{}
856 | \hlkwd{ggplot}\hlstd{(df, ...)} \hlopt{+}
857 | \hlkwd{ggtitle}\hlstd{(title)} \hlopt{+}
858 | \hlkwd{whatever_geoms}\hlstd{()} \hlopt{+}
859 | \hlkwd{theme}\hlstd{(...)}
860 | \hlstd{\}}
861 | \end{alltt}
862 | \end{kframe}
863 | \end{knitrout}
864 |
865 |
866 | Then just call your function to generate a plot.
867 | It's a lot easier to fix one function that do it over and over for many plots
868 | \begin{knitrout}\footnotesize
869 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
870 | \begin{alltt}
871 | \hlstd{plot1} \hlkwb{<-} \hlkwd{my_custom_plot}\hlstd{(dataset1,} \hlkwc{title} \hlstd{=} \hlstr{"Figure 1"}\hlstd{)}
872 | \end{alltt}
873 | \end{kframe}
874 | \end{knitrout}
875 |
876 |
877 |
878 | \end{frame}
879 |
880 | % --------------------------------------------------------------
881 | \section*{Scales}
882 | \frame{\sectionpage}
883 |
884 | % --------------------------------------------------------------
885 | \begin{frame}[fragile]
886 | \frametitle{Commonly used scales}
887 | \begin{knitrout}\footnotesize
888 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
889 | \begin{alltt}
890 | \hlkwd{scale_fill_discrete}\hlstd{();} \hlkwd{scale_colour_discrete}\hlstd{()}
891 | \hlkwd{scale_fill_hue}\hlstd{();} \hlkwd{scale_color_hue}\hlstd{()}
892 | \hlkwd{scale_fill_manual}\hlstd{();} \hlkwd{scale_color_manual}\hlstd{()}
893 | \hlkwd{scale_fill_brewer}\hlstd{();} \hlkwd{scale_color_brewer}\hlstd{()}
894 | \hlkwd{scale_linetype}\hlstd{();} \hlkwd{scale_shape_manual}\hlstd{()}
895 | \end{alltt}
896 | \end{kframe}
897 | \end{knitrout}
898 |
899 | \end{frame}
900 |
901 | % --------------------------------------------------------------
902 | \begin{frame}[fragile]
903 | \frametitle{Adding a continuous scale}
904 | \begin{knitrout}\footnotesize
905 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
906 | \begin{alltt}
907 | \hlkwd{library}\hlstd{(MASS)}
908 | \hlkwd{ggplot}\hlstd{(birthwt,} \hlkwd{aes}\hlstd{(}\hlkwd{factor}\hlstd{(race), bwt))} \hlopt{+}
909 | \hlkwd{geom_boxplot}\hlstd{(}\hlkwc{width} \hlstd{=} \hlnum{.2}\hlstd{)} \hlopt{+}
910 | \hlkwd{scale_y_continuous}\hlstd{(}\hlkwc{labels} \hlstd{= (}\hlkwd{paste0}\hlstd{(}\hlnum{1}\hlopt{:}\hlnum{4}\hlstd{,} \hlstr{" Kg"}\hlstd{)),}
911 | \hlkwc{breaks} \hlstd{=} \hlkwd{seq}\hlstd{(}\hlnum{1000}\hlstd{,} \hlnum{4000}\hlstd{,} \hlkwc{by} \hlstd{=} \hlnum{1000}\hlstd{))}
912 | \end{alltt}
913 | \end{kframe}
914 | \includegraphics[width=.75\linewidth]{figure/boxplots3_}
915 |
916 | \end{knitrout}
917 |
918 | \end{frame}
919 |
920 |
921 | % --------------------------------------------------------------
922 | \begin{frame}[fragile]
923 | \frametitle{Another continuous scale with custom labels}
924 | \begin{knitrout}\footnotesize
925 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
926 | \begin{alltt}
927 | \hlcom{# Assign the plot to an object}
928 | \hlstd{dd} \hlkwb{<-} \hlkwd{ggplot}\hlstd{(iris,} \hlkwd{aes}\hlstd{(Sepal.Length, Sepal.Width,} \hlkwc{color} \hlstd{= Species))} \hlopt{+}
929 | \hlkwd{geom_point}\hlstd{(}\hlkwc{size} \hlstd{=} \hlnum{4}\hlstd{,} \hlkwc{shape} \hlstd{=} \hlnum{16}\hlstd{)} \hlopt{+}
930 | \hlkwd{facet_grid}\hlstd{(.} \hlopt{~}\hlstd{Species)}
931 | \hlcom{# Now add a scale}
932 | \hlstd{dd} \hlopt{+}
933 | \hlkwd{scale_y_continuous}\hlstd{(}\hlkwc{breaks} \hlstd{=} \hlkwd{seq}\hlstd{(}\hlnum{2}\hlstd{,} \hlnum{8}\hlstd{,} \hlkwc{by} \hlstd{=} \hlnum{1}\hlstd{),}
934 | \hlkwc{labels} \hlstd{=} \hlkwd{paste0}\hlstd{(}\hlnum{2}\hlopt{:}\hlnum{8}\hlstd{,} \hlstr{" cm"}\hlstd{))}
935 | \end{alltt}
936 | \end{kframe}
937 | \end{knitrout}
938 |
939 | \end{frame}
940 |
941 |
942 | % --------------------------------------------------------------
943 | \begin{frame}[fragile]
944 | \frametitle{gradients}
945 | \begin{knitrout}\footnotesize
946 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
947 | \begin{alltt}
948 | \hlstd{h} \hlopt{+} \hlkwd{geom_histogram}\hlstd{(} \hlkwd{aes}\hlstd{(}\hlkwc{fill} \hlstd{= ..count..),} \hlkwc{color}\hlstd{=}\hlstr{"black"}\hlstd{)} \hlopt{+}
949 | \hlkwd{scale_fill_gradient}\hlstd{(}\hlkwc{low}\hlstd{=}\hlstr{"green"}\hlstd{,} \hlkwc{high}\hlstd{=}\hlstr{"red"}\hlstd{)}
950 | \end{alltt}
951 | \end{kframe}
952 | \includegraphics[width=.75\linewidth]{figure/scale_2}
953 |
954 | \end{knitrout}
955 |
956 | \end{frame}
957 |
958 |
959 |
960 | % --------------------------------------------------------------
961 | \section*{Publication quality figures}
962 | \frame{\sectionpage}
963 |
964 | % How to save your plots
965 | % --------------------------------------------------------------
966 | \begin{frame}[fragile]
967 | \begin{itemize}
968 | \item If the plot is on your screen
969 | \begin{knitrout}\footnotesize
970 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
971 | \begin{alltt}
972 | \hlkwd{ggsave}\hlstd{(}\hlstr{"~/path/to/figure/filename.png"}\hlstd{)}
973 | \end{alltt}
974 | \end{kframe}
975 | \end{knitrout}
976 |
977 | \item If your plot is assigned to an object
978 | \begin{knitrout}\footnotesize
979 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
980 | \begin{alltt}
981 | \hlkwd{ggsave}\hlstd{(plot1,} \hlkwc{file} \hlstd{=} \hlstr{"~/path/to/figure/filename.png"}\hlstd{)}
982 | \end{alltt}
983 | \end{kframe}
984 | \end{knitrout}
985 |
986 |
987 | \item Specify a size
988 | \begin{knitrout}\footnotesize
989 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
990 | \begin{alltt}
991 | \hlkwd{ggsave}\hlstd{(}\hlkwc{file} \hlstd{=} \hlstr{"/path/to/figure/filename.png"}\hlstd{,} \hlkwc{width} \hlstd{=} \hlnum{6}\hlstd{,}
992 | \hlkwc{height} \hlstd{=}\hlnum{4}\hlstd{)}
993 | \end{alltt}
994 | \end{kframe}
995 | \end{knitrout}
996 |
997 | \item or any format (pdf, png, eps, svg, jpg)
998 | \begin{knitrout}\footnotesize
999 | \definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
1000 | \begin{alltt}
1001 | \hlkwd{ggsave}\hlstd{(}\hlkwc{file} \hlstd{=} \hlstr{"/path/to/figure/filename.eps"}\hlstd{)}
1002 | \hlkwd{ggsave}\hlstd{(}\hlkwc{file} \hlstd{=} \hlstr{"/path/to/figure/filename.jpg"}\hlstd{)}
1003 | \hlkwd{ggsave}\hlstd{(}\hlkwc{file} \hlstd{=} \hlstr{"/path/to/figure/filename.pdf"}\hlstd{)}
1004 | \end{alltt}
1005 | \end{kframe}
1006 | \end{knitrout}
1007 |
1008 | \end{itemize}
1009 | \end{frame}
1010 |
1011 | % --------------------------------------------------------------
1012 | \begin{frame}[fragile]
1013 | \frametitle{Further help}
1014 | \begin{itemize}
1015 | \item You've just scratched the surface with ggplot2.
1016 | \item Practice
1017 | \item Read the docs (either locally in \texttt{R} or at \url{http://docs.ggplot2.org/current/})
1018 | \item Work together
1019 | \end{itemize}
1020 | \includegraphics[scale=.15]{images/chang_book.png}
1021 | \includegraphics[scale=.15]{images/hadley.png}
1022 | \end{frame}
1023 |
1024 | % --------------------------------------------------------------
1025 | % end, hope it was useful.
1026 | \end{document}
1027 |
--------------------------------------------------------------------------------
/images/chang_book.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karthik/ggplot-lecture/b2980ab6df3dc2053d3e7c46db44616d3db9c0c7/images/chang_book.png
--------------------------------------------------------------------------------
/images/color_palette.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karthik/ggplot-lecture/b2980ab6df3dc2053d3e7c46db44616d3db9c0c7/images/color_palette.png
--------------------------------------------------------------------------------
/images/color_schemes.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karthik/ggplot-lecture/b2980ab6df3dc2053d3e7c46db44616d3db9c0c7/images/color_schemes.png
--------------------------------------------------------------------------------
/images/git_repo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karthik/ggplot-lecture/b2980ab6df3dc2053d3e7c46db44616d3db9c0c7/images/git_repo.png
--------------------------------------------------------------------------------
/images/hadley.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karthik/ggplot-lecture/b2980ab6df3dc2053d3e7c46db44616d3db9c0c7/images/hadley.png
--------------------------------------------------------------------------------
/make.sh:
--------------------------------------------------------------------------------
1 |
2 | cd ~/Github/ggplot2-lecture/
3 | rm ggplot.tex
4 | Rscript -e "library(knitr); knit('ggplot.Rnw', quiet = TRUE)"
5 | pdflatex ggplot.tex
6 | open ggplot.pdf
7 | rm *.log
8 | rm *.nav
9 | rm *.out
10 | rm *.snm
11 | rm *.toc
12 | rm *.vrb
13 | # added a comment.
14 |
--------------------------------------------------------------------------------
/slides.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karthik/ggplot-lecture/b2980ab6df3dc2053d3e7c46db44616d3db9c0c7/slides.png
--------------------------------------------------------------------------------