SCI图片复现:面积图
省流:完整代码拉到最后
首先数据表格要求:
1. 趋势气泡图数据:这个数据应该是一个CSV文件,具有三个列。第一列应该是"Diameter",代表直径,应该是数字。第二列应该是"expression",代表表达量,也应该是数字。第三列应该是"group",代表组,这是一个分类变量。这个CSV文件不应该有行名。
2. Nature图数据:这个数据也应该是一个CSV文件,具有三个列。第一列应该是"celltype",代表细胞类型,这是一个分类变量。第二列应该是"stage",代表阶段,这也是一个分类变量。第三列应该是"Freq",代表频率,应该是数字。这个CSV文件不应该有行名。
以下是这两种数据的示例:
趋势气泡图数据:
Diameter | expression | group |
---|---|---|
50 | 0.5 | A |
100 | 0.6 | B |
150 | 0.7 | C |
Nature图数据:
celltype | stage | Freq |
---|---|---|
Type1 | CS12 | 0.2 |
Type2 | CS13 | 0.3 |
Type3 | CS15 | 0.5 |
请注意,所有的数据都应该是完整的,也就是说,没有缺失值。如果有缺失值,那么可能会导致代码运行错误。
步骤1:生成示例数据
首先,我会为这两种图形生成示例数据。
- 对于趋势气泡图,数据可能包含以下列:“Diameter”(直径),“expression”(表达量)以及“group”(组别)。
- 对于nature图,数据可能包含以下列:“celltype”(细胞类型),“stage”(阶段)和“Freq”(频率)。
# 创建趋势气泡图数据
set.seed(123) # 设置随机数种子以保证结果可复现
trend_data <- data.frame(
Diameter = sample(100:500, 200, replace = TRUE),
expression = runif(200, 0, 100),
group = factor(sample(LETTERS[1:4], 200, replace = TRUE))
)
head(trend_data) # 打印前几行数据
# 创建nature数据
nature_data <- data.frame(
celltype = factor(rep(LETTERS[1:4], each = 50)),
stage = factor(sample(c("CS12", "CS13", "CS15", "CS17", "CS20"), 200, replace = TRUE)),
Freq = runif(200, 0, 0.5)
)
head(nature_data) # 打印前几行数据
修改后的趋势气泡图代码
library(ggplot2)
# 读入趋势气泡图数据
# data <- read.csv("趋势气泡图.csv", header = T) # 原始代码
data <- trend_data # 修改后的代码
col <- c("#D57444", "#33B1C0", "#34993B", "#8C67AC")
ggplot(data, aes(x=Diameter, y=expression, fill=group)) +
geom_area(size=0.01,colour="white") + # 加面积
labs(y="Expression") +
theme_bw() +
scale_fill_manual(values=col) +
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),
axis.text = element_text(color = "black",size = 10),
axis.title = element_text(color = 'black', size=12)) +
geom_vline(aes(xintercept =100),linetype="dashed", size=1.2, colour="white") + # 加竖线
geom_vline(aes(xintercept =200),linetype="dashed", size=1.2, colour="white") +
geom_vline(aes(xintercept =300),linetype="dashed", size=1.2, colour="white") +
geom_vline(aes(xintercept =400),linetype="dashed", size=1.2, colour="white")
修改后的Nature图代码
# 读入Nature数据
# df <- read.csv("nature_ratio.csv", header = T) # 原始代码
df <- nature_data # 修改后的代码
table(df$Stage)
A <- prop.table(table(df$celltype, df$stage), margin = 2)
A <- as.data.frame(A)
colnames(A) <- c("celltype", "stage", "Freq")
cluster_cols <- c("#DC050C", "#FB8072", "#1965B0", "#7BAFDE", "#882E72",
"#B17BA6", "#FF7F00", "#FDB462", "#E7298A", "#E78AC3",
"#33A02C", "#B2DF8A", "#55A1B1", "#8DD3C7", "#A6761D",
"#E6AB02", "#7570B3", "#BEAED4", "#666666", "#999999",
"#aa8282", "#d4b7b7", "#8600bf", "#ba5ce3", "#808000",
"#aeae5c", "#1e90ff", "#00bfff", "#56ff0d", "#ffff00")
ggplot(A,aes(x = stage, y =Freq, group=celltype)) +
stat_summary(geom = 'line',fun='mean',cex=1,col='white') + # 先要有折线
geom_area(data = A,aes(fill=celltype)) + # 折线下面积,填充用celltype
scale_fill_manual(values=cluster_cols) +
labs(x=NULL, y=NULL) +
theme_bw() +
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),
axis.text = element_text(color = "black",size = 10)) +
geom_vline(aes(xintercept ="CS12"),linetype="dashed", size=1.2, colour="white") +
geom_vline(aes(xintercept ="CS13"),linetype="dashed", size=1.2, colour="white") +
geom_vline(aes(xintercept ="CS15"),linetype="dashed", size=1.2, colour="white") +
geom_vline(aes(xintercept ="CS17"),linetype="dashed", size=1.2, colour="white") +
geom_vline(aes(xintercept ="CS20"),linetype="dashed", size=1.2, colour="white")
优化后的趋势气泡图代码
library(ggplot2)
# 读入趋势气泡图数据
data <- trend_data # 使用我们前面创建的示例数据
col <- c("#D57444", "#33B1C0", "#34993B", "#8C67AC") # 指定每个组别的颜色
# 创建ggplot对象
p <- ggplot(data, aes(x = Diameter, y = expression, fill = group))
# 添加图层
p <- p +
geom_area(size = 0.01, colour = "white") + # 添加面积层
labs(y = "Expression") + # 设置y轴标签
theme_bw() + # 使用黑白主题
scale_fill_manual(values = col) + # 手动设置填充颜色
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(color = "black", size = 10),
axis.title = element_text(color = 'black', size = 12)
) # 设置主题元素
# 添加竖线
for (i in seq(100, 400, by = 100)) {
p <- p + geom_vline(aes(xintercept = i), linetype = "dashed", size = 1.2, colour = "white")
}
p # 打印图形
优化后的Nature图代码
# 读入Nature数据
df <- nature_data # 使用我们前面创建的示例数据
# 计算每个阶段的频率
A <- prop.table(table(df$celltype, df$stage), margin = 2)
A <- as.data.frame(A)
colnames(A) <- c("celltype", "stage", "Freq")
# 指定每个细胞类型的颜色
cluster_cols <- c("#DC050C", "#FB8072", "#1965B0", "#7BAFDE", "#882E72",
"#B17BA6", "#FF7F00", "#FDB462", "#E7298A", "#E78AC3",
"#33A02C", "#B2DF8A", "#55A1B1", "#8DD3C7", "#A6761D",
"#E6AB02", "#7570B3", "#BEAED4", "#666666", "#999999",
"#aa8282", "#d4b7b7", "#8600bf", "#ba5ce3", "#808000",
"#aeae5c", "#1e90ff", "#00bfff", "#56ff0d", "#ffff00")
# 创建ggplot对象
p <- ggplot(A, aes(x = stage, y = Freq, group = celltype))
# 添加图层
p <- p +
stat_summary(geom = 'line', fun = 'mean', cex = 1, col = 'white') + # 添加折线
geom_area(aes(fill = celltype)) + # 添加面积层
scale_fill_manual(values = cluster_cols) + # 手动设置填充颜色
labs(x = NULL, y = NULL) + # 清除x和y轴的标签
theme_bw() + # 使用黑白主题
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(color = "black", size = 10)
) # 设置主题元素
# 添加竖线
for (stage in c("CS12", "CS13", "CS15", "CS17", "CS20")) {
p <- p + geom_vline(aes(xintercept = stage), linetype = "dashed", size = 1.2, colour = "white")
}
p # 打印图形
优化代码,使得图片更为丰富和美观。我将使用ggthemes包和ggtitle函数来改善图表的外观,并给图表添加标题。
# 安装和加载ggthemes包
if (!require(ggthemes)) {
install.packages("ggthemes")
}
library(ggthemes)
# 优化趋势气泡图
p_trend <- ggplot(trend_data, aes(x = Diameter, y = expression, fill = group)) +
geom_area(size = 0.01, colour = "white") +
scale_fill_manual(values = col) +
labs(x = "Diameter", y = "Expression", title = "Trend Bubble Plot") +
theme_tufte() + # 使用Tufte主题,这个主题具有简洁的风格
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "bottom",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(color = "black", size = 10),
axis.title = element_text(color = 'black', size = 12)
)
for (i in seq(100, 400, by = 100)) {
p_trend <- p_trend + geom_vline(aes(xintercept = i), linetype = "dashed", size = 1.2, colour = "white")
}
p_trend
# 优化Nature图
p_nature <- ggplot(A, aes(x = stage, y = Freq, group = celltype)) +
stat_summary(geom = 'line', fun = 'mean', cex = 1, col = 'white') +
geom_area(aes(fill = celltype)) +
scale_fill_manual(values = cluster_cols) +
labs(x = NULL, y = NULL, title = "Nature Plot") +
theme_tufte() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "bottom",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(color = "black", size = 10)
)
for (stage in c("CS12", "CS13", "CS15", "CS17", "CS20")) {
p_nature <- p_nature + geom_vline(aes(xintercept = stage), linetype = "dashed", size = 1.2, colour = "white")
}
p_nature
在这一步中,我们使用了ggthemes包的Tufte主题,它是一个简洁且对比度较高的主题。我们还使用了ggtitle函数来给图表添加标题,并将图例放在图表底部以更好地利用空间。另外,我们添加了panel.grid.major和panel.grid.minor的element_blank()元素,以移除主要和次要的网格线。此外,我们使用了theme函数的hjust参数来水平居中标题,并使用了theme函数的legend.position参数将图例放在底部。
完整代码
# 安装和加载需要的包
if (!require(ggplot2)) {
install.packages("ggplot2")
}
if (!require(ggthemes)) {
install.packages("ggthemes")
}
library(ggplot2)
library(ggthemes)
# 定义数据
trend_data <- data.frame(Diameter = c(50, 100, 150, 200, 250),
expression = c(0.5, 0.6, 0.7, 0.6, 0.5),
group = c("A", "B", "C", "A", "B"))
nature_data <- data.frame(celltype = c("Type1", "Type2", "Type3", "Type1", "Type2"),
stage = c("CS12", "CS13", "CS15", "CS17", "CS20"),
Freq = c(0.2, 0.3, 0.5, 0.3, 0.2))
# 定义颜色
col <- c("#D57444", "#33B1C0", "#34993B", "#8C67AC")
cluster_cols <- c("#DC050C", "#FB8072", "#1965B0", "#7BAFDE", "#882E72",
"#B17BA6", "#FF7F00", "#FDB462", "#E7298A", "#E78AC3",
"#33A02C", "#B2DF8A", "#55A1B1", "#8DD3C7", "#A6761D",
"#E6AB02", "#7570B3", "#BEAED4", "#666666", "#999999",
"#aa8282", "#d4b7b7", "#8600bf", "#ba5ce3", "#808000",
"#aeae5c", "#1e90ff", "#00bfff", "#56ff0d", "#ffff00")
# 创建趋势气泡图
p_trend <- ggplot(trend_data, aes(x = Diameter, y = expression, fill = group)) +
geom_area(linewidth = 0.01, colour = "white") +
scale_fill_manual(values = col) +
labs(x = "Diameter", y = "Expression", title = "Trend Bubble Plot") +
theme_tufte() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "bottom",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(color = "black", size = 10),
axis.title = element_text(color = 'black', size = 12)
)
for (i in seq(100, 400, by = 100)) {
p_trend <- p_trend + geom_vline(aes(xintercept = i), linetype = "dashed", size = 1.2, colour = "white")
}
# 创建Nature图
p_nature <- ggplot(nature_data, aes(x = stage, y = Freq, group = celltype)) +
stat_summary(geom = 'line', fun = 'mean', cex = 1, col = 'white') +
geom_area(aes(fill = celltype)) +
scale_fill_manual(values = cluster_cols) +
labs(x = NULL, y = NULL, title = "Nature Plot") +
theme_tufte() +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = "bottom",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(color = "black", size = 10)
)
for (stage in c("CS12", "CS13", "CS15", "CS17", "CS20")) {
p_nature <- p_nature + geom_vline(aes(xintercept = stage), linetype = "dashed", size = 1.2, colour = "white")
}
# 显示图像
print(p_trend)
print(p_nature)
# 保存图像为PDF
ggsave(filename = "TrendBubblePlot.pdf", plot = p_trend, device = "pdf", width = 10, height = 7, units = "in")
ggsave(filename = "NaturePlot.pdf", plot = p_nature, device = "pdf", width = 10, height = 7, units = "in")
# 保存数据为CSV
write.csv(trend_data, file = "TrendData.csv", row.names = FALSE)
write.csv(nature_data, file = "NatureData.csv", row.names = FALSE)
阅读剩余
THE END