-
Notifications
You must be signed in to change notification settings - Fork 3
/
ggfun.Rmd
118 lines (84 loc) · 2.46 KB
/
ggfun.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
title: "Useful functions for ggplot2"
author: "Guangchuang Yu\\
School of Basic Medical Sciences, Southern Medical University"
date: "`r Sys.Date()`"
output:
prettydoc::html_pretty:
toc: true
theme: cayman
highlight: github
pdf_document:
toc: true
vignette: >
%\VignetteIndexEntry{ggplot utilities}
%\VignetteEngine{knitr::rmarkdown}
%\usepackage[utf8]{inputenc}
---
```{r style, echo=FALSE, results="asis", message=FALSE}
knitr::opts_chunk$set(tidy = FALSE,
message = FALSE)
library("grid")
library("ggplot2")
library("ggfun")
theme_set(theme_grey())
```
```{r}
library("grid")
library("ggplot2")
library("ggfun")
```
## element_roundrect
<!--
p <- ggplot(mtcars, aes(mpg, disp, color=factor(cyl), size=cyl)) + geom_point()
keybox(p, 'rect')
keybox(p, 'roundrect', gp = gpar(col = '#808080', lty = "dashed"))
-->
The `element_roundrect` works like `element_rect` to draw round rectangle background. We can use it to adjust theme elements, including legend, strip, panel and plot background.
```{r}
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p <- p + facet_grid(cols = vars(cyl))
p <- p + theme(strip.background=element_roundrect(fill="grey40", color=NA, r=0.15))
p
```
```{r}
p2 <- ggplot(mtcars, aes(mpg, disp, color=factor(cyl), size=cyl)) +
geom_point()
p2 + theme(legend.background=element_roundrect(color="#808080", linetype=2))
```
## gglegend
```{r}
p <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
data <- data.frame(colour = c("red", "blue"), VALUE = c("A", "B"))
gglegend(aes(colour = VALUE, label=VALUE), data, geom_text, p)
```
## set_font
```{r}
d <- data.frame(x=rnorm(10), y=rnorm(10), lab=LETTERS[1:10])
p <- ggplot(d, aes(x, y)) + geom_text(aes(label=lab), size=5)
set_font(p, family="Times", fontface="italic", color='firebrick')
```
## facet_set
Manually specifying facet labels.
```{r fig.width=6, fig.height=3}
library(ggplot2)
library(ggfun)
p <- ggplot(mtcars, aes(disp, drat)) +
geom_point() +
facet_grid(cols=vars(am), rows=vars(cyl))
p + facet_set(label=c(`0`="Zero", `6`="Six"))
```
Supports labeller:
```{r}
p + facet_set(label=label_both)
```
Add a facet label to a plot.
```{r}
p + facet_set(label="TEST")
```
With the [ggplotify](https://cran.r-project.org/package=ggplotify) package, we can use `facet_set` to add a facet label to almost any plot in R.
```{r eval=FALSE}
## please try:
ggplotify::as.ggplot(~barplot(1:10, col=rainbow(10))) +
facet_set('a barplot in base')
```