跟着高分SCI学画图:GPT教你绘制堆积柱状图
介绍
上期我们介绍了柱状冲积图,它是一种数据可视化技术,它通过柱状图的不同排列和组合来展示数据的层次结构和组成。
今天我们继续介绍剩余几种常见的柱状冲积图类型。
可视化展示
示例一:双向柱状图
示例二:水平垂直柱状堆积图(增加了自动列归一化算法)
示例三:水平条状图
示例四:分面柱状冲积图
代码
示例一:数据展示
setwd("你自己的工作目录")
# Load necessary libraries
#如果没安装运行: if(!require(你要安装的包))install.packages("你要安装的包")
# 加载包
library(ggplot2)
# 加载数据
df <- read.table("你自己的数据.txt",header = T,sep='\t')
df$group <- factor(df$group,levels = c("HPRC.EAS","CPC"))
df$sample <- factor(df$sample,levels = c("OR4K2","OR4K1","GRPIN2","OR9G1","SULT1A3","OR4Q3",
"PDPR","CYP2D6","PRAMEF18"))
df$value <- ifelse(df$group=="HPRC.EAS",-df$value,df$value)
head(df,n=10)
# 定义配色方案
colors_1 <- c("#ff6347", "#ff9980") # 活力橙红色调
colors_2 <- c("#003366", "#0066cc") # 深海蓝色调
colors_3 <- c("#d6aedd", "#ffccf9")# 柔和粉紫色调
colors_4 <- c("#ffc107", "#ffeb3b")# 日落黄色调
colors_5 <- c("#8b4513", "#d2b48c")# 复古棕色调
colors_6 <- c("#90ee90", "#add8e6")# 清新薄荷绿色调
colors_7 <- c("#800080", "#ba55d3")# 梦幻紫色调
# 选择要使用的配色方案(这里以配色方案1为例)
selected_colors <- colors_1
# 绘图
ggplot(df, aes(sample, value, fill = group)) +
geom_col(width = 0.8) +
coord_flip() +
scale_fill_manual(values = selected_colors) +
theme_bw() +
theme(panel.grid.major = element_line(color = "grey", linetype = "dashed"),
panel.grid.minor = element_line(color = "grey", linetype = "dotted"),
axis.text.x = element_text(color = "black", size = 13),
axis.text.y = element_text(color = "black", size = 14, face = "italic"),
axis.title = element_text(color = "black", size = 16),
legend.position = "top",
legend.title = element_blank(),
legend.text = element_text(color = "black", size = 15)) +
labs(x = NULL, y = "CNV Frequency") +
scale_y_continuous(breaks = seq(-1, 1, 0.2),
labels = as.character(abs(seq(-1, 1, 0.2))),
limits = c(-1, 1))
# 保存图表
ggsave("barplot1.png", height = 8, width = 6)
#保存绘图
library(eoffice)
topptx(filename="2.pptx",height = 6,width = 4)
示例二:数据展示
setwd("你自己的工作目录")
# 加载所需的包
#如果没安装运行: if(!require(你要安装的包))install.packages("你要安装的包")
library(ggplot2)
library(reshape2)
library(cowplot)
# 读取数据
df <- read.csv('你自己的数据.csv')
head(df,n=10)
# 第一列是亚群名,其余列是样本数据
# 计算除了第一列之外,每一列的和
col_sums <- colSums(df[, -1])
# 对除了第一列之外的每一列进行归一化,且使每列数值之和为1
df_normalized <- df
df_normalized[, -1] <- sweep(df[, -1], 2, col_sums, `/`)
head(df_normalized,n=10)
# 将数据转换为长格式
df_long <- melt(df_normalized, id.vars = 'Sample', variable.name = 'group', value.name = 'value')
#配色
colors_classic_blue <- c("#ba55d3", "#0066cc", "#add8e6", "#e6550d", "#ff9980", "#e7cb94", "#7b4173", "#a55194")
# 绘制水平堆叠柱状图,group 作为横坐标
p_horizontal <- ggplot(df_long, aes(x = group, y = value, fill = Sample)) +
geom_bar(stat = "identity", position = "stack") +
scale_fill_manual(values = colors_classic_blue) + # 使用自定义颜色
coord_flip() +
labs(x = "Group", y = "Value", title = "barplot") +
theme_minimal()
# 绘制垂直堆叠柱状图,group 作为横坐标
p_vertical <- ggplot(df_long, aes(x = group, y = value, fill = Sample)) +
geom_bar(stat = "identity", position = "stack") +
scale_fill_manual(values = colors_classic_blue) + # 使用自定义颜色
labs(x = "Group", y = "Value") +
theme_minimal()
# 拼接两个图表
combined_plot <- plot_grid(p_horizontal, p_vertical, nrow = 1)
# 显示拼接后的图表
print(combined_plot)
# 保存图表
ggsave("combined_plot.png", plot = combined_plot, width = 12, height = 6,bg='white')
#保存绘图
library(eoffice)
topptx(filename="1.pptx",height = 6,width = 10)
示例三:数据展示
setwd("你自己的工作目录")
# 安装并加载所需的包
#如果没安装运行: if(!require(你要安装的包))install.packages("你要安装的包")
# install.packages("ggplot2")
library(ggplot2)
# 读取CSV文件
data <- read.csv("你自己的数据.csv")
head(data,n=10)
# 使用提供的渐变颜色绘制条形图,并添加灰黑色边框
ggplot(data, aes(x=reorder(variables, incMSE), y=incMSE)) +
geom_bar(stat="identity", aes(fill=incMSE)) +
coord_flip() + # 将条形图横置
scale_fill_gradientn(colors=c("#DAF2E1", "#7CD7B1", "#79a0b6", "#7668bb")) +
labs(x="Variables", y="%IncMSE") +
theme_minimal() +
theme(
plot.background = element_rect(fill = "white", colour = "black"), # 图表的背景颜色和边框颜色
panel.border = element_rect(colour = "gray82", fill=NA, size=1.5) # 面板边框颜色和大小
)
# 输出图片(可选)
ggsave(file="output.png", width=6, height=8)
#保存绘图
library(eoffice)
topptx(filename="1.pptx",height = 4,width = 6)
示例四:数据展示
setwd("你自己的工作目录")
#加载必须的包
#如果没安装运行: if(!require(你要安装的包))install.packages("你要安装的包")
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(reshape) # Flexibly Reshape Data
library(ggalluvial) # Alluvial Plots in 'ggplot2'
library(dplyr) # A Grammar of Data Manipulation
library(ggh4x) # Hacks for 'ggplot2'
##加载数据
df <- read.table("你的数据.txt", check.names = F, header = 1, sep = "\t",row.names = 1)
##计算相对丰度
df2 <- as.data.frame(t(apply(t(df[5:9]),2,function(x) x/sum(x))))
df2$sample <- rownames(df2)
df3 <- df[1:4]
df3$sample <- rownames(df3)
data <- merge(df3, df2, by = "sample")
head(data,n=10)
data1 <- melt(data,
id.vars = c("sample","group1","group2","group3","time"),
measure.vars = c('SpeciesA','SpeciesB','SpeciesC',
'SpeciesD','SpeciesE'))
names(data1)[6] <- 'Species' #修改列名
data1$time <- factor(data1$time, levels = df$time[1:6])
##绘图
df_time_group12 <- data1 %>%
select(group1,group2,time,Species,value) %>%
group_by(group1,group2,time,Species) %>%
summarise_all(mean)
##绘图
ggplot(df_time_group12, aes(time, y = value*100,
fill = Species,
stratum = Species, alluvium = Species))+
geom_stratum(width = 0.6, color='white')+
geom_alluvium(alpha = 0.4,
width = 0.6,
color='white',
linewidth = 1,
curve_type = "linear")+
scale_y_continuous(expand = c(0,0))+
facet_nested(~group1+group2,scales = "free",space = "free")+
labs(y="Relative Abundance(%)")+
theme_bw()+
theme(panel.grid = element_blank(),
axis.text.y = element_text(size=10),
axis.text.x = element_text(size=10, angle = 45, vjust = 1, hjust = 1),
axis.title = element_text(size=12),
strip.text = element_text(color = "black", size = 12),
strip.background = element_rect(color = "black", fill="grey90"))+
guides(fill=guide_legend(keywidth = 1.2, keyheight = 1.2)) +
scale_fill_manual(values = c("#99B399","#7799D2","#00BFFF",
"#FAD6A5","#D1C4E9"))
# scale_fill_manual(values = c("#fc636b","#3be8b0","#1aafd0",
# "#6a67ce","#ffb900"))
# #保存绘图
# library(eoffice)
# topptx(filename="形态分布图.pptx",height = 6,width = 12)
答疑
我解答一下很多同学在输出图片时遇到的图片尺寸不合适的问题。比如这种情况可以在Save Plot as Image中手动调整Width和Height解决。
此外,如果迁移本系列代码时图片输出怪异,可能是数据尺寸没有统一,比如:
这大概率是由于数据没有进行归一化导致的,下列代码可以保证每列/每行数据和为一且比例不变。
df <- read.csv('你自己的数据.csv')
# 第一列是亚群名,其余列是样本数据
# 计算除了第一列之外,每一列的和
col_sums <- colSums(df[, -1])
# 对除了第一列之外的每一列进行归一化,且使每列数值之和为1
df_normalized <- df
df_normalized[, -1] <- sweep(df[, -1], 2, col_sums, `/`)
df <- read.csv('你自己的数据.csv')
#第一行是亚群名,其余行是样本数据
# 计算除了第一行之外,每一行的和
row_sums <- rowSums(df[-1, ])
# 对除了第一行之外的每一行进行归一化
df_normalized <- df
df_normalized[-1, ] <- df[-1, ] / row_sums
# 查看归一化后的数据
print(df_normalized)
这样就正常了
本文作者:李家旭
现在:
长按扫码关注:科研生信充电宝
10元赞赏本文,即喜欢作者~
即可直接解锁:
《跟着高分SCI学画图:GPT教你绘制堆积柱状图》对应资源哦~
看到这里你还不心动吗?
赶紧关注、转发、点赞、分享,领取你的专属福利吧~
好啦,以上就是今天推文的全部内容啦!
版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本公众号不拥有所有权,也不承担相关法律责任。
如果您发现本公众号中有涉嫌抄袭的内容,欢迎发送邮件至:kysxcdb@163.com 进行举报,一经查实,本公众号将立刻删除涉嫌侵权内容。