@@ -65,21 +65,12 @@ print.theme <- function(x, ...) utils::str(x)
65
65
66
66
# ' Set theme elements
67
67
# '
68
- # '
69
- # ' Use this function to modify theme settings.
70
- # '
71
68
# ' Theme elements can inherit properties from other theme elements.
72
69
# ' For example, \code{axis.title.x} inherits from \code{axis.title},
73
70
# ' which in turn inherits from \code{text}. All text elements inherit
74
71
# ' directly or indirectly from \code{text}; all lines inherit from
75
72
# ' \code{line}, and all rectangular objects inherit from \code{rect}.
76
73
# '
77
- # ' For more examples of modifying properties using inheritance, see
78
- # ' \code{\link{+.gg}} and \code{\link{\%+replace\%}}.
79
- # '
80
- # ' To see a graphical representation of the inheritance tree, see the
81
- # ' last example below.
82
- # '
83
74
# ' @param line all line elements (\code{element_line})
84
75
# ' @param rect all rectangular elements (\code{element_rect})
85
76
# ' @param text all text elements (\code{element_text})
@@ -225,150 +216,102 @@ print.theme <- function(x, ...) utils::str(x)
225
216
# ' differently when added to a ggplot object. Also, when setting
226
217
# ' \code{complete = TRUE} all elements will be set to inherit from blank
227
218
# ' elements.
228
- # ' @param validate TRUE to run validate_element, FALSE to bypass checks.
229
- # '
230
- # ' @seealso \code{\link{+.gg}}
231
- # ' @seealso \code{\link{\%+replace\%}}
232
- # ' @seealso \code{\link{rel}}
233
- # ' @seealso \code{\link{element_blank}}
234
- # ' @seealso \code{\link{element_line}}
235
- # ' @seealso \code{\link{element_rect}}
236
- # ' @seealso \code{\link{element_text}}
219
+ # ' @param validate \code{TRUE} to run validate_element, \code{FALSE} to bypass checks.
220
+ # '
221
+ # ' @seealso
222
+ # ' \code{\link{+.gg}} and \code{\link{\%+replace\%}},
223
+ # ' \code{\link{rel}} for details of relative sizing,
224
+ # ' \code{\link{element_blank}}, \code{\link{element_line}},
225
+ # ' \code{\link{element_rect}}, and \code{\link{element_text}} for
226
+ # ' details of the specific theme elements.
237
227
# ' @export
238
228
# ' @examples
239
- # ' \donttest{
240
- # ' p <- ggplot(mtcars, aes(mpg, wt)) +
241
- # ' geom_point()
242
- # ' p
243
- # ' p + theme(panel.background = element_rect(colour = "pink"))
244
- # ' p + theme_bw()
229
+ # ' p1 <- ggplot(mtcars, aes(wt, mpg)) +
230
+ # ' geom_point() +
231
+ # ' labs(title = "Fuel economy declines as weight increases")
232
+ # ' p1
233
+ # '
234
+ # ' # Plot ---------------------------------------------------------------------
235
+ # ' p1 + theme(plot.title = element_text(size = rel(2)))
236
+ # ' p1 + theme(plot.background = element_rect(fill = "green"))
237
+ # '
238
+ # ' # Panels --------------------------------------------------------------------
239
+ # '
240
+ # ' p1 + theme(panel.background = element_rect(fill = "white", colour = "grey50"))
241
+ # ' p1 + theme(panel.border = element_rect(linetype = "dashed", fill = NA))
242
+ # ' p1 + theme(panel.grid.major = element_line(colour = "black"))
243
+ # ' p1 + theme(
244
+ # ' panel.grid.major.y = element_blank(),
245
+ # ' panel.grid.minor.y = element_blank()
246
+ # ' )
247
+ # '
248
+ # ' # Put gridlines on top of data
249
+ # ' p1 + theme(
250
+ # ' panel.background = element_rect(fill = NA),
251
+ # ' panel.grid.major = element_line(colour = "grey50"),
252
+ # ' panel.ontop = TRUE
253
+ # ' )
254
+ # '
255
+ # ' # Axes ----------------------------------------------------------------------
256
+ # ' p1 + theme(axis.line = element_line(size = 3, colour = "grey80"))
257
+ # ' p1 + theme(axis.text = element_text(colour = "blue"))
258
+ # ' p1 + theme(axis.ticks = element_line(size = 2))
259
+ # ' p1 + theme(axis.ticks.length = unit(.25, "cm"))
260
+ # ' p1 + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))
245
261
# '
246
- # ' # Scatter plot of gas mileage by vehicle weight
247
- # ' p <- ggplot(mtcars, aes(wt, mpg)) +
248
- # ' geom_point()
249
- # ' # Calculate slope and intercept of line of best fit
250
- # ' coef(lm(mpg ~ wt, data = mtcars))
251
- # ' p + geom_abline(intercept = 37, slope = -5)
252
- # ' # Calculate correlation coefficient
253
- # ' with(mtcars, cor(wt, mpg, use = "everything", method = "pearson"))
254
- # ' #annotate the plot
255
- # ' p + geom_abline(intercept = 37, slope = -5) +
256
- # ' geom_text(data = data.frame(), aes(4.5, 30, label = "Pearson-R = -.87"))
257
- # '
258
- # ' # Change the axis labels
259
- # ' # Original plot
260
- # ' p
261
- # ' p + labs(x = "Vehicle Weight", y = "Miles per Gallon")
262
- # ' # Or
263
- # ' p + labs(x = "Vehicle Weight", y = "Miles per Gallon")
264
- # '
265
- # ' # Change title appearance
266
- # ' p <- p + labs(title = "Vehicle Weight-Gas Mileage Relationship")
267
- # ' # Set title to twice the base font size
268
- # ' p + theme(plot.title = element_text(size = rel(2)))
269
- # ' p + theme(plot.title = element_text(size = rel(2), colour = "blue"))
270
- # '
271
- # ' # Add a subtitle and adjust bottom margin
272
- # ' p + labs(title = "Vehicle Weight-Gas Mileage Relationship",
273
- # ' subtitle = "You need to wrap long subtitleson manually") +
274
- # ' theme(plot.subtitle = element_text(margin = margin(b = 20)))
275
- # '
276
- # ' # Changing plot look with themes
277
- # ' DF <- data.frame(x = rnorm(400))
278
- # ' m <- ggplot(DF, aes(x = x)) +
279
- # ' geom_histogram()
280
- # ' # Default is theme_grey()
281
- # ' m
282
- # ' # Compare with
283
- # ' m + theme_bw()
284
- # '
285
- # ' # Manipulate Axis Attributes
286
- # ' m + theme(axis.line = element_line(size = 3, colour = "red", linetype = "dotted"))
287
- # ' m + theme(axis.text = element_text(colour = "blue"))
288
- # ' m + theme(axis.text.y = element_blank())
289
- # ' m + theme(axis.ticks = element_line(size = 2))
290
- # ' m + theme(axis.title.y = element_text(size = rel(1.5), angle = 90))
291
- # ' m + theme(axis.title.x = element_blank())
292
- # ' m + theme(axis.ticks.length = unit(.85, "cm"))
293
- # '
294
- # ' # Legend Attributes
295
- # ' z <- ggplot(mtcars, aes(wt, mpg)) +
296
- # ' geom_point(aes(colour = factor(cyl)))
297
- # ' z
298
- # ' z + theme(legend.position = "none")
299
- # ' z + theme(legend.position = "bottom")
300
- # ' # Or use relative coordinates between 0 and 1
301
- # ' z + theme(legend.position = c(.5, .5))
302
- # ' # Add a border to the whole legend
303
- # ' z + theme(legend.background = element_rect(colour = "black"))
304
- # ' # Legend margin controls extra space around outside of legend:
305
- # ' z + theme(legend.background = element_rect(),
306
- # ' legend.margin = margin(1, 1, 1, 1, "cm"))
307
- # ' z + theme(legend.background = element_rect(),
308
- # ' legend.margin = margin(0, 0, 0, 0, "cm"))
309
- # ' # Legend spacing pushes legends apart
310
- # ' z + theme(legend.background = element_rect(),
311
- # ' legend.margin = margin(1, 1, 1, 1, "cm"),
312
- # ' legend.spacing = unit(1, "cm"))
313
- # ' # A border and background can also be added around the whole legend area
314
- # ' z + theme(legend.box.background = element_rect(),
315
- # ' legend.box.margin = margin(5, 5, 5, 5, "mm"))
316
- # ' # The distance from the plot area can be set with legend.box.spacing
317
- # ' z + theme(legend.box.spacing = unit(3, "cm"))
262
+ # ' \donttest{
263
+ # ' # Legend --------------------------------------------------------------------
264
+ # ' p2 <- ggplot(mtcars, aes(wt, mpg)) +
265
+ # ' geom_point(aes(colour = factor(cyl), shape = factor(vs))) +
266
+ # ' labs(
267
+ # ' x = "Weight (1000 lbs)",
268
+ # ' y = "Fuel economy (mpg)",
269
+ # ' colour = "Cylinders",
270
+ # ' shape = "Transmission"
271
+ # ' )
272
+ # ' p2
273
+ # '
274
+ # ' # Position
275
+ # ' p2 + theme(legend.position = "none")
276
+ # ' p2 + theme(legend.justification = "top")
277
+ # '
278
+ # ' p2 + theme(legend.position = "bottom")
279
+ # ' p2 + theme(
280
+ # ' legend.position = "bottom",
281
+ # ' legend.box = "horizontal",
282
+ # ' legend.justification = "left"
283
+ # ' )
284
+ # ' # Or place inside the plot using relative coordinates between 0 and 1
285
+ # ' p2 + theme(
286
+ # ' legend.justification = c("right", "top"),
287
+ # ' legend.box.just = "right",
288
+ # ' legend.position = c(.95, .95),
289
+ # ' legend.margin = margin(6, 6, 6, 6)
290
+ # ' )
291
+ # '
292
+ # ' # The legend.box properties work similarly for the space around
293
+ # ' # all the legends
294
+ # ' p2 + theme(
295
+ # ' legend.box.background = element_rect(),
296
+ # ' legend.box.margin = margin(6, 6, 6, 6)
297
+ # ' )
298
+ # '
299
+ # ' # You can also control the display of the keys
318
300
# ' # and the justifaction related to the plot area can be set
319
- # ' z + theme(legend.justification = "bottom")
320
- # ' # Or to just the keys
321
- # ' z + theme(legend.key = element_rect(colour = "black"))
322
- # ' z + theme(legend.key = element_rect(fill = "yellow"))
323
- # ' z + theme(legend.key.size = unit(2.5, "cm"))
324
- # ' z + theme(legend.text = element_text(size = 20, colour = "red", angle = 45))
325
- # ' z + theme(legend.title = element_text(face = "italic"))
326
- # '
327
- # ' # To change the title of the legend use the name argument
328
- # ' # in one of the scale options
329
- # ' z + scale_colour_brewer(name = "My Legend")
330
- # ' z + scale_colour_grey(name = "Number of \nCylinders")
331
- # '
332
- # ' # Panel and Plot Attributes
333
- # ' z + theme(panel.background = element_rect(fill = "black"))
334
- # ' z + theme(panel.border = element_rect(linetype = "dashed", colour = "black"))
335
- # ' z + theme(panel.grid.major = element_line(colour = "blue"))
336
- # ' z + theme(panel.grid.minor = element_line(colour = "red", linetype = "dotted"))
337
- # ' z + theme(panel.grid.major = element_line(size = 2))
338
- # ' z + theme(panel.grid.major.y = element_blank(),
339
- # ' panel.grid.minor.y = element_blank())
340
- # ' z + theme(plot.background = element_rect())
341
- # ' z + theme(plot.background = element_rect(fill = "green"))
342
- # '
343
- # ' # Faceting Attributes
344
- # ' set.seed(4940)
345
- # ' dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
346
- # ' k <- ggplot(dsmall, aes(carat, ..density..)) +
347
- # ' geom_histogram(binwidth = 0.2) +
348
- # ' facet_grid(. ~ cut)
349
- # ' k + theme(strip.background = element_rect(colour = "purple", fill = "pink",
350
- # ' size = 3, linetype = "dashed"))
351
- # ' k + theme(strip.text.x = element_text(colour = "red", angle = 45, size = 10,
352
- # ' hjust = 0.5, vjust = 0.5))
353
- # ' k + theme(panel.spacing = unit(5, "lines"))
354
- # ' k + theme(panel.spacing.y = unit(0, "lines"))
355
- # '
356
- # ' # Put gridlines on top
357
- # ' meanprice <- tapply(diamonds$price, diamonds$cut, mean)
358
- # ' cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut))
359
- # ' df <- data.frame(meanprice, cut)
360
- # ' g <- ggplot(df, aes(cut, meanprice)) + geom_bar(stat = "identity")
361
- # ' g + geom_bar(stat = "identity") +
362
- # ' theme(panel.background = element_blank(),
363
- # ' panel.grid.major.x = element_blank(),
364
- # ' panel.grid.minor.x = element_blank(),
365
- # ' panel.grid.minor.y = element_blank(),
366
- # ' panel.ontop = TRUE)
367
- # '
368
- # ' # Modify a theme and save it
369
- # ' mytheme <- theme_grey() + theme(plot.title = element_text(colour = "red"))
370
- # ' p + mytheme
301
+ # ' p2 + theme(legend.key = element_rect(fill = "white", colour = "black"))
302
+ # ' p2 + theme(legend.text = element_text(size = 8, colour = "red"))
303
+ # ' p2 + theme(legend.title = element_text(face = "bold"))
304
+ # '
305
+ # ' # Strips --------------------------------------------------------------------
306
+ # '
307
+ # ' p3 <- ggplot(mtcars, aes(wt, mpg)) +
308
+ # ' geom_point() +
309
+ # ' facet_wrap(~ cyl)
310
+ # ' p3
371
311
# '
312
+ # ' p3 + theme(strip.background = element_rect(colour = "black", fill = "white"))
313
+ # ' p3 + theme(strip.text.x = element_text(colour = "white", face = "bold"))
314
+ # ' p3 + theme(panel.spacing = unit(1, "lines"))
372
315
# ' }
373
316
theme <- function (line ,
374
317
rect ,
0 commit comments