── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
adata <- readxl::read_excel(path = "data/41586_2023_5801_MOESM4_ESM.xlsx", sheet = 2, skip = 1)
adata <- adata |>
pivot_longer(-`Sucralose (uM)`) |>
rename(dose = name, conc = value) |>
mutate(
group = if_else(dose == 0, "Water", "Scrl") |> fct_relevel("Water", "Scrl"),
dose = as.character(dose)
) |>
select(dose, group, conc)
set.seed(seed = 24)
ggsave(filename = "./data/barplot.pdf", plot = p, height = 2,width = 2)
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
conversion failure on 'Scrl (μM)' in 'mbcsToSbcs': for μ (U+03BC)
set.seed(seed = 24)
p <- adata |>
ggplot(mapping = aes(x = dose, y = conc)) +
stat_summary(
geom = "errorbar",
fun.data = mean_se,
width = 0.3,
color = "#6083be"
) +
stat_summary(
geom = "bar",
fun.data = mean_se,
width = 0.5,
fill = "#6083be"
) +
geom_jitter(
mapping = aes(color = group, fill = group),
size = 3,
shape = 21,
width = 0.2
) +
labs(
x = bquote(Scrl ~ (mg ~ ml^-1)),
y = "Scrl (μM)",
title = "Plasma"
) +
scale_color_manual(values = c(Water = "#a8a8a8", Scrl = "#6083be")) +
scale_fill_manual(values = c(Water = "#ebebeb", Scrl = "#b8d9ea")) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 2.5)) +
coord_cartesian(clip = "off") +
catplot::theme_cat(
font_family = NULL,
aspect_ratio = 0.7,
x_text_angle = 45,
show_legend_title = FALSE,
legend_position_inside = c(0.25, 0.87)
)
p
- 1
-
设置随机数种子,以确保图中散点位置(如使用抖动)每次运行时保持一致
- 2
-
创建 ggplot 对象,使用数据集 adata,映射 x 轴为 dose,y 轴为 conc
Figure 1: Circulating Scrl levels in mice given water containing different Scrl concentrations for 2 weeks. n = 4 individual mice per condition.
这段代码是在使用 R 语言中的 ggplot2
包来绘制一个图形。看起来很复杂,但我会逐步解释各部分的作用,让你更容易理解。
1. 设置随机数种子
set.seed()
:这是用来设置随机数种子的函数。它确保代码中产生的随机数是可重复的。比如你每次运行代码得到的图形中,散点的分布是相同的。
seed = 24
:这个数字可以是任何值,它相当于随机数生成的“钥匙”,这样别人使用同样的种子运行代码时,结果是一样的。
2. 创建 ggplot 图形对象
p <- adata |>
ggplot(mapping = aes(x = dose, y = conc))
adata
:这是你的数据集。这个数据集包含了 dose
和 conc
这两列。
ggplot(mapping = aes(x = dose, y = conc))
:这是创建一个图形对象,并指定 x
轴和 y
轴的变量。dose
作为横坐标,conc
作为纵坐标。
3. 添加误差条
stat_summary(geom = "errorbar", fun.data = mean_se, width = 0.3, color = "#6083be")
stat_summary()
:用于在图形上添加统计摘要。
geom = "errorbar"
:表示要绘制误差条,展示不同数据组之间的误差范围。
fun.data = mean_se
:表示使用均值和标准误差来计算误差条。
color = "#6083be"
:设置误差条的颜色为蓝色。
4. 添加柱形图
stat_summary(geom = "bar", fun.data = mean_se, width = 0.5, fill = "#6083be")
geom = "bar"
:绘制柱形图。
fill = "#6083be"
:给柱形图填充颜色,同样是蓝色。
5. 添加散点
geom_jitter(mapping = aes(color = group, fill = group), size = 4, shape = 21, width = 0.2)
geom_jitter()
:用于在图中添加散点,并且散点会在 x
轴方向上随机偏移,使得点不重叠,方便观察。
mapping = aes(color = group, fill = group)
:使用 group
变量为散点设置颜色和填充色。
size = 4
:设置散点的大小。
shape = 21
:设置点的形状为实心圆,带边框。
width = 0.2
:表示散点在 x
方向上有一定的抖动,避免数据点完全重叠。
6. 添加标题和轴标签
labs(x = bquote(Scrl~(mg~ml^-1)), y = "Scrl (μM)", title = "Plasma")
labs()
:用于给图形添加标题和轴标签。
x = bquote(Scrl~(mg~ml^-1))
:设置 x
轴标签为 “Scrl (mg/ml)”,bquote()
可以帮助进行数学符号的格式化。
y = "Scrl (μM)"
:设置 y
轴标签为 “Scrl (μM)”。
title = "Plasma"
:给图形添加标题,设置为 “Plasma”。
7. 设置 y
轴范围和其他坐标系统
scale_y_continuous(expand = c(0,0), limits = c(0,2.5))
scale_y_continuous()
:设置 y
轴的范围。
expand = c(0,0)
:去除 y
轴的空隙,使图形从 0
开始。
limits = c(0, 2.5)
:设置 y
轴范围从 0
到 2.5
。
coord_cartesian(clip = "off")
coord_cartesian(clip = "off")
:用于设置图形范围,并且使得超出坐标范围的部分不被裁剪。
8. 设置 x
轴标签角度
guides(x = guide_axis(angle = 45))
guides(x = guide_axis(angle = 45))
:将 x
轴标签旋转 45 度,避免重叠,更加易读。
9. 设置主题
catplot::theme_cat(frame = "open", aspect_ratio = 0.8)
catplot::theme_cat()
:使用 catplot
包中的 theme_cat()
来设置图形的主题风格。
frame = "open"
:去掉图形框架。
aspect_ratio = 0.8
:设置宽高比例为 0.8,调整图形的形状。
10. 自定义颜色
scale_fill_manual(values = c(Water = "#ebebeb", Scrl = "#b8d9ea"))
scale_color_manual(values = c(Water = "#a8a8a8", Scrl = "#6083be"))
scale_fill_manual()
:手动设置填充颜色。
Water
:灰色 (#ebebeb
)。
Scrl
:浅蓝色 (#b8d9ea
)。
scale_color_manual()
:手动设置散点的边框颜色。
Water
:深灰色 (#a8a8a8
)。
Scrl
:深蓝色 (#6083be
)。
11. 设置图例
theme(legend.title = element_blank(), legend.position = "inside", legend.position.inside = c(0.3, 0.87))
theme()
:用于进一步定制图形的主题。
legend.title = element_blank()
:去掉图例的标题。
legend.position = "inside"
:把图例放置在图形的内部。
legend.position.inside = c(0.3, 0.87)
:图例的位置被放在 x = 0.3
,y = 0.87
的位置,即图形的左上方。
总结
这段代码从头到尾用 ggplot2
绘制了一个柱形图,同时加上了误差条和散点,使得数据更加丰富地呈现出来。此外,代码还设置了轴标签、图例、主题风格和各种美化细节,目的是让图形在传达数据信息的同时,也具有较好的视觉效果。
可以简洁地表述为:
- 柱状图(Bar Chart):显示各组的平均值或中位数,用于组间对比。
- 误差棒(Error Bars):表示数据的变异性,如标准误或标准差,反映结果的可靠性。
- 组内数据点(Within-group Data Points):各组样本的散点,展示组内数据分布和样本数。
features:
catplot包的theme_cat()函数可以设置主题风格为论文发表级风格,包括线宽为0.5 pt,字号为8 pt。