Home > other >  How can I make a discontinuous axis in R with ggplot2?
How can I make a discontinuous axis in R with ggplot2?

Time:10-12

I have a dataframe (dat) with two columns 1) Month and 2) Value. I would like to highlight that the x-axis is not continuous in my boxplot by interrupting the x-axis with two angled lines on the x-axis that are empty between the angled lines.

Example Data and Boxplot

library(ggplot2)
set.seed(321)
dat <- data.frame(matrix(ncol = 2, nrow = 18))
x <- c("Month", "Value")
colnames(dat) <- x
dat$Month <- rep(c(1,2,3,10,11,12),3)
dat$Value <- rnorm(18,20,2)

ggplot(data = dat, aes(x = factor(Month), y = Value))  
  geom_boxplot()  
  labs(x = "Month")  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black"),
        axis.text.y = element_text(size = 14, color = "black"))

The ideal figure would look something like below. How can I make this discontinuous axis in ggplot?

enter image description here

CodePudding user response:

I think it is possible to get what you want. It may take some work.

Here is your graph:

library(ggplot2)
set.seed(321)
dat <- data.frame(matrix(ncol = 2, nrow = 18))
x <- c("Month", "Value")
colnames(dat) <- x
dat$Month <- rep(c(1,2,3,10,11,12),3)
dat$Value <- rnorm(18,20,2)

p <- ggplot(data = dat, aes(x = factor(Month), y = Value))  
  geom_boxplot()  
  labs(x = "Month")  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black"),
        axis.text.y = element_text(size = 14, color = "black"))

Here is my effort:

p   annotate("segment", x = c(3.3, 3.5), xend = c(3.6, 3.8), y = c(14, 14), yend = c(15, 15)) 
  coord_cartesian(clip = "off", ylim = c(15, 25)) 

Get something like this:

enter image description here

If you want to go further, it may take several tries to get it right:

p   annotate("segment", x = c(3.3, 3.5), xend = c(3.6, 3.8), y = c(14, 14), yend = c(15, 15)) 
  annotate("segment", x = c(0, 3.65), xend = c(3.45, 7), y = c(14.55, 14.55), yend = c(14.55, 14.55))  
  coord_cartesian(clip = "off", ylim = c(15, 25))  
  theme_classic() 
  theme(axis.line.x = element_blank())
  

Just replace axis with two new lines. This is a rough idea, it may take some time to make it perfect.

enter image description here

CodePudding user response:

You could use facet_wrap. If you assign the first 3 months to one group, and the other months to another, then you can produce two plots that are side by side and use a single y axis.

It's not exactly what you want, but it will show the data effectively, and highlights the fact that the x axis is not continuous.

dat$group[dat$Month %in% c("1", "2", "3")] <- 1
dat$group[dat$Month %in% c("10", "11", "12")] <- 2

ggplot(data = dat, aes(x = factor(Month), y = Value))  
  geom_boxplot()  
  labs(x = "Month")  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black"),
        axis.text.y = element_text(size = 14, color = "black"))  
  facet_wrap(~group, scales = "free_x")

* Differences in the plot are likely due to using different versions of R where the set.seed gives different result

enter image description here

  • Related