首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >geom_dotplot颜色的随机图案

geom_dotplot颜色的随机图案
EN

Stack Overflow用户
提问于 2020-03-28 08:09:34
回答 1查看 71关注 0票数 1

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

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-31 02:46:57

来自@ed_hagen on Twitter的答案

代码语言:javascript
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60895771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档