1
votes

I'm having some issues with ggplot2 and y axis tick marks - if someone can provide any input I'd really appreciate it.

I'm trying to create a 'stacked' plot with independent y axis for publication I'm working on. The idea is to have N plots stacked with a common X axis but distinct Y axes for each subplot while making it seem like a single contiguous plot.

I would like inverted tick marks on the x axis (for the bottom most subplot) and all the y axes. Problem is while the tick marks show up on the yaxis breifly in the plot generation they seem to be overwritten at the last stage and become invisible. Its such a minor thing and I could just leave them off but I'd really like to know what is going on for my own sanity...

Below is some sample code that should reproduce the problem and here is an imgur link highlighting the plot style and missing tick marks.

On a tangent, if anyone knows how to customize the axis.line.y.right / axis.line.x.top without using a dummy 'second axis' let me know (it seems a very verbose way of doing something that should be simple).

Thanks for your help

ylim=c(-5,5)
xlim=c(3,12)
ybreaks=c(-2,2)

base <-     ggplot() + 
            theme_bw() +
            scale_y_continuous(limits=ylim, breaks=ybreaks) +
            scale_x_continuous(limits=xlim) + 
            labs(x="", y="") +      
            theme(  panel.grid.major=element_line("gray78",0.5,1),
                    panel.border=element_blank(),
                    axis.text.x=element_blank(),
                    axis.text.y=element_text(margin=unit(c(0,3,0,0), "mm"))) 

bottom <-   base + xlab("xlab") +   
            theme(  axis.ticks.length.y=unit(-2,"mm"),
                    axis.ticks.length.x=unit(-2,"mm"),
                    axis.line.y=element_line(),
                    axis.line.x=element_line(),
                    plot.margin=unit(c(0,5,5,5),"mm"),
                    axis.text.x=element_text(margin=unit(c(3,0,0,0), "mm")))         




middle <-   base + 
            theme(  axis.ticks.length.y=unit(-2,"mm"),
                    axis.ticks.length.x=unit(0,"mm"),
                    axis.line.y=element_line(),
                    axis.line.x=element_line(linetype=3),
                    plot.margin=unit(c(0,5,0,5),"mm"),
                    axis.title=element_blank())   

top <-      base + 
            theme(  axis.ticks.length.y=unit(-2,"mm"),
                    axis.ticks.length.x=unit(0,"mm"),
                    axis.line.y=element_line(),
                    axis.line.x=element_line(linetype=3),
                    plot.margin=unit(c(5,5,0,5),"mm"),
                    axis.title=element_blank())    


ggarrange(top,middle,bottom,ncol=1)
1

1 Answers

0
votes

The easiest and more ggplot-y way would be to use facets.

I recommend using labs(x=NULL, y=NULL), because using = '' actually is drawing something.

I am removing the facet strips in the plot, but I generally think your graph may be slightly less confusing when you keep the labels and also keep a bit of distance between those graphs.

In order to add the dashed lines between your facets, you could simply add annotations, e.g., with annotate(geom = 'segment')

library(ggplot2)
ylim=c(-5,5)
xlim=c(3,12)
ybreaks=c(-2,2)

ggplot(data.frame(facet = letters[1:3])) + 
  theme_bw() +
  annotate(geom = 'segment', x = -Inf, xend = Inf, y = -Inf, yend = -Inf, linetype = 3)+
  scale_y_continuous(limits=ylim, breaks=ybreaks) +
  scale_x_continuous(limits=xlim) + 
  labs(x=NULL, y=NULL) +      
  theme(  panel.grid.major=element_line("gray78",0.5,1),
          panel.border=element_blank(),
          axis.text.x=element_blank(),
          axis.text.y=element_text(margin=unit(c(0,3,0,0), "mm")),
          panel.spacing = unit(0, "lines"),
          strip.background = element_blank(),
          strip.text = element_blank(),axis.line.y=element_line(),
          axis.line.x=element_line(),
          axis.ticks.length.y=unit(-2,"mm"),
          axis.ticks.length.x=unit(-2,"mm"),
          plot.margin=unit(c(b = 5, t = 5, r = 5, l = 5),"mm")) +
  facet_wrap(~facet, nrow = 3)

Created on 2020-02-22 by the reprex package (v0.3.0)