3
votes

I have grouped boxplots with ggplot

require(ggplot2)
require(tidyr)
require(lubridate)

dat.1415<-as.data.frame(sample(1:1000, 181))
dat.1415$date<-seq(as.Date("2014-11-1"), as.Date("2015-4-30"), "day")
names(dat.1415)<-c("value", "date")
dat.1415$month<-month(dat.1415$date)
dat.1415$season<-"2014/15"

dat.1516<-as.data.frame(sample(1:1000, 182))
dat.1516$date<-seq(as.Date("2015-11-1"), as.Date("2016-4-30"), "day")
names(dat.1516)<-c("value", "date")
dat.1516$month<-month(dat.1516$date)
dat.1516$season<-"2015/16"

dat.1617<-as.data.frame(sample(1:1000, 181))
dat.1617$date<-seq(as.Date("2016-11-1"), as.Date("2017-4-30"), "day")
names(dat.1617)<-c("value", "date")
dat.1617$month<-month(dat.1617$date)
dat.1617$season<-"2016/17"

dat.1718<-as.data.frame(sample(1:1000, 181))
dat.1718$date<-seq(as.Date("2017-11-1"), as.Date("2018-4-30"), "day")
names(dat.1718)<-c("value", "date")
dat.1718$month<-month(dat.1718$date)
dat.1718$season<-"2017/18"


dat<-rbind(dat.1415, dat.1516, dat.1617, dat.1718)
dat$month<-month.abb[dat$month]
dat$month<-factor(dat$month)
dat$facet = factor(dat$month, levels = c("Nov", "Dec", "Jan", "Feb", "Mar", "Apr"))

ggplot(dat, aes(x=season, y=value)) + 
    geom_boxplot(fill="grey50") + 
    facet_grid(~facet) + 
    theme_classic()+    
    theme(legend.position="top") +
    labs(x="", y="", title="") +
    guides(fill=F) +
    theme(panel.background = element_rect(fill="grey95"))

Grouped boxplots

But because it's so many boxes, I get overlapping labels on the x-axis. Is there a way I can make them alternating between the different facets? I don't want the position of the x-axis to alternate but the actual labels, say in facet one it's "2014/15" and "2016/17", in facet 2 it's "2015/16" and "2017/18" and so on. Is that possible?

2
Have you considered flipping the facets so there's only one x-axis? facet_grid(facet ~ .)Jordo82
It's a rather unusual request, but if there has been a similar question in hte past, then a ggplot2 based answer would probably have involved the use of guide-type functions.IRTFM
I'd take a stab at it if you post a reproducible example. That includes a representative but minimal sample of data, and a minimal amount of code (i.e. no cleanup, just what's needed to recreate the issue)camille
@Jordo82: Yes, that would be an option, but I just tried it and feel like it makes the graph less intuitive to read.Anke
@42 Not sure what guide-type functions are, but will look into this. Thank youAnke

2 Answers

5
votes

try to rotate you labels to have full information

+ theme(axis.text.x = element_text(angle = 30, hjust = 1)) 

Edits

Or try to manipulate your data somehow and use something like

+ scale_x_discrete(breaks=c("1","3"), labels=c(...))

Edits2: I set the color to 0 for the ones to skip.

ggplot(dat, aes(x=season, y=value)) + 
  geom_boxplot(fill="grey50") + 
  facet_grid(~facet) + 
  theme_classic()+    
  theme(legend.position="top") +
  labs(x="", y="", title="") +
  guides(fill=F) +
  theme(panel.background = element_rect(fill="grey95"))+ 
  theme(axis.text.x = element_text(color=c(1,0,1,0))) 

enter image description here

2
votes

The new version of ggplot v3.3.0 has added the ability to use guide_axis to dodge the labels.

scale_x_discrete(guide = guide_axis(n.dodge = 2))

So adding that to your plot:

ggplot(dat, aes(x=season, y=value)) + 
    geom_boxplot(fill="grey50") + 
    facet_grid(~facet) + 
    theme_classic()+    
    theme(legend.position="top") +
    labs(x="", y="", title="") +
    guides(fill=F) +
    theme(panel.background = element_rect(fill="grey95")) + 
    scale_x_discrete(guide = guide_axis(n.dodge = 2))

produces the following:

enter image description here