title: ggplot2: shorter tick marks for tick marks without labels (for y axis)
body: <p>I'm trying to replicate an answer by @sandy-muspratt here : <a href="https://stackoverflow.com/questions/27850123/ggplot2-have-shorter-tick-marks-for-tick-marks-without-labels">ggplot2: Have shorter tick marks for tick marks without labels</a></p>
  
  <p>but I'd like to add shorter tick marks for the y axis. The original code is</p>
  
  <pre><code>library(ggplot2)
  library(grid)
  
  # Data
   df = data.frame(x = 1:10, y = 1:10)
  
  # Range of x values
  range = 1:10
  
  # Major tick marks
  major = 1
  
  # Minor tick marks
  minor = 0.2
  
  # Function to insert blank labels
  # Borrowed from https://stackoverflow.com/questions/14490071/adding-minor-tick-marks-to-the-x-axis-in-ggplot2-with-no-labels/14490652#14490652
  insert_minor &lt;- function(major, n_minor) {
        labs &lt;- c(sapply(major, function(x, y) c(x, rep("", y) ), y = round(n_minor)))
        labs[1:(length(labs) - n_minor)]
  }
  
  # Getting the 'breaks' and 'labels' for the ggplot
  n_minor = major/minor - 1
  breaks = seq(min(range), max(range), minor)
  labels = insert_minor(seq(min(range), max(range), major), n_minor)
  if(length(breaks) &gt; length(labels)) labels = c(labels, rep("", length(breaks) - length(labels)))
  
  # The plot
  p &lt;- ggplot(df, aes(x = x, y = y)) + 
       geom_point() + 
       scale_x_continuous(breaks = breaks, labels = labels) + 
       coord_cartesian(xlim = range) +
       theme_bw() +
       theme(panel.grid = element_blank(),
             axis.text.x = element_text(margin = margin(t = 5, unit = "pt")))
  p
  
  # Edit the plot:
  # Change the lengths of the major tick marks
  
  g = ggplotGrob(p)
  
  # Get the x axis
  xaxis &lt;- g$grobs[[which(g$layout$name == "axis-b")]]  
  
  # Get the tick marks and tick mark labels   
  ticks &lt;- xaxis$children[[2]]
  
  # Get the tick marks
  marks = ticks$grobs[[1]]
  
  # Edit the y positions of the end points of the tick marks
  # The '6' and the '3' in the code below 
  # are the lengths in pts of the major and minor tick marks respectively. 
  marks$y = unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), unit(1, "npc"),   
                   rep(unit.c(unit(1, "npc") - unit(3, "pt"), unit(1, "npc")), n_minor)))
  
  # Put the tick marks back into the plot
  ticks$grobs[[1]] = marks
  xaxis$children[[2]] = ticks
  g$grobs[[which(g$layout$name == "axis-b")]]  = xaxis
  
  # Draw the plot
  grid.newpage()
  grid.draw(g)
  </code></pre>
  
  <p>I tried to modify the code to make it work for the y axis, but was not able to get longer lines for the major tick marks:</p>
  
  <pre><code>library(ggplot2)
  library(grid)
  
  # Data
   df = data.frame(x = 1:10, y = 1:10)
  
  # Range of y values
  range = 1:10
  
  # Major tick marks
  major = 1
  
  # Minor tick marks
  minor = 0.2
  
  # Function to insert blank labels
  # Borrowed from https://stackoverflow.com/questions/14490071/adding-minor-tick-marks-to-the-x-axis-in-ggplot2-with-no-labels/14490652#14490652
  insert_minor &lt;- function(major, n_minor) {
        labs &lt;- c(sapply(major, function(x, y) c(x, rep("", y) ), y = round(n_minor)))
        labs[1:(length(labs) - n_minor)]
  }
  
  # Getting the 'breaks' and 'labels' for the ggplot
  n_minor = major/minor - 1
  breaks = seq(min(range), max(range), minor)
  labels = insert_minor(seq(min(range), max(range), major), n_minor)
  if(length(breaks) &gt; length(labels)) labels = c(labels, rep("", length(breaks) - length(labels)))
  
  # The plot
  p &lt;- ggplot(df, aes(x = x, y = y)) + 
       geom_point() + 
       scale_y_continuous(breaks = breaks, labels = labels) + 
       coord_cartesian(ylim = range) +
       theme_bw() +
       theme(panel.grid = element_blank(),
             axis.text.y = element_text(margin = margin(t = 5, unit = "pt")))
  p
  
  # Edit the plot:
  # Change the lengths of the major tick marks
  
  g = ggplotGrob(p)
  
  # Get the x axis
  yaxis &lt;- g$grobs[[which(g$layout$name == "axis-l")]]  
  
  # Get the tick marks and tick mark labels   
  ticks &lt;- yaxis$children[[2]]
  
  # Get the tick marks
  marks = ticks$grobs[[1]]
  
  # Edit the y positions of the end points of the tick marks
  # The '6' and the '3' in the code below 
  # are the lengths in pts of the major and minor tick marks respectively. 
  marks$x = unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), unit(1, "npc"),   
                   rep(unit.c(unit(1, "npc") - unit(3, "pt"), unit(1, "npc")), n_minor)))
  
  # Put the tick marks back into the plot
  ticks$grobs[[1]] = marks
  yaxis$children[[2]] = ticks
  g$grobs[[which(g$layout$name == "axis-l")]]  = yaxis
  
  # Draw the plot
  grid.newpage()
  grid.draw(g)
  </code></pre>
  
  <p>I'd greatly appreciate any help making this work. Thank you.</p>