ggplot2包一方面可以实现多种形式的数据可视化、比如箱图、柱状图等;另一方面也可以从多个角度进行美化、修饰。对于前者,之前对ggplot2的柱状图、箱图用法进行了详细的学习。关于其它类型的图,例如密度图、折线图、直方图等,可参考他人的总结,例如下面的sthda网站。

R-ggplot2-箱图系列(1) basic - 简书 (jianshu.com)

R-ggplot2-柱状图系列 - 简书 (jianshu.com)

http://www.sthda.com/english/wiki/ggplot2-essentials

本篇笔记主要是针对我自己在绘制ggplot图片时,常常要修改的细节方面进行了大致的整理,以方便日后的查询。

1
2
library(tidyverse)
library(patchwork)

1、颜色设置

1
2
3
4
ggplot(mpg, aes(x = drv)) + 
  geom_bar(color="black", fill="grey")
#统一颜色
#一般color为边框、点的颜色,fill为填充色

1.1 离散型变量映射

1
2
p = ggplot(mpg, aes(x = drv, fill = drv)) + 
      geom_bar()
  • (1)scale_fill_manual()
1
2
3
4
5
6
p1 = p + scale_fill_manual(values = c("red","blue","green"))
# https://colorbrewer2.org/
p2 = p + scale_fill_manual("this is title",
                      values = c("4"="green", "r"="red", "f"="blue"))
p3 = p + scale_fill_manual(values = c("red","blue","green"),guide="none")
p1 + p2 + p3

image-20220703135751325

  • (2)scale_fill_brewer()
1
2
3
4
# 关于ColorBrewer的色板:https://www.jianshu.com/p/e0179b0a71d6
p1 = p + scale_fill_brewer("Set1", palette = "Set1")
p2 = p + scale_fill_brewer("Dark2", palette = "Dark2")
p1 + p2

image-20220703140134133

1
2
3
4
5
6
## (3) scale_fill_grey()
# 0~1 --> black~white
p + scale_fill_grey(start = 0.1, end = 0.8)

## (4) ggsci包
p + ggsci::scale_fill_aaas()

1.2 连续型变量映射

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
p = ggplot(mpg, aes(x = displ, y = hwy, color = cty)) + 
      geom_point()

p1 = p + scale_color_gradient(low = "green", high = "red")
p2 = p + scale_color_gradient2(low = "green", mid = "grey", high = "red", 
                               midpoint = mean(mpg$cty))
p3 = p + scale_color_gradient("this is title",
                              low = "green", high = "red",
                              breaks = c(10,20,30),
                              label = c("Low","Middle","High")) 
p1 + p2 + p3

image-20220703140643256

1
2
# ggsci包
p + ggsci::scale_color_gsea()

2、文本格式设置

Tips to Customize Text in ggplot2 plot

常用参数有如下

size:大小

colour:颜色

angle:旋转(rotate)角度

hjust/vjust:位置

face:设置粗体

family:设置字体

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
p = ggplot(mpg, aes(x = displ, y = hwy, color = cty)) + 
  geom_point()
#设置标题
p = p + labs(title = "Big Title",
         subtitle = "sub title",
         caption = "caption title",
         tag = "A") +
  xlab("X title") + 
  ylab("Y title")

p1 = p + theme(text = element_text(size = 15, color = "red"))
p + p1
image-20220703142435572
1
2
3
4
5
6
7
8
#标题居中
p1 = p + theme(plot.title = element_text(hjust = 0.5))
#旋转x轴标签文本
p2 = p + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
# p + theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))
p3 = p + theme(axis.text.x = element_text(angle = 45, hjust=1))
p4 = p + theme(axis.text.x = element_text(angle = -30, hjust = 0))
p1 / (p2 + p3 + p4)
image-20220703143413305

3、坐标轴设置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
p = ggplot(mpg, aes(displ, hwy)) + 
      geom_point()
p1 = p + scale_x_continuous(limits = c(3, 6))  #切割
# Warning message:
# Removed 105 rows containing missing values (geom_point).

p2 = p + coord_cartesian(xlim = c(3, 6))       #放大
p3 = p + scale_x_continuous(breaks = c(3, 5, 7),
                       labels = c("003", "005", "007"))
p1 + p2 + p3

image-20220703150557378

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
p = ggplot(mpg, aes(x = drv, fill = drv)) + 
  geom_bar()
p1 = p + coord_cartesian(ylim = c(0, 150)) +
  scale_y_continuous(expand = expansion(0))  #贴边

p2 = p + scale_x_discrete(breaks = c("4","f","r"),
                          labels = c("444","fff","rrr"),
                          limits = c("r","f","4"))  #自定义标签与顺序
p3 = p + coord_flip()    #转置

p1 + p2 + p3

image-20220703150934408

4、添加注释

4.1 文本注释

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
p = ggplot(mpg, aes(displ, hwy)) + 
        geom_point()
p1 = p + annotate("text", x = 6, y = 40, label = "annotate() Annotation")
p2 = p + geom_text(x = 6, y = 40, label = "geom_text() Annotation")
p3 = p + annotate("segment", x = 4, xend = 2.5, y = 35, yend = 40,
             colour = "red", size = 0.5, 
             arrow = arrow(angle = 30, length = unit(0.1, "inches")))
p4 = p + annotate("rect", xmin = 6, xmax = 7, ymin = 0, ymax = 50,
             alpha = .1,fill = "red")

(p1 + p2) / (p3 + p4) 
image-20220703155754046

4.2 直线注释

1
2
3
4
5
6
p1 = p + geom_hline(yintercept = 30)
p2 = p + geom_vline(xintercept = 4, 
               linetype="dashed", color="red", size=2)
p3 = p + geom_abline(intercept = 20, slope = 1)

p1 + p2 + p3
image-20220703155843063

5、分面多图

1
2
3
4
5
6
7
p = ggplot(mpg, aes(displ, hwy)) + 
        geom_point()
p1 = p + facet_wrap( ~ class, nrow = 3)
p2 = p + facet_grid(drv ~ ., scales = "free_y")
p3 = p + facet_grid(drv ~ cyl, scales = "free")

p1 + p2 + p3

image-20220703162311073

6、图例设置

1
2
3
4
5
6
7
8
9
p = ggplot(mpg, aes(x = drv, fill = drv)) + 
  geom_bar()
p1 = p + theme(legend.position = "none")
#  "left", "top", "right", "bottom", "none"
p2 = p + theme(legend.position = "bottom")
p3 = p + theme(legend.position = c(0.9,0.8))
p4 = p + guides(fill = guide_legend(title = "", ncol = 2))

(p1 + p2)/(p3 + p4)
image-20220703164151686