0

I like my ggplots to have the axis always start at 0, so I use this code within my ggplot to do it :

coord_cartesian(xlim = c(0,max(df$A)), ylim = c(0,max(df$B))) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +

As a full reproducible example:

df = tibble(A = sample(1:100,100, replace = TRUE), B = seq(1,100))

df  %>% 
    filter(A > 30)  %>% 
    ggplot(aes(A, B)) +
        geom_point() +
        coord_cartesian(xlim = c(0,max(df$A)), ylim = c(0,max(df$B))) +
        scale_x_continuous(expand = c(0, 0)) +
        scale_y_continuous(expand = c(0, 0)) 

Now, those 3 lines are a pain to remember, and quite bloated.

Is there a way I can put those 3 lines into a function, and call it within the ggplot whenever I want to limit my axis?

I saw this link:

ggplot - function for graph styling

Which you can use a function for the theme() part, but I am not sure how to do it for just those 3 lines I put above i.e. coord_cartesian, scale_x etc.

I.e. I want to do something like this:

df  %>% 
    filter(A > 30)  %>% 
    ggplot(aes(A, B)) +
        geom_point() +
        fix_the_axis()

Many thanks

2
  • Why not just use + xlim(0, NA) + ylim(0, NA)? Commented Oct 21, 2019 at 11:05
  • Hey, unfortunately doesnt do exactly what I want. It still leaves that padding around the 0, which is made more pronounced when you have a border around the chart :/ Commented Oct 21, 2019 at 11:08

2 Answers 2

2

Just return a list with the 3 elements and add that to your plot (ggplot understands list arguments):

zero_line <- function() {
   list(expand_limits(x = 0, y = 0),
        scale_x_continuous(expand = c(0, 0)),
        scale_y_continuous(expand = c(0, 0)))
}

df  %>% 
    filter(A > 30)  %>% 
    ggplot(aes(A, B)) +
        geom_point() +
        zero_line()

Scatterplot

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

2 Comments

Exactly what I want, thank you thothal; this is going to save me a lot of time!
Very nice! Much cleaner than my approach.
2

I am not sure how exactly you would go about it using the + annotation, but here is one approach:
Defining the function:

fix_axis = function(df_plot) {
  df_plot +
    expand_limits(x = 0, y = 0) +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0))
}

Now there are two ways I see to apply it that are both not as smooth as using the + annotation:

Using a bit awkward brackets:

df_plot_fixed = (df  %>% 
    filter(A > 30)  %>% 
    ggplot(aes(A, B)) +
        geom_point()) %>%
        fix_axis()

Using an extra line for the function call:

df_plot_fixed = df  %>% 
    filter(A > 30)  %>% 
    ggplot(aes(A, B)) +
        geom_point()

df_plot_fixed = df_plot_fixed %>%
  fix_axis()

These do what you want them to do and are maybe a bit smoother than always adding the three lines separately, but still not the perfect solution I assume.

This is the resulting plot:
enter image description here

1 Comment

Thank you, this is great! :)

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.