Skip to content

Commit

Permalink
Revise preface, edit R.plotting.Rnw to make sure it works with ggplot…
Browse files Browse the repository at this point in the history
…2 and write a few sections in new file R.more.plotting.Rnw.
  • Loading branch information
aphalo committed Jan 18, 2016
1 parent 6ace466 commit 1d227d3
Show file tree
Hide file tree
Showing 7 changed files with 27,176 additions and 5,548 deletions.
29 changes: 15 additions & 14 deletions R.more.plotting.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ Package \ggpmisc provides new stats: \code{stat\_peaks()}, \code{stat\_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",
stat_peaks(geom = "text", colour = "red",
vjust = -0.5, x.label.fmt = "%4.0f") +
stat_valleys(colour = "blue") +
stat_valleys(geom = "text", colour = "blue",
stat_valleys(geom = "text", colour = "blue",
vjust = 1.5, x.label.fmt = "%4.0f") +
ylim(-100, 7300)
@
Expand All @@ -57,7 +57,7 @@ ggplot(lynx.df, aes(year, lynx)) + geom_line() +
ggplot(lynx.df, aes(year, lynx)) + geom_line() +
stat_peaks(colour = "red") +
stat_peaks(geom = "rug", colour = "red") +
stat_peaks(geom = "text", colour = "red",
stat_peaks(geom = "text", colour = "red",
vjust = -0.5, x.label.fmt = "%4.0f") +
ylim(NA, 7300)
Expand All @@ -76,38 +76,39 @@ 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",
hjust = -0.1, label.fmt = "%4.0f",
angle = 90, size = rel(2),
aes(label = paste(..y.label..,
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",
hjust = -0.1, vjust = 1, label.fmt = "%4.0f",
angle = 90, size = rel(2),
aes(label = paste(..y.label..,
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.
Of course, if one finds use for it, the peaks and/or valleys can be plotted on their own. Here we plot an "envelope" using \code{geom\_line()}.

<<>>=
ggplot(lynx.df, aes(year, lynx)) +
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.
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 \code{stat\_debug()} 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)) +
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))
@
Expand All @@ -116,7 +117,7 @@ ggplot(lynx.df, aes(year, lynx, color = century)) +

\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.
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.


<<>>=
Expand Down
13 changes: 7 additions & 6 deletions appendixes.prj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TeX:RNW:UTF-8
152055803 0 -1 3385 -1 3387 25 25 1393 519 0 1 527 336 -1 -1 0 0 86 -1 -1 86 1 0 3387 -1 0 -1 0
abbrev.sty
TeX:STY
1060850 0 20 1 18 50 200 200 1593 649 0 0 417 -3381 -1 -1 0 0 2 0 0 2 1 0 50 18 0 0 0
1060850 0 20 1 18 50 200 200 1593 649 0 0 417 306 -1 -1 0 0 2 0 0 2 1 0 50 18 0 0 0
usingr.sty
TeX:STY
1060850 0 10 38 10 38 0 0 1691 494 0 0 321 170 -1 -1 0 0 1 17 17 1 1 0 38 10 0 0 0
Expand All @@ -25,13 +25,13 @@ TeX:RNW
1060859 0 -1 170 -1 174 25 25 1343 589 0 1 383 112 -1 -1 0 0 26 -1 -1 26 1 0 174 -1 0 -1 0
R.more.plotting.Rnw
TeX:RNW
286273531 0 -1 4072 -1 3109 200 200 1568 694 1 1 535 128 -1 -1 0 0 26 -1 -1 26 1 0 3109 -1 0 -1 0
286273531 0 -1 3695 -1 3715 200 200 1568 694 1 1 607 304 -1 -1 0 0 26 -1 -1 26 1 0 3715 -1 0 -1 0
:\aphalo\Documents\R bits and pieces\polynomials\polynomials.Rmd
DATA
5243122 0 0 1 0 1 0 0 1418 354 1 0 78 0 -1 -1 0 0 -1 -1 -1 -1 1 0 1 0 0 0 0
5243122 0 0 1 0 1 0 0 1418 354 1 0 78 0 -1 -1 0 0 4 0 0 4 1 0 1 0 0 0 0
:\aphalo\Documents\RPackages\ggpmisc\vignettes\examples.Rmd
DATA
5243122 1 176 1 181 1 25 25 1443 379 1 0 78 496 -1 -1 0 0 -1 -1 -1 -1 1 0 1 181 0 0 0
5243122 1 176 1 181 1 25 25 1443 379 1 0 78 496 -1 -1 0 0 4 0 0 4 1 0 1 181 0 0 0
R.plotting.Rnw
TeX:RNW
17838075 0 -1 34385 -1 34385 200 200 1568 694 1 1 94 448 -1 -1 0 0 26 -1 -1 26 1 0 34385 -1 0 -1 0
Expand All @@ -40,7 +40,7 @@ BibTeX
1049586 1 0 10 0 22 150 150 1543 599 0 1 230 0 -1 -1 0 0 23 0 0 23 1 0 22 0 0 -1 0
R.plot-manip.Rnw
TeX:RNW
17838075 0 -1 0 -1 1346 175 175 1568 624 0 1 50 497 -1 -1 0 0 40 -1 -1 40 1 0 1346 -1 0 -1 0
17838075 0 -1 0 -1 1346 175 175 1568 624 0 1 50 496 -1 -1 0 0 40 -1 -1 40 1 0 1346 -1 0 -1 0
R.maps.Rnw
TeX:RNW
17838075 0 -1 48 -1 10454 150 150 1543 599 0 1 41 512 -1 -1 0 0 53 -1 -1 53 1 0 10454 -1 0 -1 0
Expand All @@ -49,7 +49,7 @@ TeX:RNW
1060859 2 -1 28549 -1 28556 225 225 1593 719 0 1 1247 512 -1 -1 0 0 153 -1 -1 153 1 0 28556 -1 0 -1 0
using-r.main.tex
TeX:UTF-8
286273531 8 -1 10808 -1 10794 0 0 1393 449 0 1 41 256 -1 -1 0 0 104 -1 -1 104 1 0 10794 -1 0 -1 0
286273531 8 -1 10808 -1 10794 0 0 1393 449 0 1 230 256 -1 -1 0 0 104 -1 -1 104 1 0 10794 -1 0 -1 0
r4photobiology.sty
TeX:STY
269496306 0 53 1 16 1 0 0 1691 494 0 0 25 272 -1 -1 0 0 1 2 2 1 1 0 1 16 0 0 0
Expand Down Expand Up @@ -329,5 +329,6 @@ TeX:AUX
*R.scripts.Rnw
*R.data.Rnw
*R.plotting.Rnw
*R.more.plotting.Rnw
*R.maps.Rnw
<
10 changes: 6 additions & 4 deletions using-r.main.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ getwd()
\chapter*{Preface}

This series of Notes cover different aspects of the use of R. They are meant to be use as a complement to a course or book, as explanations
are short and terse. We do not discuss here statistics, just R as a tool and language for data manipulation and display.
are short and terse. We do not discuss here statistics, just R as a tool and language for data manipulation and display. The idea is a bit like how children learn a language: they work-out what the rules are simply by listening to people speak. I do give some explanations and comments, but the idea of this notes is mainly for you to use the numerous examples to find-out by yourself the overall patterns and coding philosophy behind the R language.

<<child-r-calc, child='R.as.calculator.Rnw', eval=FALSE>>=
This is work-in-progress. I will appreciate suggestions for further examples, notification of errors and unclear things and any bigger contributions. Many of the examples here have been collected from diverse sources over many years and because of this not all sources are acknowledged. If you recognize any example as yours or someone else's please let me know so that I can add a proper acknowledgement.

<<child-r-calc, child='R.as.calculator.Rnw', eval=TRUE>>=
@

<<child-r-scripts, child='R.scripts.Rnw', eval=FALSE>>=
Expand All @@ -122,10 +124,10 @@ are short and terse. We do not discuss here statistics, just R as a tool and lan
<<child-r-plotting, child='R.plotting.Rnw', eval=FALSE>>=
@

<<child-r-plotting, child='R.more.plotting.Rnw', eval=TRUE>>=
<<child-r-more-plotting, child='R.more.plotting.Rnw', eval=FALSE>>=
@

<<child-r-plotting, child='R.maps.Rnw', eval=FALSE>>=
<<child-r-maps, child='R.maps.Rnw', eval=FALSE>>=
@


Expand Down
Binary file modified using-r.main.pdf
Binary file not shown.
Loading

0 comments on commit 1d227d3

Please sign in to comment.