背景
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。
写在最后:有时间我们会努力更新的。大家互动交流可以前去论坛,地址在下面,复制去浏览器即可访问,弥补下公众号没有留言功能的缺憾。
bioinfoer.com
有些板块也可以预设为大家日常趣事的分享等,欢迎大家来提建议。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有