2

Apologies in advance, this is my first time posting on SO. I'm trying to create box plots for a series of data. Using a method I learned on SO, I melted the data such that it appears this way:

> head(d.m.ea)
    group variable       value
465  Test  Cascade 66.31848535
466  Test  Cascade 78.82857936
467  Test  Cascade 141.1745514
468  Test  Cascade 97.49170334
469  Test  Cascade 101.5525275
470  Test  Cascade  106.945077

When I run the following code, I get a series of box plots, each separated by their "group" variable (test or control). However the whiskers are just vertical segments; they don't include the typical horizontal tic marks I've seen in box plots.

ea <- ggplot(d.m.ea, aes(x=as.factor(variable),y=as.numeric(value))) + 
    geom_boxplot(fill=group) +
    theme(axis.text.x = element_text(angle=45, hjust=1))

How can I add the horizontal marks to the whiskers using ggplot2? I've tried using ggplotly and that creates separate tic marks for each "group" but makes the boxes overlap entirely. If I add error bars manually they show up with horizontal tic marks but only in between the boxes; the boxes themselves maintain the vertical whiskers, leaving me with three vertical lines and two very wide horizontal lines for each pair of box plots. Is there any way to display the horizontal marks on the box plots I've split using fill=group, or am I stuck with just the vertical whiskers?

  • 3
    try adding stat_boxplot(geom ='errorbar')? – Ben Aug 14 '19 at 18:42
  • @Ben Just tried this. It works if I don't include the ```geom_boxplot``` call but only results in the whiskers, not the box itself. Will I have to manually create the boxes within ```stat_boxplot```? – Liam Bristol Aug 14 '19 at 19:00

1 Answers1

2

Try this:

ea <- ggplot(d.m.ea, aes(x=as.factor(variable),y=as.numeric(value),fill=group)) + 
  geom_boxplot() +
  stat_boxplot(geom ='errorbar') +
  theme(axis.text.x = element_text(angle=45, hjust=1))
Ben
  • 28,684
  • 5
  • 23
  • 45