r - position_dodge2 与 facet_wrap
问题描述
我正在尝试创建一个函数来概括绘图交互比率。我只是有几个审美问题。
如果您查看下图中的各个方面,您会注意到 Pclass 的列没有正确对齐。你如何对齐它们?
我知道position_dodge2(preserve = "single")
保留列宽但不保留列的位置,我该如何更改?
该功能还有其他建议吗?
该数据来自 Titanic Kaggle 数据集。
df <- structure(list(Pclass = structure(c(3L, 1L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 2L), .Label = c("1", "2", "3"), class = "factor"), Survived = structure(c(1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("0", "1"), class = "factor"), Parch = c(0, 0, 0, 0, 0, 0, 0, 1, 2, 0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
x1 <- "Parch"
x2 <- "Pclass"
y <- "Survived"
x1 <- sym(x1)
x2 <- sym(x2)
y <- sym(y)
df %>%
select(!!x1, !!x2, !!y) %>%
group_by(!!x1, !!x2, !!y) %>%
tally() %>%
mutate(perc = n / sum(n)) %>%
{
if(sapply(select(df, !!x1), class) == "numeric") {
ggplot(., aes(x = !!x1, y = perc, fill = !!x2))
} else
ggplot(., aes(x = factor(!!x1), y = perc, fill = !!x2, group = !!x2))
} +
geom_col(position = position_dodge2(preserve = "single")) +
facet_grid(vars(!!y)) +
{
if(sapply(select(df, !!x2), class) == "numeric") {
scale_fill_gradient2(low = "blue", high = "red",
midpoint = 25)
}
} +
scale_y_continuous(labels = percent_format(1)) +
scale_x_continuous(breaks = 0:nrow(distinct(select(df, !!x1)))) +
labs(x = x1, y = "Percentage") + theme_bw()
编辑:
添加代码以使其可重现。
替换position_dodge2
为position_dodge
下面的问题
解决方案
这是complete
fromtidyr
真正有帮助的地方,它可以用指定的值完成缺失的案例。在这种情况下,用 n = 0 填充缺失的组合 position_dodge
也是可行的方法,position_dodge2
没有必要。
library(tidyverse)
library(scales)
#>
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#>
#> discard
#> The following object is masked from 'package:readr':
#>
#> col_factor
df <- structure(list(Pclass = structure(c(3L, 1L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 2L), .Label = c("1", "2", "3"), class = "factor"), Survived = structure(c(1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("0", "1"), class = "factor"), Parch = c(0, 0, 0, 0, 0, 0, 0, 1, 2, 0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
x1 <- "Parch"
x2 <- "Pclass"
y <- "Survived"
x1 <- sym(x1)
x2 <- sym(x2)
y <- sym(y)
df %>%
select(!!x1, !!x2, !!y) %>%
group_by(!!x1, !!x2, !!y) %>%
tally() %>%
complete(Pclass, Survived, fill = list(n = 0)) %>%
mutate(perc = n / sum(n)) %>%
{
if(sapply(select(df, !!x1), class) == "numeric") {
ggplot(., aes(x = !!x1, y = perc, fill = !!x2))
} else
ggplot(., aes(x = factor(!!x1), y = perc, fill = !!x2, group = !!x2))
} +
geom_col(position = position_dodge()) +
facet_grid(vars(!!y)) +
{
if(sapply(select(df, !!x2), class) == "numeric") {
scale_fill_gradient2(low = "blue", high = "red",
midpoint = 25)
}
} +
scale_y_continuous(labels = percent_format(1)) +
scale_x_continuous(breaks = 0:nrow(distinct(select(df, !!x1)))) +
labs(x = x1, y = "Percentage") + theme_bw()
#> Warning: Removed 8 rows containing missing values (geom_col).
由reprex 包(v0.2.1)于 2019 年 1 月 4 日创建
推荐阅读
- python-3.x - 重新计算 XML 子元素
- c# - 使用 C# 获取具有相同标签名称的所有标签,无论 XML 文件中的级别如何
- rust - Rust“工具链”到底是什么?
- spring-boot - 使用可选和映射在 java 8 中获取和设置值
- php - 删除自定义帖子类型中的 H1 也会删除导航项
- csv - 根据“ID”读取 csv 文件的特定行
- react-datepicker - react-datepicker 为什么突出显示月份的同一天?
- python - 尝试定位满足条件的索引时出现索引错误
- excel - Excel - 如何计算表格中特定日期的不同文本的数量?
- java - 如何在 JTable 中使用 JFileChooser 选择文件并在 JTable 中显示数据