为了避免各位错过最新的推文教程,强烈建议大家将“科研后花园”设置为“星标”!
文献图片展示:
1、加载R包(未安装需要自行安装):
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(reshape2) # Flexibly Reshape Data: A Reboot of the Reshape Package
library(ggside) # Side Grammar Graphics
library(dplyr) # A Grammar of Data Manipulation
library(tidyr) # Tidy Messy Data
library(ggnewscale) # Multiple Fill and Colour Scales in 'ggplot2'
library(cowplot) # Streamlined Plot Theme and Plot Annotations for 'ggplot2'
2、加载数据(随机编写,无实际意义):
df1 <- read.table("data1.txt", header = 1, sep = "\t")
df2 <- read.table("data2.txt", header = 1, sep = "\t")
df3 <- read.table("data3.txt", header = 1, sep = "\t", check.names = F)
3、绘图模板展示(模板部分注释源码及原始测试数据可在公众号后台查看具体获取方式):
####绘图
data1 <- melt(df1, id.vars = c("Sample", "Group1", "Group2"), variable.name = "Period")
xside_data <- data1 %>%
select(Sample, Group1,Group2) %>%
pivot_longer(cols = c(Group1,Group2)) %>%
distinct()
xside_data$name <- factor(xside_data$name, levels = c("Group2", "Group1"))
ggplot(data1, aes(Sample, Period))+
geom_tile(aes(fill = value))+
scale_fill_gradient2(high = "#fe6263", mid = "white", low = "#0099cc",
name = NULL,
limits = c(-1000,1000),
breaks = c(-1000,-500,0,500,1000),
guide = guide_colourbar(frame.colour = "black",
ticks.colour = "black",
legend.key.width = unit(0.3, "lines"),
legend.key.height = unit(8, "lines")))+
geom_xsidetile(data = xside_data,
aes(y = name, xfill = value))+
scale_xfill_manual(values = c("#f44321","#5091cd","#dbc65d","#7ac143",
"#f99104","#00b7c9"),
name = NULL,
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
scale_y_discrete(position = "right")+
theme_void()+
theme(axis.text.y = element_text(size = 8, color = "black", hjust = 0),
legend.position = "bottom",
legend.direction = 'vertical')->p1
data2 <- melt(df2, id.vars = c("Sample"), variable.name = "group", value.name = "Species")
ggplot(data2, aes(Sample, group, fill = Species))+
geom_tile(color = "white")+
scale_fill_manual(values = c("#f5f5f5", "#3be8b0","#1aafd0","#6a67ce","#ffb900","#fc636b"),
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
scale_y_discrete(position = "right")+
theme_bw()+
theme(axis.text.y = element_text(size = 8, color = "black"),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.position = "bottom",
legend.direction = 'vertical')->p2
df4 <- df3 %>%
mutate(`Age (year)` = cut(`Age (year)`, breaks = c(-Inf, 25, 50, 75, Inf),
labels = c("0~25", "25~50", "50~75", ">75")),
`Height (cm)` = cut(`Height (cm)`, breaks = c(-Inf, 160, 180, 200, Inf),
labels = c("<160", "160~180", "180~200", ">200")),
`Width (cm)` = cut(`Width (cm)`, breaks = c(-Inf, 30, 40, Inf),
labels = c("20~30", "30~40", "40~50")),
`Length (cm)` = cut(`Length (cm)`, breaks = c(-Inf, 100, 120, Inf),
labels = c("<100", "100~120", ">120")),
Day1 = cut(Day1, breaks = c(-Inf, 2, 3, 4, Inf),
labels = c("1~2", "2~3", "3~4", "4~5")),
Day2 = cut(Day2, breaks = c(-Inf, 2, 5, 8, Inf),
labels = c("<2", "2~5", "5~8", ">8")),
Day3 = cut(Day3, breaks = c(-Inf, 25, 50, 75, Inf),
labels = c("0~25", "25~50", "50~75", "75~100")),
Day4 = cut(Day4, breaks = c(-Inf, 1, Inf),
labels = c("<1", ">1")))
data3 <- melt(df4, id.vars = c("Sample"), variable.name = "Group")
ggplot(data3, aes(Sample, Group))+
geom_tile(data = ~ subset(., Group == "Age (year)"),
aes(fill =value), color = "grey80")+
scale_fill_manual(values = c("#A8C25E","#FFCA99","#F29366","#B56035"),
name = "Age (year)",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Sex"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#e64b50","#dbc65d"),name = "Sex",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Stage"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#008eaa","#0077c8","#a51890","#da1884"),
name = "Stage",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Height (cm)"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#eff3f6","#ced7df","#59626a","#143e50"),
name = "Height (cm)",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Width (cm)"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#98c807","#b1a24a","#edd812"),
name = "Width (cm)",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Length (cm)"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#A8C25E","#FFCA99","#F29366"),
name = "Length (cm)",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Day1"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#c1f1fc","#ebffac","#ffc2e5","#ffaaaa"),
name = "Day1",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Day2"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#fdb94e","#f69653","#f07654","#ef5956"),
name = "Day2",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Day3"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#13a538","#009ee3","#954a97","#e50064"),
name = "Day3",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
new_scale_fill() +
geom_tile(data = ~ subset(., Group == "Day4"),
aes(fill = value), color = "grey80")+
scale_fill_manual(values = c("#fe423f","#02a388"),
name = "Day4",
guide=guide_legend(keywidth=0.8, keyheight=0.8))+
scale_y_discrete(position = "right")+
theme_void()+
theme(axis.text.y = element_text(size = 8, color = "black", hjust = 0),
legend.position = "bottom",
legend.direction = 'vertical')->p3
library(patchwork) # The Composer of Plots
p1/p2/p3+
plot_layout(guides = 'collect', heights = c(3, 1, 1.5))&
theme(legend.position='bottom')
最后在在AI中对细节进行调整:
需要附带注释的源码及测试数据请查看下方绘图模板获取方式!!
PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。
「R绘图模板」跟着New Phytologist学绘图—共发生网络图的绘制!!!!!!
「R绘图模板」基因注释+箭头图+分面+标签!!!
「R绘图模板」组合图系列-箱线图+散点+分组散点注释!!!
「R绘图模板」分组气泡图+边际频率分布直方图+拟合曲线!!!
「R绘图模板」世界地图+采样点标记+柱状堆积图!!!
「R绘图模板」环形无分组排序柱状堆积图!!!
「R绘图模板」多y轴组合图-柱状堆积图+箱线图!!!
「R绘图模板」环形分组气泡图(重发版-修改标题)!!!
「R绘图模板」蝶形图系列-并列箱线图与并列柱状图组合!!!
「R绘图模板」组合图系列-热图+棒棒糖+条形图!!!
「R绘图模板」mantel test分析拓展——多样式多形式展示数据!!!
「R绘图模板」用于两组比较的对称小提琴图+箱线图+散点图+均值点+误差棒+显著性!!!
「R绘图模板」多层嵌套柱状图+字母标记+标签嵌套!!!
「R绘图模板」3D热图的绘制!!!
「R绘图模板」进化树+分组注释+配对连线!!!
「R绘图模板」组合图-并列柱状图+数字标签+误差棒+占比饼图!!!
「R绘图模板」箱线图+显著性差异着色+分组差异检验!!!
「R绘图模板」柱状堆积图+柱间连线+文本标签+误差棒+字母标记!!!
「R绘图模板」嵌套柱状图+柱状堆积图+显著性!!!
「R绘图模板」环形分组散点图+误差棒+分组标签+灰白间隔背景+辅助线!!!
「R绘图模板」箱线图+散点+配对连线+显著性+分面!!!
「R绘图模板」组合图-进化树+柱状图+箱线图+热图!!!
「R绘图模板」内向环形柱状图+分组+分面!!!
「R绘图模板」层级网络图的绘制!!!
「R绘图模板」世界地图+采样点标记+分组条形图+柱状图嵌套!!!
「R绘图模板」多分组箱线图+不同组字母标记+条纹填充!!!
「R绘图模板」并列柱状图+不同组字母标记+组间显著性标记!!!
「R绘图模板」组合型热图+分组标记+R值与P值同时可视化!!!
「R绘图模板」mantel test拓展—多图绘制与组合!!!
「R绘图模板」热图+分组+配对连线+统计差异!!!
「R绘图模板」跟着Food Chemistry学绘图——散点+误差棒+折线+字母标记!!!
「R绘图模板」环状箱线图+分组+显著性+均值标记!!!
「R绘图模板」环形水平条形图+柱状堆积图的绘制方法!!!!!!
「R绘图模板」嵌套柱状图+显著性+字母标记!!!
「R绘图模板」跟着Nature Communications学绘图—世界地图+采样点标记+饼图+柱状图!!!
「R绘图模板」跟着Nature Communications学绘图—并列柱状堆积图+误差线+显著性!!!
「R绘图模板」双层环形热图+不同方式显著性标注!!!
跟着Nature Communications学绘图——3D曲线图+多分组!!!
R绘图模板——成比例Venn图的绘制!!!
R绘图模板——网络图+微生物丰度与基因间的相关性+正负相关(粉丝投稿)!!!
R可视化——一文带你打通ggplot2个性化绘制柱状图(条形图)!!!
R可视化——一文带你打通ggplot2个性化绘制箱线图(更新版)!!!
R可视化——一文带你打通ggplot2个性化绘制散点图!!!