如何使用 ggplot2 绘制与排序点叠加的箱线图
How to plot boxplots superimposed with sorted points using ggplot2
使用 ggplot2,我可以绘制一个叠加了点的箱线图。但是这些点位于一条垂直线上。
library(ggplot2)
example_data <- data.frame(cohort = c("ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC"),
sample = c("A5LI", "A5JQ", "A5JP", "A5LE", "A5LG", "A5JV", "A5JD", "A5J8", "A5K8", "A5L3", "AA33", "AA30", "AA2T", "A95A", "AAZT", "A8I3", "AAV9", "A8Y4", "A8Y8", "AA31", "AAAT", "A9U4", "A7Q1", "A7DS", "A9TV", "A4D5", "A9TY", "A7CX", "A9TW", "A86F"),
count = c(50, 5, 65, 22, 18, 25, 27, 86, 24, 20, 48, 96, 60, 27, 81, 34, 43, 58, 31, 77, 160, 31, 157, 104, 84, 53, 153, 111, 278, 105))
ggplot(example_data, aes(cohort, count)) +
geom_boxplot(aes(color = cohort)) +
geom_point(aes(color = cohort)) +
scale_y_log10() +
labs(x = NULL) +
theme(axis.line.x = element_blank(), axis.ticks.x = element_blank(),
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 0.5), legend.position = 'none')
我怎样才能像这个图一样根据它们的 y 值(example_data 中的“计数”大小)重新排序这些点?
如果您查看您显示的所需输出的示例图并考虑比例,基本上有两个不同的层:
总体:x 轴作为某个类别(“DKFZ”、“Sanger”、“SMuFin”...),y 轴是用于箱线图的某个值。
在每个箱线图中:x 轴是其他一些连续值,y 轴与箱线图中的 y 轴使用的值相同。
这意味着 每个箱线图 的 x 轴与整个图的 x 轴不同。你有点想要一个“辅助 x 轴”。抛开所有关于这是否是个好主意的评论,我可以在 ggplot2
.
中向您展示如何做到这一点的方法
辅助 x 轴不是具有 ggplot2
的 built-in 特征;但是,由于您想要的轴之一是 categorical/discrete(example_data$cohort
),而另一个轴是连续的(example_data$count
),我们可以通过一些巧妙的刻面格式来模拟两个 x 轴的这种效果.
一般的想法是,我们根据 cohort
将您的情节分成多个方面,然后在每个情节中我们显示一个整体箱线图(按 cohort
分组)和每个方面的绘图点.这意味着我们的 x 轴值是 count
以及 y 轴值 - 我假设在您的真实数据中轴值不会相同,但它适用于示例目的。然后,我们可以使用一些 theme
元素和有关分面标签的选项(在 ggplot2
中称为 strip.text
元素)来模拟相同的外观。我也切换到默认使用 theme_classic()
,否则你必须处理在最终情节中没有意义的 x 网格线。如果您想要垂直线,则必须根据您的数据手动或以编程方式放置它们。
通常,刻面是分开的,但我通过 panel.spacing.x
将它们推到一起。
比较图side-by-side很有用,所以请注意,我在这里使用cowplot::plot_grid()
排列新旧图以供演示。
一个非常重要的注意事项是我将 outlier.shape = NA
添加到 geom_boxplot()
的调用中。这很重要,因为默认情况下,任何异常值都将通过 geom_boxplot()
命令显示为点,并且它们将位于“不正确”的 x 位置。由于我们已经为所有这些点处理了所需的位置,因此有必要像这样删除它们。
p <- # your code you shared + labs(title="Old Plot")
p1 <-
ggplot(example_data, aes(count, count)) +
geom_boxplot(aes(color=cohort), outlier.shape = NA) +
geom_point(aes(color=cohort)) +
facet_wrap(~cohort, scales='free_x', strip.position = 'bottom') +
scale_y_log10() +
labs(title='New Plot', x=NULL) +
theme_classic() +
theme(
panel.spacing.x = unit(0,'pt'),
axis.text.x = element_blank(),
strip.placement = 'outside',
strip.background = element_blank(),
axis.ticks.x = element_blank()
)
library(cowplot)
plot_grid(p, p1)
使用 ggplot2,我可以绘制一个叠加了点的箱线图。但是这些点位于一条垂直线上。
library(ggplot2)
example_data <- data.frame(cohort = c("ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "ACC", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "CHOL", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC", "DLBC"),
sample = c("A5LI", "A5JQ", "A5JP", "A5LE", "A5LG", "A5JV", "A5JD", "A5J8", "A5K8", "A5L3", "AA33", "AA30", "AA2T", "A95A", "AAZT", "A8I3", "AAV9", "A8Y4", "A8Y8", "AA31", "AAAT", "A9U4", "A7Q1", "A7DS", "A9TV", "A4D5", "A9TY", "A7CX", "A9TW", "A86F"),
count = c(50, 5, 65, 22, 18, 25, 27, 86, 24, 20, 48, 96, 60, 27, 81, 34, 43, 58, 31, 77, 160, 31, 157, 104, 84, 53, 153, 111, 278, 105))
ggplot(example_data, aes(cohort, count)) +
geom_boxplot(aes(color = cohort)) +
geom_point(aes(color = cohort)) +
scale_y_log10() +
labs(x = NULL) +
theme(axis.line.x = element_blank(), axis.ticks.x = element_blank(),
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 0.5), legend.position = 'none')
我怎样才能像这个图一样根据它们的 y 值(example_data 中的“计数”大小)重新排序这些点?
如果您查看您显示的所需输出的示例图并考虑比例,基本上有两个不同的层:
总体:x 轴作为某个类别(“DKFZ”、“Sanger”、“SMuFin”...),y 轴是用于箱线图的某个值。
在每个箱线图中:x 轴是其他一些连续值,y 轴与箱线图中的 y 轴使用的值相同。
这意味着 每个箱线图 的 x 轴与整个图的 x 轴不同。你有点想要一个“辅助 x 轴”。抛开所有关于这是否是个好主意的评论,我可以在 ggplot2
.
辅助 x 轴不是具有 ggplot2
的 built-in 特征;但是,由于您想要的轴之一是 categorical/discrete(example_data$cohort
),而另一个轴是连续的(example_data$count
),我们可以通过一些巧妙的刻面格式来模拟两个 x 轴的这种效果.
一般的想法是,我们根据 cohort
将您的情节分成多个方面,然后在每个情节中我们显示一个整体箱线图(按 cohort
分组)和每个方面的绘图点.这意味着我们的 x 轴值是 count
以及 y 轴值 - 我假设在您的真实数据中轴值不会相同,但它适用于示例目的。然后,我们可以使用一些 theme
元素和有关分面标签的选项(在 ggplot2
中称为 strip.text
元素)来模拟相同的外观。我也切换到默认使用 theme_classic()
,否则你必须处理在最终情节中没有意义的 x 网格线。如果您想要垂直线,则必须根据您的数据手动或以编程方式放置它们。
通常,刻面是分开的,但我通过 panel.spacing.x
将它们推到一起。
比较图side-by-side很有用,所以请注意,我在这里使用cowplot::plot_grid()
排列新旧图以供演示。
一个非常重要的注意事项是我将 outlier.shape = NA
添加到 geom_boxplot()
的调用中。这很重要,因为默认情况下,任何异常值都将通过 geom_boxplot()
命令显示为点,并且它们将位于“不正确”的 x 位置。由于我们已经为所有这些点处理了所需的位置,因此有必要像这样删除它们。
p <- # your code you shared + labs(title="Old Plot")
p1 <-
ggplot(example_data, aes(count, count)) +
geom_boxplot(aes(color=cohort), outlier.shape = NA) +
geom_point(aes(color=cohort)) +
facet_wrap(~cohort, scales='free_x', strip.position = 'bottom') +
scale_y_log10() +
labs(title='New Plot', x=NULL) +
theme_classic() +
theme(
panel.spacing.x = unit(0,'pt'),
axis.text.x = element_blank(),
strip.placement = 'outside',
strip.background = element_blank(),
axis.ticks.x = element_blank()
)
library(cowplot)
plot_grid(p, p1)