2
votes

I searched and found a similar post in Python not R. I am using Richie Cotton's code in this post Logarithmic y-axis Tick Marks in R plot() or ggplot2(). I don't want to display all labels for minor ticks, I only want to display major ticks such as 1, 10, 100 etc. Please see an exampled image below, that's why I don't want to display all minor tick's labels. I tried to remove "labels=breaks"enter image description here in the code, but nothing happened.

library(ggplot2)

dfr <- data.frame(x = 1:100, y = rlnorm(100))

p <- ggplot(dfr, aes(x, y)) + 
  geom_point() +
  scale_x_log10(breaks = breaks, labels = breaks)

get_breaks <- function(x){
  lo <- floor(log10(min(x, na.rm = TRUE)))
  hi <- ceiling(log10(max(x, na.rm = TRUE)))
  as.vector(10 ^ (lo:hi) %o% 1:9)
}

breaks <- get_breaks(dfr$x)

log10_breaks <- log10(breaks)

p + labs(axis.ticks = element_line(
  size = ifelse(log10_breaks == floor(log10_breaks), 2, 1)
  ))
1
Don't you get what you want with just ggplot(dfr, aes(x, y)) + geom_point() + scale_x_log10()? I'm not sure I understand.MrFlick
@MrFlick OP wants the minor ticks w/o numbersjay.sf
@MrFlick, I want to have all minor ticks on the x-axis, but I don't want to display their labels. Please look at the image above. Thank you.Peter Rowan

1 Answers

4
votes

You could do this.

ggplot(dfr, aes(x, y)) + 
  geom_point() +
  scale_x_log10(breaks = breaks, labels = c(breaks[1:3], rep("", 24))) 

yields:

enter image description here