From 14266fe361e733d2e37e4b227e0d1db8b558e352 Mon Sep 17 00:00:00 2001 From: "Pedro J. Aphalo" Date: Sat, 21 Jan 2017 14:17:54 +0200 Subject: [PATCH] Minor changes to update_labels example to work around bug in ggplot2. Issue raised in Github. --- R.plotting.Rnw | 57 +- appendixes.prj | 8 +- sync.ffs_db | Bin 0 -> 126 bytes using-r-main.idx | 704 +- using-r-main.ilg | 28 +- using-r-main.ind | 408 +- using-r-main.synctex | 163566 ++++++++++++++++++++-------------------- using-r-main.toc | 220 +- 8 files changed, 82709 insertions(+), 82282 deletions(-) create mode 100644 sync.ffs_db diff --git a/R.plotting.Rnw b/R.plotting.Rnw index 63c8fad1..5385ef21 100644 --- a/R.plotting.Rnw +++ b/R.plotting.Rnw @@ -441,7 +441,7 @@ We start by checking which fonts families R recognizes on our system for the PDF names(pdfFonts()) @ -\code{"Helvetica"} is the default, but we can change the default through parameter \code{family}. Some of the family names are \emph{generic} like \code{serif}, \code{sans} (sans-serif) and \code{mono} (mono-spaced), and others refer to actual font names. Some related fonts (e.g.\ from different designers or foundries) may also use variations of the same name. +A sans-serif font, either \code{"Helvetica"} or \code{"Arial"} is the default, but we can change the default through parameter \code{family}. Some of the family names are \emph{generic} like \code{serif}, \code{sans} (sans-serif) and \code{mono} (mono-spaced), and others refer to actual font names. Some related fonts (e.g.\ from different designers or foundries) may also use variations of the same name. Base R does not support the use of system fonts in graphics output devices. However, add-on packages allow their use. The simplest to use is package \pkgname{showtext} described in \ref{sec:plot:fonts} on page \pageref{sec:plot:fonts}. <<>>= my.data <- @@ -518,7 +518,7 @@ I describe this in the same section, and immediately after the section on plotti The most flexible approach is to use \code{labs} as it allows the user to set the text or expressions to be used for these different elements. -<<>>= +<>= ggplot(data = Orange, aes(x = age, y = circumference, color = Tree)) + geom_line() + @@ -534,7 +534,7 @@ ggplot(data = Orange, There are in addition to \code{labs} convenience functions for setting the axis labels, \code{xlab} and \code{ylab}. -<<>>= +<>= ggplot(data = Orange, aes(x = age, y = circumference, color = Tree)) + geom_line() + @@ -546,7 +546,7 @@ ggplot(data = Orange, An additional convenience function, \code{ggtitle} can be used to add a title and optionally a subtitle. -<<>>= +<>= ggplot(data = Orange, aes(x = age, y = circumference, color = Tree)) + geom_line() + @@ -560,31 +560,42 @@ ggplot(data = Orange, Make an empty plot (\code{ggplot()}) and add to it as title an expression producing $y = b_0 + b_1 x + b_2 x^2$. (Hint: have a look at the examples for the use of expressions as labels in section \ref{sec:plot:text} on page \pageref{sec:plot:text} and the \code{plotmath} demo in R.) \end{playground} -Function \code{update\_labels} allows the replacement of labels in an existing plot. +Function \code{update\_labels} allows the replacement of labels in an existing plot. We first create a plot with one set of labels, and afterwards we replace them. -<<>>= +<>= p <- ggplot(data = mtcars, - aes(x=disp, y=hp, colour=factor(cyl), - shape=factor(cyl))) + + aes(x = disp, y = hp, colour = factor(cyl), + shape = factor(cyl))) + geom_point() + - labs(x="Engine displacement)", - y="Gross horsepower", - color="Number of\ncylinders", - shape="Number of\ncylinders") + labs(x = "Engine displacement)", + y = "Gross horsepower", + color = "Number of\ncylinders", + shape = "Number of\ncylinders") +p +@ +<>= update_labels(p, list(x = "Cilindrada", y = "Potencia bruta (caballos de fuerza)", - color = "no. de\ncilindros", + colour = "no. de\ncilindros", shape = "no. de\ncilindros")) @ +\begin{warningbox} +When setting or updating labels using either \code{labs()} or \code{update\_labels()} be aware that even though \code{color} and \code{colour} are synonyms for the same \emph{aesthetics}, the `name' used in the call to \code{aes()} must match the `name' used when setting or updating the labels. +\end{warningbox} + +\begin{playground} +Modify the code used in the code chunk above to update labels, so that \code{colour} is used instead of \code{color}. How does the figure change? +\end{playground} + The labels used in keys and axis tick-labels for factor levels can be changed through the different \emph{scales} as described in section \ref{sec:plot:scales} on page \pageref{sec:plot:scales}. \begin{explainbox} Sometimes we would like to include in the title or as an annotation in the plot, the name of the argument passed to \code{ggplot}'s \code{data} parameter. To obtain the name of an object as a character string, the usual R ``slang'' is \code{deparse(substitute(x))} where \code{x} is the object. -<<>>= +<>= ggplot(data = Orange, aes(x = age, y = circumference, color = Tree)) + geom_line() + @@ -595,7 +606,7 @@ ggplot(data = Orange, The example above rarely is of much use, as we have anyway to pass the object itself twice, and consequently there is no advantage in effort compare to typing \code{"Data: Orange"} as argument to \code{ggtitle}. A more general way to solve this problem is to write a wrapper function. -<<>>= +<>= ggwrapper <- function(data, ...) { ggplot(data, ...) + ggtitle(paste("Object: ", substitute(data))) @@ -614,7 +625,7 @@ Using this function in a loop over a list or vector, will produce output is not We create a suitable set of data frames, build a list with name \code{my.dfs} containing them. -<<>>= +<>= df1 <- data.frame(x = 1:10, y = (1:10)^2) df2 <- data.frame(x = 10:1, y = (10:1)^2.5) my.dfs <- list(first.df = df1, second.df = df2) @@ -622,7 +633,7 @@ my.dfs <- list(first.df = df1, second.df = df2) If we print the output produced by the wrapper function when called in a loop but we get always the same title, so this approach is not useful. -<<>>= +<>= for (df in my.dfs) { print( ggwrapper(data = df, @@ -638,7 +649,7 @@ for (df in my.dfs) { As we have given names to the list members, we can use these and enclose the loop in a function. This is a very inflexible approach, and on top the plots are only printed, and the \code{ggplot} objects get discarded once printed. -<<>>= +<>= plot.dfs <- function(x, ...) { list.name <- deparse(substitute(x)) member.names <- names(x) @@ -658,7 +669,7 @@ if (is.null(member.names)) { } @ -<<>>= +<>= plot.dfs(my.dfs) @ @@ -674,7 +685,7 @@ When one has control over the objects, one can add the desired title as an attri As an advanced exercise I suggest implementing this attribute-based solution by tagging the data frames using a function defined as shown below or by directly using \code{attr}. You will also need modify the code to use the new attribute when building the \code{ggplot} object. -<<>>= +<>= add.title.attr <- function(x, my.title) { attr(x, "title") <- my.title x @@ -1905,7 +1916,7 @@ Looking at the definition of \code{theme\_minimal} gives us enough information a theme_minimal @ -Using \code{theme\_minimal} as a model, we will proceed to define our own theme function. Argument \code{complete = TRUE} is essential as it affects the behaviour of the returned theme. A `complete' theme replaces any theme present in the ggplot object clearing all settings, while a theme that is not `complete' adds to the existing the new elements without clearing existing settings not being redefined. Saved themes like \code{theme\_grey()} are complete themes, while the themes objects returned by \code{theme()} are by default not complete. +Using \code{theme\_minimal} as a model, we will proceed to define our own theme function. Argument \code{complete = TRUE} is essential as it affects the behaviour of the returned theme. A `complete' theme replaces any theme present in the ggplot object clearing all settings, while a theme that is not `complete' adds to the existing the new elements without clearing existing settings not being redefined. Saved themes like \code{theme\_grey()} are complete themes, while the themes objects returned by \code{theme()} are by default not complete. <>= my_theme <- @@ -1928,7 +1939,7 @@ The function \code{theme\_minimal} was a good model for the example above, howev \end{explainbox} Frequently one needs the same plots differently formatted, e.g.\ for overhead slides and for use in a printed article or book. In such a case, we may even want some elements like titles to be included only in the plots in overhead slides. One could create two different \code{ggplot} objects, one for each occasion, but this can lead to inconsistencies if the code used to create the plot is updated. A better solution is to use themes, more generally, define themes for the different occasions according to one's taste and needs. A simple example is given in the next five code chunks. - + <>= theme_ovh <- function (base_size = 15, base_family = "") { @@ -1939,7 +1950,7 @@ theme_ovh <- theme_prn <- function (base_size = 11, base_family = "serif") { theme_classic(base_size = base_size, base_family = base_family) + - theme(plot.title = element_blank(), + theme(plot.title = element_blank(), plot.subtitle = element_blank(), complete = TRUE) } diff --git a/appendixes.prj b/appendixes.prj index 049878c2..a841f785 100644 --- a/appendixes.prj +++ b/appendixes.prj @@ -34,10 +34,10 @@ TeX:RNW 1060859 0 30 66 -1 1183 64 64 974 522 0 1 577 320 -1 -1 0 0 42 -1 -1 42 1 0 1183 -1 0 -1 0 R.intro.Rnw TeX:RNW -17838075 0 -1 102 -1 622 182 182 1542 705 0 1 41 224 -1 -1 0 0 22 -1 -1 22 1 0 622 -1 0 -1 0 +17838075 0 -1 102 -1 622 182 182 1542 705 0 1 41 244 -1 -1 0 0 22 -1 -1 22 1 0 622 -1 0 -1 0 rbooks.bib BibTeX:UNIX -1147890 0 758 7 758 7 52 52 872 313 0 1 89 306 -1 -1 0 0 -1 -1 -1 -1 1 0 7 758 0 -1 0 +1147890 0 758 7 758 7 52 52 872 313 0 1 89 304 -1 -1 0 0 21 0 0 21 1 0 7 758 0 -1 0 R.as.calculator.Rnw TeX:RNW 1060859 4 -1 33164 -1 33165 26 26 1386 549 0 1 233 64 -1 -1 0 0 31 -1 -1 31 1 0 33165 -1 0 -1 0 @@ -46,13 +46,13 @@ TeX:RNW 17838075 2 -1 190 -1 196 78 78 1438 601 1 1 137 96 -1 -1 0 0 31 -1 -1 31 1 0 196 -1 0 -1 0 R.functions.Rnw TeX:RNW -17838075 0 -1 6964 -1 6590 130 130 1490 653 0 1 257 112 -1 -1 0 0 30 -1 -1 30 1 0 6590 -1 0 -1 0 +17838075 0 -1 6964 -1 6590 130 130 1490 653 0 1 257 212 -1 -1 0 0 30 -1 -1 30 1 0 6590 -1 0 -1 0 using-r-main.toc TeX:AUX 1060850 0 135 1 75 1 64 64 1390 511 0 0 25 160 -1 -1 0 0 103 0 0 103 1 0 1 75 0 0 0 R.friends.Rnw TeX:RNW -1060859 0 -1 452 -1 970 104 104 853 490 0 1 609 304 -1 -1 0 0 31 -1 -1 31 1 0 970 -1 0 -1 0 +1060859 0 -1 452 -1 970 104 104 853 490 0 1 433 288 -1 -1 0 0 31 -1 -1 31 1 0 970 -1 0 -1 0 using-r-main.tex TeX 269496315 0 -1 24643 -1 24628 0 0 1009 511 0 1 257 160 -1 -1 0 0 73 -1 -1 73 1 0 24628 -1 0 -1 0 diff --git a/sync.ffs_db b/sync.ffs_db new file mode 100644 index 0000000000000000000000000000000000000000..ea8399f861629b22b0d9862cc6ce8a98851cdbd0 GIT binary patch literal 126 zcmZ=ON=