Conditionally changing tick size to NA after a specified value

Viewed 45

Following code produces a plot with y-axis on log scale:

ibrary(ggplot2)
library(tidyverse)

cars %>%
  ggplot(aes(x = speed, y = dist)) +
  geom_point() +
  scale_y_continuous(trans = "log2", breaks = c(2, 4, 8, 16, 64, 128),
                     limits = c(2, 128)) +
  annotation_logticks(sides = "l") +
  theme_bw()

enter image description here

I want the axis tick marks to stop at 2. Please note, I do not wish to use the expand = c(0, 0) argument, since I want the white space around the plot. Is there a way to conditionally set the size of the small tick marks to NA if they are representing values less than 2?

I have found a few queries like this one which conditionally change the text that appears on the x-axis. Is there a similar or better way to do that with the tick sizes of the annotation_logticks?

Any help will be appreciated.

enter image description here

I am adding my actual plot for reference - the red box marks the ticks that need to be removed.

1 Answers

Are you sure you want base 10 ticks when using a log2 scale? You can change the base in the annotation_logticks() function. See below.

library(ggplot2)
library(tidyverse)

cars %>%
  ggplot(aes(x = speed, y = dist)) +
  geom_point() +
  scale_y_continuous(trans = "log2", breaks = c(2, 4, 8, 16, 64, 128),
                     limits = c(2, 128)) +
  annotation_logticks(base = 2, sides = "l") +
  theme_bw()

Created on 2022-01-05 by the reprex package (v2.0.1)

Related