简体   繁体   English

geom_dotplot 颜色的随机模式

[英]random pattern for geom_dotplot color

I'm trying to show a sampling distribution of treatment effects when the null hypothesis is true, and I'd like to have the dot color randomly distributed.当零假设为真时,我试图显示处理效果的抽样分布,并且我希望点颜色随机分布。 Right now the color groups are stacked.现在颜色组是堆叠的。 I'm not sure if it will look good with these 10 colors from viridis, but I'm curious to see if there is a way to make the dot colors look like we dumped out a jar full of jelly beans.我不确定这 10 种来自 viridis 的颜色是否看起来不错,但我很想知道是否有办法让点颜色看起来像我们倒掉了一个装满果冻豆的罐子。

在此处输入图片说明

library(tidyverse)
library(infer)

set.seed(1)

rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }

hap <- data.frame(trt = c(rep(0, 248), rep(1, 245)),
                  bdi3 = c(rnorm2(248, 27.52, 13.26),
                           rnorm2(245, 19.99, 15.70))
                  )
)

hap %>%
  specify(bdi3 ~ trt) %>%
  hypothesize(null = "independence") %>% 
  generate(reps = 10000, type = "permute") %>% 
  calculate(stat = "slope") %>%
  mutate(color=factor(sample(1:10, 10000, replace=TRUE))) %>%
  ggplot(., aes(x=stat, fill=color)) +
  scale_color_viridis_d() +
  geom_dotplot(method = 'dotdensity', binwidth = .04,
               color="white", alpha=1,
               binpositions="all", stackgroups=TRUE,
               stackdir = "up") +
  scale_y_continuous(NULL, breaks = NULL) +
  theme_minimal() +
  theme(legend.position = "none")

The answer from @ed_hagen on Twitter :@ed_hagen 在 Twitter 上的回答:

在此处输入图片说明

library(tidyverse)
library(infer)

set.seed(1)

rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }

hap <- data.frame(trt = factor(c(rep(0, 248), rep(1, 245))),
                  bdi3 = c(rnorm2(248, 27.52, 13.26),
                           rnorm2(245, 19.99, 15.70))
                  )

hap %>%
  specify(bdi3 ~ trt) %>%
  hypothesize(null = "independence") %>% 
  generate(reps = 10000, type = "permute") %>% 
  calculate(stat = "diff in means", 
            order = c("0", "1")) %>%
  mutate(color=factor(sample(1:2, 10000, replace=TRUE)),
         group=1:10000) %>%
  ggplot(., aes(x=stat, fill=color, group=group)) +
  scale_fill_manual(values=c("#1f9ac9", "grey")) +
  geom_dotplot(method = 'dotdensity', binwidth = .035,
               color="white", alpha=1,
               binpositions="all", stackgroups=TRUE,
               stackdir = "up") +
  scale_y_continuous(NULL, breaks = NULL) +
  theme_minimal() +
  theme(legend.position = "none")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM