《R语言医学数据分析实战》学习记录|第四章 数据可视化
第四章 数据可视化
内容记录
- R的基础绘图系统
- 高水平作图函数直接产生图形如
plot()、hist()、boxplot()等 - 低水平作图函数则在高水平做的图上添加新的图形或元素,如
points()、lines()、title()等。 - 常规绘图:
- 绘图
plot(x轴data, y轴data, type=类型),直方图hist(一维data),条形图barplot(data(数据), col(颜色), xlab, ylab, las=1纵轴刻度标签变横向),饼图pie(),箱线图boxplot(data, xlab, ylab, las),小提琴图vioplot(); ~连接变量,符合左边看成因变量,符号右边看成是自变量;- 导出图形
pdf()``png()``jpeg()``tiff()
- 绘图
- ggplot2绘图:
- ggplot函数
将数学空间映射到图形元素空间。 aes()指定美学aesthetics元素geom_函数用于指定图形元素,包括点线面多边形等。散点拟合曲线geom_smooth();散点图geom_point();比例构成条形图geom_bar(position=stack绝对值叠加/fill百分比堆积/dodge并排)scale_标度函数是图形细节的调节函数,用于控制图形的颜色、点的大小和形状等。scale_color_manual()facetk_分面函数,实现分组绘图功能,将整个数据按照一个或几个分类变量分成多个子集分别作图。facet_grid()theme_主题函数用于定义绘图的风格ggsave()保存图形..(name)..变量- 形如
..(name)..的变量是“在统计变换(stat)阶段产生的计算变量(computed/statistic variables)”。它们不是数据框里的普通列,而是用来表示某次统计计算的中间结果(如计数、密度等)。使用双点包裹是为了避免与数据中的真实列名冲突。 - 常见用法和注意点:
- 常见的计算变量名有:
..count..、..density..、..ndensity..等。 - 旧的写法(在
aes()里直接用..count..或..density..)仍能工作,但现在更推荐使用后续语法: 用after_stat()参考计算结果,例如aes(y = after_stat(density))、aes(y = after_stat(count))。 - 这些变量只在该图层的统计变换中有效,不能直接作为数据框的普通列在其他图层之间共享。
- 如果想要跨层比较或保存计算结果,最好在数据框层面计算好新的列并使用这些列。
- 常见的计算变量名有:
- 形如
- 其他图形
- 热图
stats::heatmap()
# 基础绘图
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type = "b", lty = 1, pch = 15)
lines(dose, drugB, type = "b", lty = 2, pch = 17)
legend("topleft", title = "Drug Type",
legend = c("A", "B"),
lty = c(1, 2),
pch = c(15, 17))
# ggplot2绘图
library(ggplot2)
mtcars$am <- factor(mtcars$am)
# 根据am属性区分图形颜色color
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg, color = am)) + geom_point()
# 根据am属性区分图形形状shape
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg, shape = am)) + geom_point()
# 根据am属性分别绘制拟合曲线
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg, shape = am)) + geom_smooth()
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg, shape = am)) + geom_smooth(method = "lm")
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
geom_point(aes(color = am)) +
scale_color_manual(values = c("red", "yellow"))+
stat_smooth()
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
geom_point() +
stat_smooth() +
facet_grid(~am)
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
geom_point(aes(color = am)) +
stat_smooth() +
theme_bw()
data(anorexia, package = "MASS")
anorexia$wt.change <- anorexia$Postwt - anorexia$Prewt
# 设定y轴为频率(密度),统计变换的中间结果。..name..的语法更新为after_stat(name)
# binwidth设置组间距,fill设置填充色,color设置边框颜色。
ggplot(anorexia, aes(x = wt.change,y = after_stat(density))) +
geom_histogram(binwidth = 2, fill = "skyblue", color = "black") +
stat_density(geom = "line", linetype = "dashed", size = 1) +
labs(x = "Weight change(lbs)") +
theme_bw()
# 比较不同治疗方式下的体重change分布
ggplot(anorexia, aes(x = wt.change, color = Treat, linetype = Treat)) +
stat_density(geom = "line", size = 1) +
labs(x = "Weight change(lbs)") +
theme_bw()
# 用箱线图比较不同治疗方式下的体重change分布
ggplot(anorexia, aes(x = Treat, y = wt.change, fill = Treat)) +
geom_boxplot() +
theme_bw()
# ggpubr添加组间比较的统计学差异
library(ggpubr)
my_comparisons <- list(c("CBT", "Cont"),c("CBT", "FT"),c("Cont", "FT"))
# 组间均值比较的统计学差异,t检验
ggplot(anorexia, aes(x = Treat, y = wt.change, fill = Treat)) +
geom_boxplot() +
stat_compare_means(comparisons = my_comparisons, method = "t.test", color = "blue") +
theme_bw()
# 小提琴图
ggplot(anorexia, aes(x = Treat, y = wt.change, fill = Treat)) +
geom_violin() +
geom_point(position = position_jitterdodge(0.1), alpha = .5) +
theme_bw()
习题
4-1 绘制散点图
请加载 datasets 包里的数据集 women,绘制其中的变量 height 和 weight 的散点图和线图,并尝试改变图中点的特征、颜色、线的类型等。
# 常规作图
data(women)
plot(height ~ weight, data = women)
plot(height ~ weight, data = women,type = "b", lty = 1, pch = 17)
library(ggplot2)
ggplot(women, aes(x = weight, y = height)) +
geom_point(color = "red", size = 2, shape = 2) +
theme_bw()
ggplot(women, aes(x = weight, y = height)) +
geom_smooth(color = "blue", size = 2, linetype = 3)
4-2
请加载 datasets 包里的数据集 iris,使用适当的图形展示 3 个品种鸢尾花的 4 种几何尺寸的分布。
data("iris")
boxplot(Sepal.Length ~ Species, data = iris)
boxplot(Sepal.Width ~ Species, data = iris)
boxplot(Petal.Length ~ Species, data = iris)
boxplot(Petal.Width ~ Species, data = iris)
library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot()
ggplot(iris, aes(x = Species, y = Sepal.Width)) + geom_boxplot()
ggplot(iris, aes(x = Species, y = Petal.Length)) + geom_boxplot()
ggplot(iris, aes(x = Species, y = Petal.Width)) + geom_boxplot()
4-3
请将上面习题 4-1 作出的图形分别保存为 pdf 和 png 格式的文件。
data(women)
library(ggplot2)
ggplot(data = women, aes(x = weight, y = height)) + geom_point()
ggsave("women_data.pdf")
ggsave("women_data.png")
4-4
请为图4-19添加组间均值比较的统计学差异的标记。
library(ggplot2)
library(ggpubr)
data(anorexia, package = "MASS")
anorexia$wt.change <- anorexia$Postwt - anorexia$Prewt
my_comparisons <- list(c("CBT", "Cont"),c("CBT", "FT"),c("Cont", "FT"))
ggplot(anorexia, aes(x = Treat, y = wt.change, fill = Treat)) +
geom_violin() +
geom_point(position = position_jitterdodge(0.1), alpha = .5) +
stat_compare_means(comparisons = my_comparisons, method = "t.test")+
theme_bw()

浙公网安备 33010602011771号