【问题标题】:Plot all permutations of two discontinuous ranges绘制两个不连续范围的所有排列
【发布时间】:2020-11-17 13:47:29
【问题描述】:

我想画出掷两个非常规六面骰子的所有可能结果:

die_1 <- c(0, 1, 2, 3, 6, 6)
die_2 <- c(0, 0, 0, 6, 6, 6)

每个排列的骰子掷得更高的颜色。

结果如下所示:

到目前为止,我已经尝试过:

library(tidyverse)
expand_grid(die_1, die_2) %>% 
  mutate(winner = if_else(die_1 > die_2, "die_1", if_else(die_1 == die_2, "draw", "die_2"))) %>%
  ggplot(aes(x = die_1, y = die_2, fill = winner)) +
  geom_point(aes(colour = winner), size = 10, shape = "square") +
  scale_discrete_manual(aesthetics = "colour", values = c("orange", "blue", "grey")) +
  scale_y_reverse(breaks = c(0:6)) +
  scale_x_continuous(position = "top", breaks = c(0:6)) 

imgur 我的 ggplot2 版本

所以我试图将 permutations 放在 x 和 y 轴上,但 ggplot2 绘制 结果,这会产生过度绘图。如果我不解释自己,请比较图片。

有谁知道如何在 ggplot2 中实现这一点?

【问题讨论】:

    标签: r ggplot2 permutation dice


    【解决方案1】:

    一种解决方案是添加两个辅助列,而不是骰子的结果,而是哪一面朝上。然后,您可以在轴上绘制卷边并将标签更改为结果。如果您想更接近原始情节的美感,使用geom_tile 可能会更容易。可能有更好的方法可以做到这一点,但这在这里有效:

    die_1 <- c(0, 1, 2, 3, 6, 6)
    die_2 <- c(0, 0, 0, 6, 6, 6)
    
    expand_grid(die_1, die_2) %>% 
      mutate(die_1_side = c(rep(1,6),rep(2,6),rep(3,6),rep(4,6),rep(5,6), rep(6,6)),
             die_2_side = c(rep(c(1,2,3,4,5,6), 6) )) %>%
      mutate(winner = if_else(die_1 > die_2, "die_1", if_else(die_1 == die_2, "draw", "die_2"))) %>%
      ggplot(aes(x = die_1_side, y = die_2_side, fill = winner)) +
      geom_tile(aes(fill = winner), color = "white", size = 1) +
      scale_discrete_manual(aesthetics = "fill", values = c("orange", "blue", "grey")) +
      scale_y_reverse(breaks = c(1:6), labels = c(0,0,0,6,6,6)) +
      scale_x_continuous(position = "top", breaks = c(1:6), labels = c(0,1,2,3,6,6))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2021-12-09
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多