|
发表于 2022-9-30 09:15:54
|
查看: 1556 |
回复: 0
背景
ggplot2 色轮图
一、默认修改颜色
- mtcars$cyl <- as.factor(mtcars$cyl)
- p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill=cyl)) +geom_boxplot()
- p+scale_fill_brewer(palette = "Set2")
- p+scale_fill_manual(values = c("red","green","blue"))
复制代码
离散型数据修改 ggplot2 默认配色
- p <- ggplot(mtcars, aes(x=wt, y=mpg,color=mpg)) +geom_point()
- p
- #两种渐变色
- p+scale_color_gradient(low = "yellow",high = "red")
- #三种渐变色
- p+scale_color_gradient2(low = "yellow",mid = "orange",high = "red")
复制代码
为连续型数据增加两种渐变色
二、ggsci:科学文献配色
- library(ggsci)
- mtcars$cyl <- as.factor(mtcars$cyl)
- p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill=cyl)) +geom_boxplot()
- p
- #查看帮助文档
- # help(package="ggsci")
- p+scale_fill_aaas()
- p+scale_fill_npg() #nature配色
- p+scale_fill_nejm()
- p+scale_fill_jama()
- p+scale_fill_lancet()
复制代码
利用 ggsci 调整为 nature 杂志配色
三、ggthemes:常见期刊主题
- library(ggthemes)
- p
- p+theme_economist()
- p+theme_wsj()
- p+theme_excel()
- p+theme_stata()
复制代码
为 ggplot2 绘图调整以前的excel主题
四、绘图布局
- library(ggplot2)
- library(ggExtra)
- data(mtcars)
- # classic plot :
- p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) +
- geom_point() + theme(legend.position="none")
- p
- # Set relative size of marginal plots (main plot 10x bigger than marginals)
- p1 <- ggMarginal(p, type="histogram", size=10)
- p1
- # Custom marginal plots:
- p2 <- ggMarginal(p, type="histogram", fill = "slateblue", xparams = list( bins=10))
- p2
- # Show only marginal plot for x axis
- p3 <- ggMarginal(p, margins = 'x', color="purple", size=4)
- p3
复制代码
使用 ggExtra 布局p1
五、绘图组合
- library(ggplot2)
- library(gridExtra)
- # Make 3 simple graphics:
- g1 <- ggplot(mtcars, aes(x=qsec)) + geom_density(fill="slateblue")
- g2 <- ggplot(mtcars, aes(x=drat, y=qsec, color=cyl)) + geom_point(size=5) +
- theme(legend.position="none")
- g3 <- ggplot(mtcars, aes(x=factor(cyl), y=qsec, fill=cyl)) + geom_boxplot() +
- theme(legend.position="none")
- g4 <- ggplot(mtcars , aes(x=factor(cyl), fill=factor(cyl))) + geom_bar()
- # Plots
- grid.arrange(g2, arrangeGrob(g3, g4, ncol=2), nrow = 2)
- grid.arrange(g1, g2, g3, nrow = 3)
- grid.arrange(g2, arrangeGrob(g3, g4, ncol=2), nrow = 1)
- grid.arrange(g2, arrangeGrob(g3, g4, nrow=2), nrow = 1)
复制代码
利用 gridExtra 组合图形
六、ggpubr 绘图
- library(ggpubr)
- ggviolin(ToothGrowth, x = "dose", y = "len",add = "jitter", shape = "dose")
- ggviolin(ToothGrowth, "dose", "len", fill = "dose",palette = c("#00AFBB", "#E7B800", "#FC4E07"),
- add = "boxplot", add.params = list(fill = "white"))
复制代码
利用 ggviolin 绘制小提琴图
七、plotly 交互绘图
- library(plotly)
- #查看版本
- packageVersion('plotly')
- p <- ggplot(mtcars, aes(x=cyl, y=mpg, fill=cyl)) +geom_boxplot()
- ggplotly(p)
复制代码 生成的一个网页,鼠标移动、放大、可存储到本地png。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
|