# cowplot是旧爱，patchwork乃新欢

Thomas Lin Pedersen简直是个天才，最近`patchwork`动作很大，看到我都准备转投它的怀抱，把旧爱`cowplot`给扔了。

``````library(ggplot2)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('图一')

p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('图二')

p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('图三')

p4 <- ggplot(mtcars) +
geom_bar(aes(gear)) +
facet_wrap(~cyl) +
ggtitle('图四')
``````

# ggplot2小技巧-数据没有，函数倒是有一个

``````set.seed(2019-10-23)
d <- data.frame(val=abs(rnorm(20)),
type=rep(c('A', 'B'), 10))
``````

20个数长这样子：

``````          val type
1  0.04625141    A
2  0.28000082    B
3  0.25317063    A
4  0.96411077    B
5  0.49222664    A
6  0.69874551    B
7  0.82134409    A
8  0.70966741    B
9  1.56752284    A
10 1.12881681    B
11 0.82488089    A
12 0.19897743    B
13 0.76739568    A
14 0.70597703    B
15 0.24332380    A
16 0.55423292    B
17 2.49008811    A
18 1.35153628    B
19 2.13711738    A
20 0.92299795    B
``````

# 一样的函数，不一样的upset

upsetplot大家见得多，首先来个富集分析的实例：

``````library(DOSE)
data(geneList)
de <- names(geneList)[abs(geneList) > 2]
edo <- enrichDGN(de)

library(enrichplot)
upsetplot(edo)
``````

# shit，拟合的残差这么大！

``````set.seed(123)
iris2 <- iris[sample(1:nrow(iris), 30), ]
model <- lm(Petal.Length ~ Sepal.Length, data = iris2)
iris2\$fitted <- predict(model)

iris3 <- iris2[abs(iris2\$Petal.Length-iris2\$fitted) > 0.5,]
p <- ggplot(iris2, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
geom_linerange(aes(ymin = fitted, ymax = Petal.Length),
data=iris3, colour = "purple") +
geom_abline(intercept = model\$coefficients[1],
slope = model\$coefficients[2])
p
``````

# 让我们画个黑板报吧

``````require(ggplot2)
require(ggimage)

d = data.frame(x=LETTERS[1:3], y = 1:3)
d\$y2 = rev(cumsum(rev(d\$y)))

p = ggplot(d, aes(x=1, y, fill=x)) + geom_col(color='white') +
geom_bgimage("img/blackboard.jpg") + theme_void() +
coord_polar("y") + theme(legend.position='none') +
geom_text(aes(y = y2 - y/2, label=x),family='xkcd', size=8) +
xlim(-1, NA) + scale_fill_viridis_d()
``````
• page 1 of 11

#### Guangchuang Yu

Bioinformatics Professor @ SMU

Bioinformatics Professor

Guangzhou