title: Quick, sleek and simple way of adding minor ticks in ggplot2?
body: <p>This question has been raised a number of times on StackOverflow over the years (see <a href="https://stackoverflow.com/questions/25944955/adding-minor-ticks-and-labels-to-ggplot2">here</a> and <a href="https://stackoverflow.com/questions/43334538/minor-ticks-not-showing-in-ggplot2-r">here</a>), however I'm yet to come across a way that I'm satisfied with for easily adding unlabelled minor ticks to my ggplot axes.</p>
  <p>Let's generate some dummy data to play around with:</p>
  <pre><code>df &lt;- data.frame(x = rnorm(1000, mean = 25, sd = 5),
              y = rnorm(1000, mean = 23, sd = 3))
  </code></pre>
  <p>There are two methods I've come across for adding unlabelled minor ticks.</p>
  <p><strong>Method 1 - Manually construct axis label vectors</strong></p>
  <p>Concatenate the values that you would like to appear at major ticks with empty spaces defined using <code>&quot;&quot;</code>. If you would like to add just one unlabelled minor tick in-between major tick values, you can construct the vector of axis labels like so:</p>
  <pre><code>axis_values &lt;- c(0, &quot;&quot;, 10, &quot;&quot;, 20, &quot;&quot;, 30, &quot;&quot;, 40, &quot;&quot;, 50)
  </code></pre>
  <p>Or if you'd like <em>n</em> unlabelled minor ticks:</p>
  <pre><code># Where n = 2 and for an axis range [0, 50]
  
  axis_values &lt;- c(0, rep(&quot;&quot;, 2), 15, rep(&quot;&quot;, 2), 30, rep(&quot;&quot;, 2), 45, &quot;&quot;)
  </code></pre>
  <p>The user can then supply this vector to the 'labels' argument in the ggplot2::scale_x_continuous or ggplot2::scale_y_continuous functions as long as the length of the vector of labels matches the length of the vector supplied to the 'breaks' argument in the same functions.</p>
  <pre><code>ggplot(df, aes(x = x, y = y)) +
    geom_point() +
    scale_x_continuous(breaks = seq(0, 50, 5), labels = axis_values, limits = c(0, 50)) +
    scale_y_continuous(breaks = seq(0, 50, 5), labels = axis_values, limits = c(0, 50))
  </code></pre>
  <p><strong>Method 2 - Define your own function for generating axis label vectors</strong></p>
  <p>This <a href="https://stackoverflow.com/questions/25268264/drawing-minor-ticks-not-grid-ticks-in-ggplot2-in-a-date-format-axis">post</a> describes a function to which the user can supply a vector of values to appear at major ticks, along with the number of unlabelled minor ticks desired:</p>
  <pre><code>insert_minor &lt;- function(major_labs, n_minor) {
    labs &lt;- c( sapply( major_labs, function(x) c(x, rep(&quot;&quot;, n_minor) ) ) )
    labs[1:(length(labs)-n_minor)]
  }
  
  # Generate plot
  
  ggplot(df, aes(x = x, y = y)) +
    geom_point() +
    scale_x_continuous(breaks = seq(0, 50, 5), labels = insert_minor(major_labs = seq(0, 50, 10),
                                                                     n_minor = 1), limits = c(0, 50)) +
    scale_y_continuous(breaks = seq(0, 50, 5), labels = insert_minor(major_labs = seq(0, 50, 10),
                                                                     n_minor = 1), limits = c(0, 50))
  </code></pre>
  <p>Method 2 is the best way of generating unlabelled minor ticks I've seen yet. However drawbacks are:</p>
  <ol>
  <li>Not dummy-proof - Users need to make sure that the value given to the 'n_minor' argument is compatible with the data supplied to the 'breaks' and 'major_labs' arguments. Call me lazy, but I don't want to think about this when I'm trying to produce plots quickly.</li>
  <li>Function management required - When you want to use this function in another script, you have to
  retrieve it from the last script you used it in, or alternatively perhaps you can package it up in a library to call in future scripts.</li>
  </ol>
  <p>In my eyes, the ideal solution is for the ggplot2 developers to add an argument to scale_x_continuous or scale_y_continuous ggplot2 functions that takes a user-defined value for the number of unlabelled minor ticks the user would like to add to their plot axes, which then takes the vector supplied to the 'breaks' argument and determines 'major_labs' in the background out of the user's sight.</p>
  <p>Has anyone else found any other way of computing unlabelled minor ticks in ggplot2?</p>