图形是数据的一种表现方式,任何精美的图都要有精美的数据做支撑。

常用的可视化R包

base满足基本的绘图要求ggplot2和ggpubr可以做出精美的图片
image.png

ggplot2

绘图代码模板

  1. ggplot (data = <DATA>)+
  2. <GEOM_FUNCTION>(mapping = ase (<MAPPINGS>))

属性设置

常见属性参数

  1. color ## 边框颜色,设置为字符串,如blue、十六位进制颜色代码等
  2. size ## 形状大小,单位是mm
  3. shape ## 形状,用具体的数字编号表示
  4. alpha ## 透明度
  5. fill ## 填充颜色

如:

  1. ggplot(data = iris)+
  2. geom_point(mapping = aes(x = Sepal.Length,
  3. y = Petal.Length,
  4. color = Species))

image.png

  1. ## 修改颜色,需要在全局改
  2. ggplot(data = iris)+
  3. geom_point(mapping = aes(x = Sepal.Length,
  4. y = Petal.Length),
  5. color = "blue")

image.png

  1. ## 修改点的格式,需要在全局改
  2. ggplot(data = iris)+
  3. geom_point(mapping = aes(x = Sepal.Length,
  4. y = Petal.Length),
  5. color = "blue",
  6. size = 10,
  7. alpha = 0.5,
  8. shape = "#")

image.png

映射

按照数据框的某一列定义图的某个属性,是全局和个别的关系。
image.png
十六进制颜色代码表:
https://blog.csdn.net/TheLittlePython/article/details/79063779

区分color和fill两个属性

color代表边框颜色
fill代表填充颜色
image.png

分面

用ggplot2包里自带的facet_wrap()函数

分面

如:

  1. ggplot(data = iris)+
  2. geom_point(mapping = aes(x = Sepal.Length,
  3. y = Petal.Length),
  4. color = "blue",
  5. size = 10,
  6. alpha = 0.5,
  7. shape = "#")

image.png

  1. ## 分面
  2. ggplot(data = iris)+
  3. geom_point(mapping = aes(x = Sepal.Length,
  4. y = Petal.Length),
  5. color = "blue",
  6. size = 10,
  7. alpha = 0.5,
  8. shape = "#")+
  9. facet_wrap(~Species)

image.png

双分面

  1. ## 双分面
  2. ggplot(data = iris)+
  3. geom_point(mapping = aes(x = Sepal.Length,
  4. y = Petal.Length),
  5. color = "blue",
  6. size = 10,
  7. alpha = 0.5,
  8. shape = "#")+
  9. facet_wrap(Petal.Length ~ Species)

image.png

几何对象

具体指做的图层,可以叠加,需要注意图层上下顺序,以便点在最上层

图层叠加

如:

  1. ## 图层1
  2. ggplot(data = iris)+
  3. geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))

image.png

  1. ## 图层2
  2. ggplot(data = iris)+
  3. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))

image.png

  1. ## 叠加
  2. ggplot(data = iris)+
  3. geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))+
  4. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))

image.png

注意局部与全局的差别

函数内的是局部,改变仅限于函数内;外面则是全局。
image.png

位置和坐标系

位置

主要理解geom_point()和geom_jitter()的区别。
image.png

坐标系

翻转坐标系

  1. ggplot(data = iris)+
  2. geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))+
  3. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))

image.png

  1. ## 反转坐标系
  2. ggplot(data = iris)+
  3. geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))+
  4. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))+
  5. coord_flip()

image.png

极坐标系

  1. ggplot(data = iris)+
  2. geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))+
  3. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))

image.png

  1. ## 极坐标系
  2. ggplot(data = iris)+
  3. geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))+
  4. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))+
  5. coord_polar()

image.png

完整绘图模板

  1. ggplot(data = <DATA>)+
  2. <GEOM_FUNCTION>(
  3. mapping = aes(<MAPPINGS>),
  4. sata = <STAT>,
  5. position = <POSITION>
  6. ) +
  7. <COORDINATE_FUNCTION> +
  8. <FACET_FUNCTION>

ggbubr

代码相对简单,作图比较容易。

图层叠加

如:

  1. ## 做点图
  2. ggscatter(iris,
  3. x="Sepal.Length",
  4. y="Petal.Length",
  5. color="Species")

image.png

  1. ## 箱线图上增加点图
  2. ggboxplot(iris,
  3. x = "Species",
  4. y = "Sepal.Length",
  5. color = "Species",
  6. shape = "Species",
  7. add = "jitter")

image.png

添加p值

分为三步:箱线图命名+设置比较组别+stat_compare_means()函数计算

  1. p = ggboxplot(iris,
  2. x = "Species",
  3. y = "Sepal.Length",
  4. color = "Species",
  5. shape = "Species",
  6. add = "jitter")
  7. my_comparisons = list(c("setosa","versicolor"),
  8. c("setosa","virginica"),
  9. c("versicolor","virginica"))
  10. p + stat_compare_means(comparisons = my_comparisons)+
  11. stat_compare_means(label.y = 9)

image.png

图片的导出和保存

ggplot2系列

  1. ## 保存画板图片
  2. ggsave("iris_box_ggpubr.png")
  3. ## 命名图片保存
  4. ggsave(p, filename = "iris_box_ggpubr2.png")

pdf系列

三段论格式

  1. pdf(test.pdf) ##新建pdf
  2. ... ##绘图代码
  3. dev.off ##关闭画板

eoffice

  1. topptx(p, "iris_box_ggpubr.pptx")

拼图

patchwork

详见
https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ

参考材料:

https://www.yuque.com/xiaojiewanglezenmofenshen/dbwkg1