-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit after splitting the original R appendixes into a separat…
…e book.
- Loading branch information
Showing
16 changed files
with
8,801 additions
and
21,819 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
% !Rnw root = appendix.main.Rnw | ||
|
||
<<echo=FALSE, include=FALSE>>= | ||
opts_chunk$set(opts_fig_wide) | ||
opts_knit$set(concordance=TRUE) | ||
@ | ||
|
||
\chapter[Plots with ggpplot, ggrepel and ggpmisc]{Plots with \ggplot, \ggrepel and \ggpmisc}\label{chap:R:plotting} | ||
|
||
\section{Packages used in this chapter} | ||
|
||
For executing the examples listed in this chapter you need first to load the following packages from the library: | ||
|
||
<<message=FALSE>>= | ||
library(ggplot2) | ||
library(ggrepel) | ||
library(ggpmisc) | ||
@ | ||
|
||
We set a font larger size than the default | ||
<<>>= | ||
theme_set(theme_grey(16)) | ||
@ | ||
<<echo=FALSE,include=FALSE>>= | ||
opts_chunk$set(opts_fig_narrow) | ||
@ | ||
|
||
<<eval=eval_diag, include=eval_diag, echo=eval_diag, cache=FALSE>>= | ||
opts_knit$get() | ||
search() | ||
getwd() | ||
@ | ||
|
||
\section[ggpmisc]{\ggpmisc} | ||
|
||
\subsection{New stats} | ||
|
||
Package \ggpmisc provides new stats: \code{stat\_peaks()}, \code{stat\_valleys()}, and \code{stat\_poly\_eq()}. Peaks and valleys are local (or global) maxima and minima. These stats return the $x$ and $y$ values at the peaks or valleys plus suitable labels, and default aesthetics that make easy their use with several different geoms, including \code{geom\_point}, \code{geom\_text}, \code{geom\_label}, \code{geom\_vline}, \code{geom\_hline} and \code{geom\_rug}, and also with geoms defined by package \ggrepel. Some examples follow. | ||
|
||
\subsection{Peaks and valleys} | ||
<<>>= | ||
lynx.df <- data.frame(year = as.numeric(time(lynx)), lynx = as.matrix(lynx)) | ||
@ | ||
|
||
<<>>= | ||
ggplot(lynx.df, aes(year, lynx)) + geom_line() + | ||
stat_peaks(colour = "red") + | ||
stat_peaks(geom = "text", colour = "red", | ||
vjust = -0.5, x.label.fmt = "%4.0f") + | ||
stat_valleys(colour = "blue") + | ||
stat_valleys(geom = "text", colour = "blue", | ||
vjust = 1.5, x.label.fmt = "%4.0f") + | ||
ylim(-100, 7300) | ||
@ | ||
|
||
<<>>= | ||
ggplot(lynx.df, aes(year, lynx)) + geom_line() + | ||
stat_peaks(colour = "red") + | ||
stat_peaks(geom = "rug", colour = "red") + | ||
stat_peaks(geom = "text", colour = "red", | ||
vjust = -0.5, x.label.fmt = "%4.0f") + | ||
ylim(NA, 7300) | ||
@ | ||
|
||
<<>>= | ||
ggplot(lynx.df, aes(year, lynx)) + geom_line() + | ||
stat_peaks(colour = "red") + | ||
stat_peaks(geom = "rug", colour = "red") + | ||
stat_valleys(colour = "blue") + | ||
stat_valleys(geom = "rug", colour = "blue") | ||
@ | ||
|
||
<<>>= | ||
ggplot(lynx.df, aes(year, lynx)) + geom_line() + | ||
stat_peaks(colour = "red") + | ||
stat_peaks(geom = "rug", colour = "red") + | ||
stat_peaks(geom = "text", colour = "red", | ||
hjust = -0.1, label.fmt = "%4.0f", | ||
angle = 90, size = rel(2), | ||
aes(label = paste(..y.label.., | ||
"skins in year", ..x.label..))) + | ||
stat_valleys(colour = "blue") + | ||
stat_valleys(geom = "rug", colour = "blue") + | ||
stat_valleys(geom = "text", colour = "blue", | ||
hjust = -0.1, vjust = 1, label.fmt = "%4.0f", | ||
angle = 90, size = rel(2), | ||
aes(label = paste(..y.label.., | ||
"skins in year", ..x.label..))) + | ||
ylim(NA, 10000) | ||
@ | ||
|
||
Of course, if one finds use for it, the peaks and/or valleys can be plotted on their own. | ||
|
||
<<>>= | ||
ggplot(lynx.df, aes(year, lynx)) + | ||
stat_peaks(geom = "line") + stat_valleys(geom = "line") | ||
@ | ||
\subsection{Learning and/or debugging} | ||
|
||
A very simple stat named \code{stat\_debug()} can save the work of adding print statements to the code of stats to get information about what data is being passed to the \code{compute\_group()} function. Because the code of this function is stored in a \code{ggproto} object, at the moment it is impossible to directly set breakpoints in it. This stat may also help users diagnose problems with the mapping of aesthetics in their code or just get a better idea of how the internals of \ggplot work. | ||
|
||
<<>>= | ||
ggplot(lynx.df, aes(year, lynx)) + geom_line() + | ||
stat_debug(alpha = 0.8) | ||
@ | ||
|
||
<<>>= | ||
lynx.df$century <- ifelse(lynx.df$year >= 1900, "XX", "XIX") | ||
ggplot(lynx.df, aes(year, lynx, color = century)) + | ||
geom_line() + | ||
stat_debug(alpha = 0.8, size = rel(2.5)) | ||
@ | ||
|
||
\section[ggrepel]{\ggrepel} | ||
|
||
\subsection{New geoms} | ||
|
||
Package \ggrepel provides two new geoms: \code{geom\_text\_repel} and \code{geom\_label\_repel}. They are used similarly to \code{geom\_text} and \code{geom\_label} but the text or labels ``repel'' each other so that they rarely overlap unless the plot is very crowded. | ||
|
||
|
||
<<>>= | ||
try(detach(package:ggpmisc)) | ||
try(detach(package:ggrepel)) | ||
try(detach(package:ggplot2)) | ||
@ | ||
|
Oops, something went wrong.