R语言制作克利夫兰点阵图

# install.packages("gcookbook")
# 加载 gcookbook 包(其中包含数据集 cabbage_exp)
library(gcookbook) # Load gcookbook for the cabbage_exp data set
library(ggplot2)
# 基于 tophit 数据绘图:
# - 横轴:avg
# - 纵轴:对 name 按 avg 进行重排(让显示顺序与 avg 大小一致)
ggplot(tophit, aes(x = avg, y = reorder(name, avg))) +
# 用散点图表示每个观测值
# size = 3:增大点的大小,便于观察
geom_point(size = 3) +
# 使用黑白主题(更简洁、对比度更强)
theme_bw() +
# 自定义网格线样式
theme(
# 去掉横轴方向的主要网格线
panel.grid.major.x = element_blank(),
# 去掉横轴方向的次要网格线
panel.grid.minor.x = element_blank(),
# 保留纵轴方向的主要网格线,并设置为灰色虚线
panel.grid.major.y = element_line(colour = "grey60", linetype = "dashed")
)
# install.packages("gcookbook")
# 加载 gcookbook 包(其中包含数据集 cabbage_exp)
library(gcookbook) # Load gcookbook for the cabbage_exp data set
library(ggplot2)
# 绘制“平均值 avg”与分类变量 name 之间的关系图,并按 lg 分面显示
ggplot(tophit, aes(x = avg, y = reorder(name, avg))) +
# 从 x=0 到 x=avg 画水平线段(y 坐标在对应的 name 上)
# aes(yend = name):线段的终点 y 仍为该 name(因此是水平线)
# xend = 0:线段从 0 开始
# colour = "grey50":线段统一为灰色
geom_segment(aes(yend = name), xend = 0, colour = "grey50") +
# 在每个 name 位置叠加散点
# size = 3:点变大
# aes(colour = lg):点颜色由 lg 决定
geom_point(size = 3, aes(colour = lg)) +
# 使用 Set1 调色板,并限制颜色映射的取值范围为 NL 和 AL
# guide = FALSE:不显示颜色图例(不生成 legend)
scale_colour_brewer(palette = "Set1", limits = c("NL", "AL"), guide = FALSE) +
# 使用黑白主题
theme_bw() +
# 去掉横向(y 方向)的主要网格线
theme(panel.grid.major.y = element_blank()) +
# 按 lg 进行分面:
# - 行维度:lg ~ . (lg 分面到列或行,具体取决于 facet 的默认布局)
# - scales = "free_y":每个分面 y 轴范围可不同(适合 name 个数/顺序变化)
# - space = "free_y":每个分面的绘图区域大小随 y 的内容自适应
facet_grid(lg ~ ., scales = "free_y", space = "free_y")

评论

发表评论

了解 数据控|突破是我们的每一步 的更多信息

立即订阅以继续阅读并访问完整档案。

继续阅读