Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204742

ggplot2 geom_jitterdodge points and with overlayed dodged boxplots: I want to preserve color in points but force boxplots to be black

$
0
0

Using ggplot2, I want to geom_jitterdodge a swarm of points with overlayed dodged boxplots. The trick is that I want the boxplots to be black, not colored like the points. The point plot looks like this:

enter image description here

It's easy enough to get boxplots in place:

enter image description here

The code for that looks like this:




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)))

For visibilities sake, I want the boxplot lines to be black, but I can't quite figure out how to get there. Closest I've come is this (same as before but for the call to 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)))

That gets the color effect I want, but positions the boxplots incorrectly. Shown here:

enter image description here

How can I achieve the effect I want: correctly positioned, black boxplots over colored points?


Viewing all articles
Browse latest Browse all 204742

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>