将 "Overall" 组添加到 facet_wrap (ggplot2)
Adding "Overall" group to facet_wrap (ggplot2)
我的数据中有两组,A1 和 A2。每组有大约 50% 的男性和近似正态的年龄分布。我想按性别绘制每个组的年龄分布直方图,以及按性别划分的总体年龄分布(也可能是无性别的?)。
有没有办法用 facet_wrap 做到这一点?如果没有,有没有一种方法可以操纵我的数据(例如添加一个虚拟变量)并添加它?
假设你有:
library(tidyverse)
ggplot(iris,
aes(Sepal.Length, fill = Sepal.Width > 3)) +
geom_histogram() +
facet_wrap(~Species)
您可以操纵您的数据以包含 Species 始终为 "total." 的数据集的另一个副本,然后 geom_histogram
将使用对应于 "total."[=15 的方面的完整数据集=]
ggplot(iris %>%
bind_rows(iris %>% mutate(Species = "total")),
aes(Sepal.Length, fill = Sepal.Width > 3)) +
geom_histogram() +
# I want 'total' at the end
facet_wrap(~fct_relevel(Species, "total", after = Inf), nrow = 1)
我的数据中有两组,A1 和 A2。每组有大约 50% 的男性和近似正态的年龄分布。我想按性别绘制每个组的年龄分布直方图,以及按性别划分的总体年龄分布(也可能是无性别的?)。
有没有办法用 facet_wrap 做到这一点?如果没有,有没有一种方法可以操纵我的数据(例如添加一个虚拟变量)并添加它?
假设你有:
library(tidyverse)
ggplot(iris,
aes(Sepal.Length, fill = Sepal.Width > 3)) +
geom_histogram() +
facet_wrap(~Species)
您可以操纵您的数据以包含 Species 始终为 "total." 的数据集的另一个副本,然后 geom_histogram
将使用对应于 "total."[=15 的方面的完整数据集=]
ggplot(iris %>%
bind_rows(iris %>% mutate(Species = "total")),
aes(Sepal.Length, fill = Sepal.Width > 3)) +
geom_histogram() +
# I want 'total' at the end
facet_wrap(~fct_relevel(Species, "total", after = Inf), nrow = 1)