Adding breaks to a y-axis on a facet_grid ggplot

Hello - I have spent days trying to figure out how to create breaks on my y-axis on a facet_grid in ggplot. I used scale_x_continuous to manipulate my x-axis, but that function will not work for my y-axis. My y-axis is depth in feet, is numeric, and has limits from 0-55.

Here is my code:

do_vs_depth <- ggplot(data = do_monthly_means,
       aes(x = mean_do,
           y = reorder(depth_ft, - depth_ft))) +
  geom_point(color = "steelblue", size = 1) +
  facet_grid(site ~ month) +
  scale_x_continuous(limits = c(0, 12),
                      expand = c(0, 0),
                      breaks = seq(0, 10, by = 3)) +
  # scale_y_continuous(limits = c(0, 60),
  #                    breaks = seq(20, 60, by = 10)) +
  theme_bw() +
  labs(x = "DO (mg/L)",
       y = "Depth (ft)")

My graph looks like this:

1 Like

Hi @hangarwick ,
I asked a somewhat similar question and another user helps me:

Its not for breaks but help in scale graphics.

facet_grid(site ~ month, scale="free_x")

# whitout
 scale_x_continuous()
1 Like

I would assume that the reorder-step actually converts your numeric data into a factor.
Maybe you just show the depth as negative value with:

do_vs_depth <- ggplot(data = do_monthly_means,
       aes(x = mean_do,
           y = -depth_ft))

Thank you for your response, Matthias. I took out the reorder for my y_axis. This allowed me to get my y-axis looking the way I want, but now I can't get my points to show up on my figure.

I also experimented using the arrange function, both ascending and descending that data. It seems using arrange isn't necessary if I use y = -depth_ft

test <- ggplot(data = do_means_test,
               aes(x = mean_do,
                   y = -depth_ft)) +
  geom_point(color = "steelblue", size = 1) +
  facet_grid(site ~ month) +
  scale_y_continuous(limits = c(60, 0),
                     breaks = seq(60, 10, by = -10)) +
  theme_bw() +
  labs(x = "DO (mg/L)",
       y = "Depth (ft)")

My graph looks like:

Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

Hi andresrcs - I hope I did this right (thank you for the links):

data <- tibble::tribble(
           ~site, ~depth_ft, ~month,    ~mean_do,
          "LP-2",        0L,  "Apr", 9.696666667,
          "LP-2",        0L,  "Aug",        8.14,
          "LP-2",        0L,  "Dec",       11.05,
          "LP-2",        0L,  "Feb",       10.85,
          "LP-2",        0L,  "Jan",       10.23,
          "LP-2",        0L,  "Jul",        8.27,
          "LP-2",        0L,  "Jun",       8.545,
          "LP-2",        0L,  "Mar",       10.72,
          "LP-2",        0L,  "May",       8.645,
          "LP-2",        0L,  "Nov",        9.44,
          "LP-2",        0L,  "Oct", 8.476666667,
          "LP-2",        0L,  "Sep",        7.53,
          "LP-2",        4L,  "Apr", 9.673333333,
          "LP-2",        4L,  "Aug",        8.06,
          "LP-2",        4L,  "Dec",       10.89,
          "LP-2",        4L,  "Feb",       10.77,
          "LP-2",        4L,  "Jan",       10.18,
          "LP-2",        4L,  "Jul",        8.25,
          "LP-2",        4L,  "Jun",        8.39,
          "LP-2",        4L,  "Mar",       10.68,
          "LP-2",        4L,  "May",       8.645,
          "LP-2",        4L,  "Nov",       9.355,
          "LP-2",        4L,  "Oct",        8.35,
          "LP-2",        4L,  "Sep",        7.32
          )

Not sure why there are "Ls" in the depth_ft column. It does not show up when I do head(data)

1 Like

Use scale_y_reverse(), otherwise ggplot2 can't make sense of the specified scale limits.

library(tidyverse)

do_means_test <- tibble::tribble(
    ~site, ~depth_ft, ~month,    ~mean_do,
    "LP-2",        0L,  "Apr", 9.696666667,
    "LP-2",        0L,  "Aug",        8.14,
    "LP-2",        0L,  "Dec",       11.05,
    "LP-2",        0L,  "Feb",       10.85,
    "LP-2",        0L,  "Jan",       10.23,
    "LP-2",        0L,  "Jul",        8.27,
    "LP-2",        0L,  "Jun",       8.545,
    "LP-2",        0L,  "Mar",       10.72,
    "LP-2",        0L,  "May",       8.645,
    "LP-2",        0L,  "Nov",        9.44,
    "LP-2",        0L,  "Oct", 8.476666667,
    "LP-2",        0L,  "Sep",        7.53,
    "LP-2",        4L,  "Apr", 9.673333333,
    "LP-2",        4L,  "Aug",        8.06,
    "LP-2",        4L,  "Dec",       10.89,
    "LP-2",        4L,  "Feb",       10.77,
    "LP-2",        4L,  "Jan",       10.18,
    "LP-2",        4L,  "Jul",        8.25,
    "LP-2",        4L,  "Jun",        8.39,
    "LP-2",        4L,  "Mar",       10.68,
    "LP-2",        4L,  "May",       8.645,
    "LP-2",        4L,  "Nov",       9.355,
    "LP-2",        4L,  "Oct",        8.35,
    "LP-2",        4L,  "Sep",        7.32
)

ggplot(data = do_means_test,
               aes(x = mean_do,
                   y = depth_ft)) +
    geom_point(color = "steelblue", size = 1) +
    facet_grid(site ~ month) +
    scale_y_reverse(limits = c(60L, 0L),
                    breaks = seq(60, 10, by = -10)) +
    theme_bw() +
    labs(x = "DO (mg/L)",
         y = "Depth (ft)")

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

The "Ls" appear there to denote that those numbers are "integers"

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

THANK YOU SO MUCH! It has taken months to get this figured out. And thank you, I forgot about reprex. My data analysis teacher would be very disappointed...

Again, thank you x a million!!!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

Perfect, just checked your response as the solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.