5

I would like to change the position of the ticks of the left plot to be like the right one (ticks are inside the plot).

library(ggplot2)
library(grid)

p <- ggplot(mtcars,aes(mpg,cyl))+
  geom_point() + 
  theme(
        axis.ticks.length=unit(0.5,"cm"),
        axis.line = element_line(color = 'black',size=0.1),
        axis.ticks.y = element_line(size=1,color='red'),
        axis.text.y = element_text(hjust=0.5))

enter image description here

I think i can get the desired plot playing with grobs but I am surprise that there is not a simple setting to adjust ticks position!

edit (shift tick marks using solution here):

setting axis.ticks.length as mentioned gives nearly the right solution , the axis text should be also postioned more near to the axis. hjust has no effect.

p <- ggplot(mtcars,aes(mpg,cyl))+
  geom_point() + 
  theme(
    axis.ticks.length=unit(-0.25, "cm"), 
    axis.ticks.margin=unit(0.5, "cm"),
    axis.line = element_line(color = 'black',size=0.1),
    axis.ticks.y = element_line(size=1,color='red'),
    axis.text.y = element_text(hjust=0.5)) ##this don't work

enter image description here

Community
  • 1
  • 1
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Maybe geom_rug? http://stackoverflow.com/questions/24180733/how-to-plot-a-comb – zx8754 Jul 23 '15 at 12:13
  • If you're ok with setting all tick marks on the inside, here's a possible solution: http://stackoverflow.com/questions/26367296/how-do-i-make-my-axis-ticks-face-inwards-in-ggplot2 – Roman Luštrik Jul 23 '15 at 12:21
  • @RomanLuštrik looks like a good solution, how about adjusting the text labels to the right? – agstudy Jul 23 '15 at 12:25
  • I thought you could use a negative length to have the ticks inside, per axis: axis.ticks.length = unit(-0.25, "cm") – lawyeR Jul 23 '15 at 12:28
  • @lawyeR yes but it is the same as Roman suggestion – agstudy Jul 23 '15 at 12:35
  • 1
    It would appear adjusting `axis.ticks.margin` can control the distance between labels and axis. – Roman Luštrik Jul 23 '15 at 13:38
  • @RomanLuštrik yes. that 's why I am closing the question as a duplicated. – agstudy Jul 23 '15 at 13:52
  • Has this ever been resolved? I see that this is marked as a duplicate, but the linked answer does not address making the tick marks on the inside of just one axis and not the other. – jkenney9 Aug 18 '16 at 19:51

1 Answers1

3

Here a solution based on manipulating the plot grobs. It gives exactly what I am looking for but manipulating grobs...is never the right way to go (unreadable code)

adjust_ticks <- 
  function(pn,adj=0.5){
    ## get grobs 
    p <- p +theme(
      axis.ticks.length=unit(adj,"cm")
    )
    gt <- ggplotGrob(p)
    # Get the row number of the left axis in the layout
    rn <- which(gt$layout$name == "axis-l")
    ## Extract the axis ticks grobs (text)
    axis.grobs <- gt$grobs[[rn]]
    axisb <- axis.grobs$children[[2]]  
    ## change the position of ticks (text and ticks )
    gt$grobs[[rn]]$children[[2]]$grobs[[2]]$x <- axisb$grobs[[2]]$x + unit(adj,"cm")
    gt$grobs[[rn]]$children[[2]]$grobs[[1]]$x <- axisb$grobs[[1]]$x + unit(adj,"cm")
    ## show the differnce 
    gt
  }

plot(adjust_ticks(p))

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261