1

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.

gokhale
  • 41
  • 3

1 Answers1

1

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)

Seb
  • 332
  • 1
  • 3
  • Moreover, are you aware that your data are not actually transformed? – o_v Jan 05 '22 at 13:32
  • @Seb I was trying to reproduce an example that is closest to my actual problem. In my plot the data are log transformed with base 10 and capped axes on the plot. Annotation_logticks() generates a trail of 5 small ticks since the coordinate system has a TRUE value in the expand argument. Is there a way of adding an image either to this comment or this post, for a clearer example? – gokhale Jan 05 '22 at 15:53
  • I have updated my initial query to include the actual plot that I am working with. Hope this explains the problem better. – gokhale Jan 05 '22 at 16:26