【问题标题】:ggplot2 geom_jitterdodge points and with overlayed dodged boxplots: I want to preserve color in points but force boxplots to be blackggplot2 geom_jitter 闪避点和重叠闪避箱线图:我想保留点的颜色,但强制箱线图为黑色
【发布时间】:2020-02-28 19:13:15
【问题描述】:

使用 ggplot2,我想 geom_jitterdodge 用重叠的闪避箱线图来计算一组点。诀窍是我希望箱线图是黑色的,而不是像点那样着色。点图如下所示:

制作箱线图很容易:

代码如下:




D_cohort1 %>%
    filter(!is.na(pssa_ela_code)) %>%
    ggplot(aes(x=timepoint,
               y=dibels_lnf,
               color=pssa_ela_code)) +
    geom_point(alpha=1/6, size=2,  width=1/3, height=0,
                  position=position_jitterdodge()) +
    geom_boxplot(fill=NA, outlier.shape=NA,
                 position=position_dodge2(padding=.3)) +
    facet_grid(rows=vars(school_type)) +
    guides(colour = guide_legend(override.aes = list(alpha=1))) +
    labs(title="Figure A.1: DIBELS LNF Scores at each Timepoint") +
    theme_cowplot() +
    theme(plot.background=element_rect(fill="aliceblue"),
          panel.border=element_rect(color="black", fill=NA),
          legend.position = c(.85,.87),
          legend.text = element_text(size = rel(.7)))

为了能见度,我希望箱线图是黑色的,但我不知道如何到达那里。我最接近的是这个(和以前一样,但调用 geom_boxplot():


D_cohort1 %>%
    filter(!is.na(pssa_ela_code)) %>%
    ggplot(aes(x=timepoint,
               y=dibels_lnf,
               color=pssa_ela_code)) +
    geom_point(alpha=1/6, size=2,  width=1/3, height=0,
                  position=position_jitterdodge()) +
    geom_boxplot(aes(color=NULL, group=fct_cross(timepoint, pssa_ela_code)),
                 fill=NA, outlier.shape=NA,
                 position=position_dodge2(padding=.3)) +
    facet_grid(rows=vars(school_type)) +
    guides(colour = guide_legend(override.aes = list(alpha=1))) +
    labs(title="Figure A.1: DIBELS LNF Scores at each Timepoint") +
    theme_cowplot() +
    theme(plot.background=element_rect(fill="aliceblue"),
          panel.border=element_rect(color="black", fill=NA),
          legend.position = c(.85,.87),
          legend.text = element_text(size = rel(.7)))

这得到了我想要的颜色效果,但箱线图的位置不正确。此处显示:

我怎样才能达到我想要的效果:正确定位,彩色点上的黑箱图?

【问题讨论】:

    标签: r ggplot2 plot boxplot


    【解决方案1】:

    好的。我睡在上面,今天早上能够想出一个解决方案。我想要的效果如下图。用于到达那里的代码是这样的:

    
    D_cohort1 %>%
        filter(!is.na(pssa_ela_code)) %>%
        ggplot(aes(x=timepoint,
                   y=dibels_lnf,
                   color=pssa_ela_code)) +
        geom_point(alpha=1/6, size=2,  width=1/3, height=0,
                      position=position_jitterdodge()) +
        geom_boxplot(aes(color=NULL, fill=pssa_ela_code),
                     outlier.shape=NA, alpha=0,
                     position=position_dodge2(padding=.3)) +
        facet_grid(rows=vars(school_type)) +
        guides(colour = guide_legend(override.aes = list(alpha=1))) +
        labs(title="Figure A.1: DIBELS LNF Scores at each Timepoint") +
        theme_cowplot() +
        theme(plot.background=element_rect(fill="aliceblue"),
              panel.border=element_rect(color="black", fill=NA),
              legend.position = c(.85,.87),
              legend.text = element_text(size = rel(.7)))
    
    

    除了调用 geom_boxplot() 之外,它和以前一样。它超越了颜色美学和设置填充。然后,alpha=0 使填充完全透明,这正是我想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多