6、R语言作图 - 图1

1、基础包—绘图函数

image.png
*高级:直接出图片;低级:需要以图片作为基础

  1. #1.基础包 ,了解
  2. plot(iris[,1],iris[,3],col = iris[,5]) #横坐标,纵坐标,颜色
  3. text(6.5,4, labels = 'hello')# 某坐标添加文字
  4. dev.off() #关闭画板

2、ggplot2与ggpubr

2.1作图数据、横纵坐标

  1. ggplot(data = <DATA>)+
  2. <geom_point>(mapping = aes(<MAPPINGS>))
  3. <>:表示代替内容
  4. 特殊语法:
  5. 列名不带引号,行末写加号
  6. 用“+”连接不同的函数

image.png

2.2属性设置

映射

image.png

image.png
映射和手动设置的区别:
映射是根据数据的某一列的内容分配颜色。 color = Species
手动设置:把图形设置为一个或几个颜色,与数据内容无关。 color = “blue”
image.png

颜色编码
image.png

填充

image.png
image.png

2.3分面

三分面和双分面
image.png
image.png

认识函数:sample(letters[1:5],150,replace = T)

上述涉及的代码

  1. library(ggplot2)
  2. #1.入门级绘图模板:作图数据,横纵坐标
  3. ggplot(data = iris)+
  4. geom_point(mapping = aes(x = Sepal.Length,
  5. y = Petal.Length))
  6. #2.属性设置(颜色、大小、透明度、点的形状,线型等)
  7. #2.1 手动设置,需要设置为有意义的值
  8. ggplot(data = iris) +
  9. geom_point(mapping = aes(x = Sepal.Length,
  10. y = Petal.Length),
  11. color = "blue")
  12. ggplot(data = iris) +
  13. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length),
  14. size = 5, # 点的大小5mm
  15. alpha = 0.5, # 透明度 50%
  16. shape = 8) # 点的形状
  17. #2.2 映射:按照数据框的某一列来定义图的某个属性
  18. ggplot(data = iris)+
  19. geom_point(mapping = aes(x = Sepal.Length,
  20. y = Petal.Length,
  21. color = Species))
  22. #映射VS 手动设置
  23. ggplot(data = iris)+
  24. geom_point(mapping = aes(x = Sepal.Length,
  25. y = Petal.Length),
  26. color = "blue")
  27. ## Q1 自行指定映射的具体颜色
  28. ggplot(data = iris)+
  29. geom_point(mapping = aes(x = Sepal.Length,
  30. y = Petal.Length,
  31. color = Species))+
  32. scale_color_manual(values = c("blue","grey","red"))
  33. ## Q2 区分color和fill两个属性
  34. ### Q2-1 空心形状和实心形状都用color设置颜色
  35. ggplot(data = iris)+
  36. geom_point(mapping = aes(x = Sepal.Length,
  37. y = Petal.Length,
  38. color = Species),
  39. shape = 17) #17号,实心的例子
  40. ggplot(data = iris)+
  41. geom_point(mapping = aes(x = Sepal.Length,
  42. y = Petal.Length,
  43. color = Species),
  44. shape = 2) #2号,空心的例子
  45. ### Q2-2 既有边框又有内心的,才需要color和fill两个参数
  46. ggplot(data = iris)+
  47. geom_point(mapping = aes(x = Sepal.Length,
  48. y = Petal.Length,
  49. color = Species),
  50. shape = 24,
  51. fill = "black") #24号,双色的例子
  52. #3.分面
  53. ggplot(data = iris) +
  54. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
  55. facet_wrap(~ Species) #3张子图
  56. #双分面
  57. dat = iris #注意避免修改内置数据
  58. dat$Group = sample(letters[1:5],150,replace = T)
  59. ggplot(data = dat) +
  60. geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
  61. facet_grid(Group ~ Species) #横纵均分面,有重复值,有限
  62. #认识sample函数
  63. sample(letters[1:5],3)#带有随机性
  64. sample(letters[1:5],6)#有范围的取样,6大于5,故会报错
  65. sample(letters[1:5],6,replace = T)#添加上replace = T,则可循环

2.4几何对象

geom_xxx()画出单个几何对象,几何对象可叠加

  1. #4.几何对象
  2. #局部设置(仅对当前图层有效)和全局设置(对所有图层有效)
  3. #geom_xxx()画出单个几何对象,几何对象可叠加
  4. ggplot(data = iris) +
  5. geom_smooth(mapping = aes(x = Sepal.Length,
  6. y = Petal.Length))+
  7. geom_point(mapping = aes(x = Sepal.Length,
  8. y = Petal.Length))
  9. #简化上述代码,如下
  10. ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  11. geom_smooth()+
  12. geom_point()

2.5统计变换

image.png

image.png

image.png

2.6位置关系

image.png

2.7坐标系

image.png
涉及代码

  1. #5.统计变换-直方图
  2. View(diamonds)
  3. table(diamonds$cut)
  4. #画条形图,了解。自动生成y
  5. ggplot(data = diamonds) +
  6. geom_bar(mapping = aes(x = cut))
  7. #展示数据,了解
  8. ggplot(data = diamonds) +
  9. stat_count(mapping = aes(x = cut))
  10. #统计变换使用场景
  11. #5.1.不统计,数据直接做图
  12. fre = as.data.frame(table(diamonds$cut))
  13. fre
  14. ggplot(data = fre) +
  15. geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
  16. #5.2count改为prop
  17. ggplot(data = diamonds) +
  18. geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
  19. #6.位置关系
  20. # 6.1抖动的点图 ,geom_jitter
  21. ggplot(data = iris,mapping = aes(x = Species,
  22. y = Sepal.Width,
  23. fill = Species)) +
  24. geom_boxplot()+
  25. geom_point()
  26. ggplot(data = iris,mapping = aes(x = Species,
  27. y = Sepal.Width,
  28. fill = Species)) +
  29. geom_boxplot()+
  30. geom_jitter()
  31. # 6.2堆叠直方图
  32. ggplot(data = diamonds) +
  33. geom_bar(mapping = aes(x = cut,fill=clarity))
  34. # 6.3 并列直方图
  35. ggplot(data = diamonds) +
  36. geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
  37. #7.坐标系
  38. #翻转coord_flip()
  39. ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  40. geom_boxplot() +
  41. coord_flip()
  42. #极坐标系coord_polar()
  43. bar <- ggplot(data = diamonds) +
  44. geom_bar(
  45. mapping = aes(x = cut, fill = cut),
  46. width = 1
  47. ) +
  48. theme(aspect.ratio = 1) +
  49. labs(x = NULL, y = NULL)
  50. bar
  51. bar + coord_flip() #翻转坐标系
  52. bar + coord_polar()#极坐标系

绘图模板

image.png

3、ggpubr

image.png

  1. # ggpubr 搜代码直接用,不需要系统学习
  2. # sthda上有大量ggpubr出的图
  3. library(ggpubr)
  4. ggscatter(iris,x="Sepal.Length",
  5. y="Petal.Length",
  6. color="Species")
  7. theme_bw() #可将背景消除
  8. p <- ggboxplot(iris, x = "Species",
  9. y = "Sepal.Length",
  10. color = "Species",
  11. shape = "Species",
  12. add = "jitter")
  13. p
  14. my_comparisons <- list( c("setosa", "versicolor"),
  15. c("setosa", "virginica"),
  16. c("versicolor", "virginica") )
  17. p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  18. stat_compare_means(label.y = 9)

图片的保存与导出

  1. 三种方式
  2. #1.ggplot2,图片的后缀很重要
  3. ggsave(“名称.png”)
  4. ggsave(p,filename=“名称.png”)
  5. #2.三段论
  6. #3.eoffice
  7. library(eoffice)
  8. topptx(p,“名称.pptx”)
  9. #图片保存的三种方法
  10. #1.基础包作图的保存
  11. pdf("iris_box_ggpubr.pdf")
  12. boxplot(iris[,1]~iris[,5])
  13. text(6.5,4, labels = 'hello')
  14. dev.off()
  15. #2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave
  16. p <- ggboxplot(iris, x = "Species",
  17. y = "Sepal.Length",
  18. color = "Species",
  19. shape = "Species",
  20. add = "jitter")
  21. ggsave(p,filename = "iris_box_ggpubr.png",width = 12,height = 9)
  22. #3.eoffice包 导出为ppt,全部元素都是可编辑模式
  23. library(eoffice)
  24. topptx(p,"iris_box_ggpubr.pptx")
  25. #https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ

image.png

拼图

patchwork
image.png

关闭画板:dev.off()

image.png

画图代码来源:STHDA

image.png
画图R包:ggstatsplot
library(ggstatsplot)
image.png

画图的思维

image.png

作业

  1. # 6-1
  2. # 1.加载test.Rdata,分别test的以a和b列作为横纵坐标,change列映射颜色,画点图。
  3. load("test.Rdata")
  4. ggplot(data = test)+
  5. geom_point(mapping = aes(x = a,
  6. y = b,
  7. color = change))
  8. # 2.尝试修改点的颜色为暗绿色(darkgreen)、灰色、红色
  9. ggplot(data = test)+
  10. geom_point(mapping = aes(x = a,
  11. y = b,
  12. color = change))+
  13. scale_color_manual(values = c("darkgreen","grey","red"))
  14. # 1.尝试写出下图的代码,小提琴图
  15. ggplot(iris,aes(x = Sepal.Width,
  16. y = Species))+
  17. geom_violin(aes(fill = Species)) +
  18. geom_boxplot()+
  19. geom_jitter(aes(shape=Species))
  20. ggplot(data = iris,mapping = aes(x = Species,y = Sepal.Width))+
  21. geom_violin(aes(fill = Species))+
  22. geom_boxplot()+
  23. geom_jitter(aes(shape = Species))+
  24. coord_flip()
  25. # 6-3
  26. # 任意作3张ggplot2图
  27. ggplot(data = iris) +
  28. geom_smooth(mapping = aes(x = Sepal.Length,
  29. y = Petal.Length),
  30. color = "blue")
  31. # 1.探索labs() 函数如何使用
  32. #1.1 设置标题、副标题、引用
  33. ggplot(data = iris) +
  34. geom_smooth(mapping = aes(x = Sepal.Length,
  35. y = Petal.Length),
  36. color = "blue")+
  37. labs(title = "This", subtitle = "is", caption = "caption")
  38. #1.2 修改x轴和y轴的标题??
  39. ggplot(data = iris) +
  40. geom_point(mapping = aes(x = Sepal.Length,
  41. y = Petal.Length),
  42. color = "blue")+
  43. labs(xlab = "New x lab", ylab = "New y lab")
  44. # 2.尝试多种方式保存,并探索如何在保存的同时调整宽和高
  45. ggsave(p,filename = "iris_box_ggpubr.png",width = 12,height = 9)
  46. # 3.练习patchwork拼图
  47. patch <- p1 + p2