Axis labels run into axis when exporting plots with negative tick mark length (ggplot2)

StackOverflow https://stackoverflow.com/questions/21284501

  •  01-10-2022
  •  | 
  •  

Pregunta

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
¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top