当前位置:首页 > 技术知识 > 正文内容

R语言折线图+散点图可视化数据时间动态

maynowei7个月前 (08-16)技术知识104
图1 颗粒物的C:P和N:P比对供应比中的氮磷比(N:P)较为敏感,而 C:N 比则相对稳定( Seelen EA , et al. (2025). Nitrogen and phosphorus differentially control marine biomass production and stoichiometry . Nature Communications, 16, 5713 .
# 加载必要的包library(ggplot2)library(vegan)library(cowplot)library(dplyr)
# --------------------------# (1) 模拟数据生成(控制变异性)# --------------------------
# 1.1 折线图数据(减小随机波动范围)set.seed(123)n_replicates <- 3 # 每个组合的重复数df_line <- expand.grid( Day = c(0, 5, 10, 15, 25, 30), Treatment = LETTERS[1:5], Replicate = 1:n_replicates)
# 为每个处理-时间点生成更集中的数据(减小标准差)for (i in 1:nrow(df_line)) { day <- df_line$Day[i] treatment <- df_line$Treatment[i] base_value <- 15 + day * 0.2 # 基础值随天数轻微增加 df_line$Part.C[i] <- rnorm(1, mean = base_value, sd = 1.5) # 标准差从3减小到1.5}
# 计算均值和标准差df_summary <- df_line %>% group_by(Day, Treatment) %>% summarise( Mean = mean(Part.C), SD = sd(Part.C), .groups = "drop" )
# 1.2 PCoA 数据(保持不变)set.seed(456)n_samples <- 30n_otus <- 10otu_table <- matrix(rpois(n_samples * n_otus, lambda = 10), nrow = n_samples, ncol = n_otus)rownames(otu_table) <- paste0("Sample", 1:n_samples)metadata <- data.frame( Sample = rownames(otu_table), Treatment = rep(LETTERS[1:5], each = 6), Day = rep(c(0, 5, 10, 15, 25, 30), times = 5))
# --------------------------# (2) 绘制折线图(误差棒缩小)# --------------------------p_line <- ggplot(df_summary, aes(x = Day, y = Mean, color = Treatment, group = Treatment)) + geom_line(linewidth = 1) + geom_point(size = 4,shape=18) + geom_errorbar( aes(ymin = Mean - SD, ymax = Mean + SD), width = 0, linewidth = 0.8 ) + scale_color_manual(values = c("#FFC95E", "#FC7E86", "#507564", "#73C5DC", "#000000")) + labs( x = "Day", y = "Concentration (Part.C)", title = "" ) + theme_bw()+theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+ theme(axis.text=element_text(colour='black',size=9))+ theme( legend.position = "NA", plot.title = element_text(face = "bold", hjust = 0) )
# --------------------------# (3) PCoA 分析及绘图(图B,保持不变)# --------------------------dist_matrix <- vegdist(otu_table, method = "bray")pcoa_result <- cmdscale(dist_matrix, k = 2, eig = TRUE)pcoa_scores <- as.data.frame(pcoa_result$points)colnames(pcoa_scores) <- c("PCo1", "PCo2")pcoa_scores$Treatment <- metadata$Treatmentpcoa_scores$Day <- metadata$Dayvariance <- round(pcoa_result$eig / sum(pcoa_result$eig) * 100, 1)
p_pcoa <- ggplot(pcoa_scores, aes(x = PCo1, y = PCo2, color = Treatment, shape = factor(Day))) + geom_point(size = 4, shape=24, alpha = 1) + scale_color_manual(values = c("#FFC95E", "#FC7E86", "#507564", "#73C5DC", "#000000")) + scale_shape_manual(values = c(16, 17, 15, 18, 8, 3)) + labs( x = paste0("PCo1 (", variance[1], "%)"), y = paste0("PCo2 (", variance[2], "%)"), title = "", shape = "Day" ) + theme_bw()+theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+ theme(axis.text=element_text(colour='black',size=9))+ theme( legend.position = "right", plot.title = element_text(face = "bold", hjust = 0) )
# --------------------------# (4) 组合图形# --------------------------library(cowplot)cowplot::plot_grid(p_line, p_pcoa ,ncol= 2, rel_widths = c(1, 1.4))

相关文章

Axure案例小红书APP登陆界面划屏效果制作

有一款叫做小红书的APP(不是小黄书)登陆界面做的非常清新脱俗,教他家用Axure模仿练习一下他的登陆界面划屏效果,这个内容虽然有些多,但是制作方法并不难,耐心跟着做的话肯定没问题哒!点击观看效果效果...

Android之自定义ListView(一)(android 自定义view绘制流程)

PS:自定义View是Android中高手进阶的路线.因此我也打算一步一步的学习.看了鸿洋和郭霖这两位大牛的博客,决定一步一步的学习,循序渐进.学习内容:1.自定义View实现ListView的Ite...

Android指示器,轮播与循环轮播(android轮播图代码)

Android UI Libs之CircleIndicator1. 说明CircleIndicator,顾名思义,圆形指示器,只一个可以用来做轮播的第三方库。2. 配置在模块的build.gradle...

Google前工程主管“入住”Oracle(google公司前台)

ZDNet至顶网服务器频道 10月11日 新闻消息:Oracle 已聘用了前 Snapchat 和 Google 工程部主管 Peter Magnusson,其主要的职责是运行一个被重新调整过的 of...

China, UK should focus on cooperation, eliminate distractions: Chinese ambassador

LONDON, July 12 (Xinhua) -- China and the United Kingdom (UK) should implement the important consens...

SCO media, think tank summit releases Zhengzhou Consensus

ZHENGZHOU, July 25 (Xinhua) -- The Shanghai Cooperation Organization (SCO) Media and Think Tank Summ...