复现Naute子刊图,换数据即可使用!
本次使用R语言复现Nature Communications上的1张组合图,这张图兼具颜值+节约版面!
✅读入测试数据!
✅关键代码,
# 关键代码
library(ggplot2)
library(dplyr)
library(cowplot)
# --- 外圈图 ---
p_outer <- ggplot(data_aug, aes(x=factor(id), y=average, fill=w)) +
geom_bar(stat="identity", width=0.8, show.legend=FALSE, alpha=1) +
coord_polar(start=0) +
geom_path(data=circle_df, aes(x=angle*(n_groups)/(2*pi), y=y), color="black", size=0.2, inherit.aes=FALSE) +
theme(
plot.background=element_rect(fill="white",color=NA)
) +
geom_text(aes(label=label, y=average/2 + 1500),
angle=data_aug$angle, hjust=data_aug$hjust,
size=3.5, color="black") +
geom_text(aes(label=value_label, y=average/2),
angle=data_aug$angle, hjust=0.5,
size=3.5, color="black")
# --- 内圈图 (分组柱 + 折线) ---
p_inner <- ggplot(data_summary, aes(x=w, y=mean_val, fill=w)) +
geom_bar(stat="identity", width=0.6, alpha=1) +
geom_point(data=data, aes(x=w, y=average), position=position_jitter(width=0.1), size=0.8) +
geom_curve(aes(x = 1, xend = 1.5, y = 7500, yend = 4000),
curvature = 0.2, color = "darkgreen", size = 2, alpha = 0.2) +
geom_curve(aes(x = 1.5, xend = 2.5, y = 4000, yend = 5000),
curvature = -0.1, color = "darkgreen", size = 2, alpha = 0.2) +
geom_segment(aes(x = 2.5, xend = 3, y = 5000, yend = 2000),
color = "darkgreen", size = 2, alpha = 0.2,
arrow = arrow(type = "closed", length = unit(0.3, "cm"))) +
geom_errorbar(
aes(ymin=mean_val - sd_val, ymax=mean_val + sd_val),
width=0.4, color="black", size=0.4
)
# --- 拼图 ---
final_plot <- ggdraw() +
draw_plot(p_outer) +
draw_plot(p_inner, x=0.35, y=0.35, width=0.25, height=0.25)
print(final_plot)
✅该图中,
外圈是一个三组环状barplot,展示各时间点(1、4、8 week)各器官("brain", "heart", "lung", "kidney", "spleen"等10种)中检测到的蛋白质总数量。 中间柱状图显示各时间点,各器官中检测到的蛋白质平均数量,同时展示erroebar(数据为平均值 ± 标准差)。 中间散点图显示各时间点,各器官中检测到的蛋白质数量,与外圈数据一致。 中间折现图显示各时间点,各器官中检测到的蛋白质数量的走势。
✅值得学习的R可视化知识点:
🔸极坐标转换方法,
ggplot(data_aug, aes(x=factor(id), y=average, fill=w)) +
geom_bar(stat="identity", width=0.8, show.legend=FALSE, alpha=1) +
coord_polar(start=0)
coord_polar(start=0)对水平barplot进行极坐标转换,转换成圆形barplot,
🔸文本添加,
# 添加文字
geom_text(aes(label=label, y=average/2 + 1500),
angle=data_aug$angle, hjust=data_aug$hjust,
size=3.5, color="black") +
# 添加数值
geom_text(aes(label=value_label, y=average/2),
angle=data_aug$angle, hjust=0.5,
size=3.5, color="black")
🔸geom_path添加弧线,ggplot2 geom_path的核心功能是根据数据的原始顺序连接各个数据点,形成连续的路径,
# 添加完整的黑色圆圈
geom_path(data=circle_df, aes(x=angle*(n_groups)/(2*pi), y=y), color="black", size=0.2, inherit.aes=FALSE)
🔸errorbar添加,
geom_errorbar(
aes(ymin=mean_val - sd_val, ymax=mean_val + sd_val),
width=0.4, color="black", size=0.4
)
🔸趋势线添加,
# 第一段
geom_curve(aes(x = 1, xend = 1.5, y = 7500, yend = 4000),
curvature = 0.2, color = "darkgreen", size = 2, alpha = 0.2) +
# 第二段
geom_curve(aes(x = 1.5, xend = 2.5, y = 4000, yend = 5000),
curvature = -0.1, color = "darkgreen", size = 2, alpha = 0.2) +
# 第三段
geom_segment(aes(x = 2.5, xend = 3, y = 5000, yend = 2000),
color = "darkgreen", size = 2, alpha = 0.2,
arrow = arrow(type = "closed", length = unit(0.3, "cm")))
前两段通过geom_curve添加曲线,第三段通过geom_segment添加箭头线。二者使用方法类似,传入起始和终止xy坐标即可。
🔸cowplot拼图,
# --- 拼图 ---
final_plot <- ggdraw() +
draw_plot(p_outer) +
draw_plot(p_inner, x=0.35, y=0.35, width=0.25, height=0.25)
print(final_plot)
将内圈图通过cowplot拼图嵌入外圈环状barplot中,
本期结束!
(收费,备注:299)
👇加入学习(收费,备注:299)