3

I'm having some trouble exporting a simple scatterplot to a pdf file in ggplot2. Specifically when I set the tick mark length to a negative value (essential), the tick mark labels merge with the axis line (as shown below).

Problem tick mark labels

The figure appears normally in the plot window of rstudio but when I export to pdf the problem occurs. Altering vjust in axis.text.x doesn't seem to be helping at all. I've also tried manipulating the plot margins.

Is anyone aware of a way of moving the labels away from the axis in such a situation?

Hopefully the code below should replicate the issue.

data = data.frame(xvar = seq(1:20), yvar = seq(1:20), labvar = rep(c("A", "B"), 10))

require(ggplot2)
require(gridExtra)
p <- ggplot(data = data, aes(x = xvar, y = yvar)) +  geom_point() +
  facet_wrap(~labvar, scales = "fixed") +    
  theme_classic()+
  theme(axis.ticks.length=unit(-0.1, "cm"),
  axis.text.x=element_text(vjust = 0))

p
tonytonov
  • 25,060
  • 16
  • 82
  • 98
Adam Kimberley
  • 879
  • 5
  • 12
  • 2
    See Hadley's old comment on this: https://groups.google.com/forum/#!topic/ggplot2/C0iGwoJy-Pw I suppose that's why `vjust` doesn't work. Is `unit(-0.05, "cm")` affordable? It fixed the overlapping for me. – tonytonov Jan 22 '14 at 14:05
  • @tonytonov Thanks for the link, interesting discussion. Reducing the length of the ticks does seem to be one way of doing the job, cheers. – Adam Kimberley Jan 22 '14 at 14:20

1 Answers1

2

Try using axis.ticks.margin:

p + theme(axis.ticks.margin = unit(5, "lines"))

enter image description here


I notice that the documentation for ?theme now includes a list of all theme elements and what they do.

Andrie
  • 176,377
  • 47
  • 447
  • 496