生信人

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

278

主题
发表于 2022-9-30 09:15:54 | 查看: 830| 回复: 0

背景
      
       ggplot2 色轮图
一、默认修改颜色
  1. mtcars$cyl <- as.factor(mtcars$cyl)
  2. p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill=cyl)) +geom_boxplot()
  3. p+scale_fill_brewer(palette = "Set2")
  4. p+scale_fill_manual(values = c("red","green","blue"))
复制代码
      
       离散型数据修改 ggplot2 默认配色
  1. p <- ggplot(mtcars, aes(x=wt, y=mpg,color=mpg)) +geom_point()
  2. p
  3. #两种渐变色
  4. p+scale_color_gradient(low = "yellow",high = "red")
  5. #三种渐变色
  6. p+scale_color_gradient2(low = "yellow",mid = "orange",high = "red")
复制代码
      
       为连续型数据增加两种渐变色

二、ggsci:科学文献配色
  1. library(ggsci)
  2. mtcars$cyl <- as.factor(mtcars$cyl)
  3. p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill=cyl)) +geom_boxplot()
  4. p
  5. #查看帮助文档
  6. # help(package="ggsci")
  7. p+scale_fill_aaas()
  8. p+scale_fill_npg() #nature配色
  9. p+scale_fill_nejm()
  10. p+scale_fill_jama()
  11. p+scale_fill_lancet()
复制代码
      
       利用 ggsci 调整为 nature 杂志配色

三、ggthemes:常见期刊主题
  1. library(ggthemes)
  2. p
  3. p+theme_economist()
  4. p+theme_wsj()
  5. p+theme_excel()
  6. p+theme_stata()
复制代码
      
       为 ggplot2 绘图调整以前的excel主题

四、绘图布局
  1. library(ggplot2)
  2. library(ggExtra)
  3. data(mtcars)

  4. # classic plot :
  5. p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) +
  6.   geom_point() + theme(legend.position="none")
  7. p
  8. # Set relative size of marginal plots (main plot 10x bigger than marginals)
  9. p1 <- ggMarginal(p, type="histogram", size=10)
  10. p1
  11. # Custom marginal plots:
  12. p2 <- ggMarginal(p, type="histogram", fill = "slateblue", xparams = list(  bins=10))
  13. p2
  14. # Show only marginal plot for x axis
  15. p3 <- ggMarginal(p, margins = 'x', color="purple", size=4)
  16. p3
复制代码
      
       使用 ggExtra 布局p1


五、绘图组合
  1. library(ggplot2)
  2. library(gridExtra)

  3. # Make 3 simple graphics:
  4. g1 <- ggplot(mtcars, aes(x=qsec)) + geom_density(fill="slateblue")
  5. g2 <- ggplot(mtcars, aes(x=drat, y=qsec, color=cyl)) + geom_point(size=5) +
  6.   theme(legend.position="none")
  7. g3 <- ggplot(mtcars, aes(x=factor(cyl), y=qsec, fill=cyl)) + geom_boxplot() +
  8.   theme(legend.position="none")
  9. g4 <- ggplot(mtcars , aes(x=factor(cyl), fill=factor(cyl))) +  geom_bar()

  10. # Plots
  11. grid.arrange(g2, arrangeGrob(g3, g4, ncol=2), nrow = 2)
  12. grid.arrange(g1, g2, g3, nrow = 3)
  13. grid.arrange(g2, arrangeGrob(g3, g4, ncol=2), nrow = 1)
  14. grid.arrange(g2, arrangeGrob(g3, g4, nrow=2), nrow = 1)
复制代码
      
       利用 gridExtra 组合图形

六、ggpubr 绘图
  1. library(ggpubr)
  2. ggviolin(ToothGrowth, x = "dose", y = "len",add = "jitter", shape = "dose")
  3. ggviolin(ToothGrowth, "dose", "len", fill = "dose",palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  4.          add = "boxplot", add.params = list(fill = "white"))
复制代码
      
       利用 ggviolin 绘制小提琴图


七、plotly 交互绘图
  1. library(plotly)
  2. #查看版本
  3. packageVersion('plotly')
  4. p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill=cyl)) +geom_boxplot()
  5. ggplotly(p)
复制代码
      生成的一个网页,鼠标移动、放大、可存储到本地png。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|生信人

GMT+8, 2024-4-29 12:47 , Processed in 0.036037 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表