具有facet_wrap的position_dodge2

时间:2019-01-04 17:28:53

标签: r ggplot2

我正在尝试创建一个函数来广义绘制交互比率。我只是有几个审美问题。

如果您在下图中的各个构面之间查看,您会注意到Pclass的列未正确对齐。您如何对齐它们?

too low rep :(

我知道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的问题

rep too low :(

1 个答案:

答案 0 :(得分:0)

这是complete中的tidyr通过用指定的值完成丢失的案例的真正帮助。在这种情况下,用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 package(v0.2.1)于2019-01-04创建