当零假设为真时,我试图显示治疗效果的抽样分布,并且我希望随机分布点颜色。现在,颜色组堆叠在一起。我不确定用viridis的这10种颜色看起来会不会很好,但我很好奇有没有办法让圆点颜色看起来像我们倒掉了一罐软糖豆。

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")发布于 2020-03-31 02:46:57
来自@ed_hagen on 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")https://stackoverflow.com/questions/60895771
复制相似问题