1

I'd like to use ggplot2 to make a plot where the axis text size varies between labels- for example, larger font every five ticks with smaller font for the intervening ticks. I looked at using minor_breaks in scale_x_continuous, but I couldn't find a way to label the minor breaks.

The best I've got to work so far is a modification from this answer where I use bquote to pass an expression for the axis labels:

label_span <- 1:40

ShrinkIf <- Vectorize(function(val) {
  if (val %% 5 == 0) return(as.character(val))
  return(bquote(scriptstyle(.(as.character(val)))))
})

x_labels <- ShrinkIf(label_span)

x_labels <- purrr::invoke(expression, x_labels)

ggplot(mtcars, aes(x = mpg , y = hp)) +
  geom_point() + 
  scale_x_continuous(breaks = label_span, labels = x_labels)

enter image description here

Is there a better way to go about this, or maybe a way with a little more control of the label size (or even font choice / text decoration, etc)? Thanks in advance for your help!

1 Answer 1

2

You can create a vector of text sizes and add it using element_text()

library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = hp)) + 
    geom_point() + 
    theme(
        axis.text.x = element_text(size = rep(c(12,24), 3))
    )

example

Sign up to request clarification or add additional context in comments.

1 Comment

Clean and simple, that'll do it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.