-
Notifications
You must be signed in to change notification settings - Fork 18
/
overlay.rmd
61 lines (46 loc) · 2.42 KB
/
overlay.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
---
title: Overlay plots
---
```{r include=FALSE}
library(visreg)
knitr::opts_knit$set(aliases=c(h = 'fig.height', w = 'fig.width'))
knitr::opts_chunk$set(comment='#', collapse=TRUE, cache=FALSE, tidy=FALSE)
knitr::knit_hooks$set(small.mar = function(before, options, envir) {
if (before) par(mar = c(4, 4, .1, .1))
})
```
The default [cross-sectional plot](cross.html) places the different cross-sections in separate panels. Occasionally, it is more helpful to overlay the plots on top of one another to see more directly how they compare. Using the same model as [before](cross.html):
```{r}
airquality$Heat <- cut(airquality$Temp, 3, labels=c("Cool", "Mild", "Hot"))
fit <- lm(Ozone ~ Solar.R + Wind * Heat, data=airquality)
```
We can specify `overlay=TRUE` to obtain a version of the plot in which all the images are overlaid:
```{r, h=5, w=5}
visreg(fit, "Wind", by="Heat", overlay=TRUE)
```
```{r, h=5, w=5}
visreg(fit, "Heat", by="Wind", overlay=TRUE)
```
The options described in [cross-sectional plots](cross.html) work in the same way here. For example,
```{r, h=5, w=5}
visreg(fit, "Heat", by="Wind", overlay=TRUE, breaks=c(6, 12))
```
In particular, the option `strip.names` is used in the same way for consistency, even though there are no actual strips in an overlay plot:
```{r, h=5, w=5}
visreg(fit, "Wind", by="Heat", overlay=TRUE, strip.names=c("Cold", "Not so hot", "Really hot"))
```
Changing the appearance of lines, etc., is accomplished in a similar manner to [other `visreg` plots](options.html), although (1) you need to pass a vector to specify the appearance for differeny `by` levels and (2) you need to be sure that you're correctly matching the colors of lines, points, and bands:
```{r, h=5, w=5}
visreg(fit, "Wind", by="Heat", overlay=TRUE,
line=list(col=c("#00C1C9", "#D63EFF", "#FF4E37"), lwd=1),
fill=list(col=c("#00C1C980", "#D63EFF80", "#FF4E3780")),
points=list(col=c("#00C1C9", "#D63EFF", "#FF4E37"), cex=1))
```
Note that you can still pass parameters as single elements (`cex=1`); these will apply to all levels of `by`.
For `gg` plots, the mechanism is different -- the returned plot can be modified with calls to `scale_xxx_manual()`:
```{r, h=5, w=5, message=FALSE, warning=FALSE}
library(ggplot2)
visreg(fit, "Wind", by="Heat", overlay=TRUE, gg=TRUE) +
scale_color_manual(values=c("#00C1C9", "#D63EFF", "#FF4E37")) +
scale_fill_manual(values=c("#00C1C980", "#D63EFF80", "#FF4E3780"))
```