首页 > 解决方案 > 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_dodge2position_dodge下面的问题

代表太低:(

标签: rggplot2

解决方案


这是completefromtidyr真正有帮助的地方,它可以用指定的值完成缺失的案例。在这种情况下,用 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 日创建


推荐阅读