|
45 | 45 | #' will be shown, regardless of whether or not they appear in the data.
|
46 | 46 | #' @export
|
47 | 47 | #' @examples
|
48 |
| -#' \donttest{ |
49 |
| -#' p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() |
50 |
| -#' # With one variable |
51 |
| -#' p + facet_grid(. ~ cyl) |
52 |
| -#' p + facet_grid(cyl ~ .) |
| 48 | +#' p <- ggplot(mpg, aes(displ, cty)) + geom_point() |
53 | 49 | #'
|
54 |
| -#' # With two variables |
55 |
| -#' p + facet_grid(vs ~ am) |
56 |
| -#' p + facet_grid(am ~ vs) |
57 |
| -#' p + facet_grid(vs ~ am, margins=TRUE) |
| 50 | +#' p + facet_grid(. ~ cyl) |
| 51 | +#' p + facet_grid(drv ~ .) |
| 52 | +#' p + facet_grid(drv ~ cyl) |
58 | 53 | #'
|
59 | 54 | #' # To change plot order of facet grid,
|
60 | 55 | #' # change the order of variable levels with factor()
|
61 | 56 | #'
|
62 |
| -#' set.seed(6809) |
63 |
| -#' diamonds <- diamonds[sample(nrow(diamonds), 1000), ] |
64 |
| -#' diamonds$cut <- factor(diamonds$cut, |
65 |
| -#' levels = c("Ideal", "Very Good", "Fair", "Good", "Premium")) |
66 |
| -#' |
67 |
| -#' # Repeat first example with new order |
68 |
| -#' p <- ggplot(diamonds, aes(carat, ..density..)) + |
69 |
| -#' geom_histogram(binwidth = 1) |
70 |
| -#' p + facet_grid(. ~ cut) |
71 |
| -#' |
72 |
| -#' g <- ggplot(mtcars, aes(mpg, wt)) + |
73 |
| -#' geom_point() |
74 |
| -#' g + facet_grid(. ~ vs + am) |
75 |
| -#' g + facet_grid(vs + am ~ .) |
76 |
| -#' |
77 |
| -#' # You can also use strings, which makes it a little easier |
78 |
| -#' # when writing functions that generate faceting specifications |
79 |
| -#' |
80 |
| -#' p + facet_grid("cut ~ .") |
81 |
| -#' |
82 |
| -#' # see also ?plotmatrix for the scatterplot matrix |
83 |
| -#' |
84 |
| -#' # If there isn't any data for a given combination, that panel |
85 |
| -#' # will be empty |
86 |
| -#' |
87 |
| -#' g + facet_grid(cyl ~ vs) |
88 |
| -#' |
89 | 57 | #' # If you combine a facetted dataset with a dataset that lacks those
|
90 | 58 | #' # facetting variables, the data will be repeated across the missing
|
91 | 59 | #' # combinations:
|
92 |
| -#' |
93 |
| -#' g + facet_grid(vs ~ cyl) |
94 |
| -#' |
95 |
| -#' df <- data.frame(mpg = 22, wt = 3) |
96 |
| -#' g + facet_grid(vs ~ cyl) + |
| 60 | +#' df <- data.frame(displ = mean(mpg$displ), cty = mean(mpg$cty)) |
| 61 | +#' p + |
| 62 | +#' facet_grid(. ~ cyl) + |
97 | 63 | #' geom_point(data = df, colour = "red", size = 2)
|
98 | 64 | #'
|
99 |
| -#' df2 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(0, 1)) |
100 |
| -#' g + facet_grid(vs ~ cyl) + |
101 |
| -#' geom_point(data = df2, colour = "red", size = 2) |
102 |
| -#' |
103 |
| -#' df3 <- data.frame(mpg = c(19, 22), wt = c(2,4), vs = c(1, 1)) |
104 |
| -#' g + facet_grid(vs ~ cyl) + |
105 |
| -#' geom_point(data = df3, colour = "red", size = 2) |
106 |
| -#' |
107 |
| -#' |
| 65 | +#' # Free scales ------------------------------------------------------- |
108 | 66 | #' # You can also choose whether the scales should be constant
|
109 | 67 | #' # across all panels (the default), or whether they should be allowed
|
110 | 68 | #' # to vary
|
111 | 69 | #' mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
|
112 | 70 | #' geom_point()
|
113 | 71 | #'
|
114 | 72 | #' mt + facet_grid(. ~ cyl, scales = "free")
|
| 73 | +#' |
115 | 74 | #' # If scales and space are free, then the mapping between position
|
116 |
| -#' # and values in the data will be the same across all panels |
117 |
| -#' mt + facet_grid(. ~ cyl, scales = "free", space = "free") |
| 75 | +#' # and values in the data will be the same across all panels. This |
| 76 | +#' # is particularly useful for categorical axes |
| 77 | +#' ggplot(mpg, aes(drv, model)) + |
| 78 | +#' geom_point() + |
| 79 | +#' facet_grid(manufacturer ~ ., scales = "free", space = "free") + |
| 80 | +#' theme(strip.text.y = element_text(angle = 0)) |
118 | 81 | #'
|
119 |
| -#' mt + facet_grid(vs ~ am, scales = "free") |
120 |
| -#' mt + facet_grid(vs ~ am, scales = "free_x") |
121 |
| -#' mt + facet_grid(vs ~ am, scales = "free_y") |
122 |
| -#' mt + facet_grid(vs ~ am, scales = "free", space = "free") |
123 |
| -#' mt + facet_grid(vs ~ am, scales = "free", space = "free_x") |
124 |
| -#' mt + facet_grid(vs ~ am, scales = "free", space = "free_y") |
| 82 | +#' # Facet labels ------------------------------------------------------ |
| 83 | +#' p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() |
| 84 | +#' p |
125 | 85 | #'
|
126 |
| -#' # You may need to set your own breaks for consistent display: |
127 |
| -#' mt + facet_grid(. ~ cyl, scales = "free_x", space = "free") + |
128 |
| -#' scale_x_continuous(breaks = seq(10, 36, by = 2)) |
129 |
| -#' # Adding scale limits override free scales: |
130 |
| -#' last_plot() + xlim(10, 15) |
| 86 | +#' # label_both() displays both variable name and value |
| 87 | +#' p + facet_grid(vs ~ cyl, labeller = label_both) |
131 | 88 | #'
|
132 |
| -#' # Free scales are particularly useful for categorical variables |
133 |
| -#' ggplot(mpg, aes(cty, model)) + |
| 89 | +#' # label_parsed() parses text into mathematical expressions, see ?plotmath |
| 90 | +#' mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "sqrt(x, y)")) |
| 91 | +#' ggplot(mtcars, aes(wt, mpg)) + |
134 | 92 | #' geom_point() +
|
135 |
| -#' facet_grid(manufacturer ~ ., scales = "free", space = "free") |
136 |
| -#' # particularly when you reorder factor levels |
137 |
| -#' mpg$model <- reorder(mpg$model, mpg$cty) |
138 |
| -#' manufacturer <- reorder(mpg$manufacturer, mpg$cty) |
139 |
| -#' last_plot() %+% mpg + theme(strip.text.y = element_text()) |
| 93 | +#' facet_grid(. ~ cyl2, labeller = label_parsed) |
140 | 94 | #'
|
141 |
| -#' # Use as.table to to control direction of horizontal facets, TRUE by default |
142 |
| -#' h <- ggplot(mtcars, aes(x = mpg, y = wt)) + |
143 |
| -#' geom_point() |
144 |
| -#' h + facet_grid(cyl ~ vs) |
145 |
| -#' h + facet_grid(cyl ~ vs, as.table = FALSE) |
| 95 | +#' # label_bquote() makes it easy to construct math expressions |
| 96 | +#' p + facet_grid(. ~ vs, labeller = label_bquote(cols = alpha ^ .(vs))) |
146 | 97 | #'
|
147 |
| -#' # Use labeller to control facet labels, label_value is default |
148 |
| -#' h + facet_grid(cyl ~ vs, labeller = label_both) |
149 |
| -#' # Using label_parsed, see ?plotmath for more options |
150 |
| -#' mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "sqrt(x, y)")) |
151 |
| -#' k <- ggplot(mtcars, aes(wt, mpg)) + |
152 |
| -#' geom_point() |
153 |
| -#' k + facet_grid(. ~ cyl2) |
154 |
| -#' k + facet_grid(. ~ cyl2, labeller = label_parsed) |
155 |
| -#' # For label_bquote the label value is x. |
156 |
| -#' p <- ggplot(mtcars, aes(wt, mpg)) + |
157 |
| -#' geom_point() |
158 |
| -#' p + facet_grid(. ~ vs, labeller = label_bquote(alpha ^ .(x))) |
159 |
| -#' p + facet_grid(. ~ vs, labeller = label_bquote(.(x) ^ .(x))) |
| 98 | +#' # The facet strips can be displayed near the axes with switch |
| 99 | +#' data <- transform(mtcars, |
| 100 | +#' am = factor(am, levels = 0:1, c("Automatic", "Manual")), |
| 101 | +#' gear = factor(gear, levels = 3:5, labels = c("Three", "Four", "Five")) |
| 102 | +#' ) |
| 103 | +#' p <- ggplot(data, aes(mpg, disp)) + geom_point() |
| 104 | +#' p + facet_grid(am ~ gear, switch = "both") |
| 105 | +#' # It looks better without boxes around the strips |
| 106 | +#' p + facet_grid(am ~ gear, switch = "both") + |
| 107 | +#' theme(strip.background = element_blank()) |
160 | 108 | #'
|
| 109 | +#' # Margins ---------------------------------------------------------- |
| 110 | +#' \dontrun{ |
161 | 111 | #' # Margins can be specified by logically (all yes or all no) or by specific
|
162 | 112 | #' # variables as (character) variable names
|
163 | 113 | #' mg <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
|
|
170 | 120 | #' mg + facet_grid(vs + am ~ gear, margins = "vs")
|
171 | 121 | #' mg + facet_grid(vs + am ~ gear, margins = "gear")
|
172 | 122 | #' mg + facet_grid(vs + am ~ gear, margins = c("gear", "am"))
|
173 |
| -#' |
174 |
| -#' # The facet strips can be displayed near the axes with switch |
175 |
| -#' data <- transform(mtcars, |
176 |
| -#' am = factor(am, levels = 0:1, c("Automatic", "Manual")), |
177 |
| -#' gear = factor(gear, levels = 3:5, labels = c("Three", "Four", "Five")) |
178 |
| -#' ) |
179 |
| -#' p <- ggplot(data, aes(mpg, disp)) + geom_point() |
180 |
| -#' p + facet_grid(am ~ gear, switch = "both") + theme_light() |
181 |
| -#' |
182 |
| -#' # It may be more aesthetic to use a theme without boxes around |
183 |
| -#' # around the strips. |
184 |
| -#' p + facet_grid(am ~ gear + vs, switch = "y") + theme_minimal() |
185 |
| -#' p + facet_grid(am ~ ., switch = "y") + |
186 |
| -#' theme_gray() %+replace% theme(strip.background = element_blank()) |
187 | 123 | #' }
|
188 | 124 | #' @importFrom plyr as.quoted
|
189 | 125 | facet_grid <- function(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, switch = NULL, drop = TRUE) {
|
|
0 commit comments