1

I'm trying to add a border to the axis of this ggplot but it extends past the 0Mb mark and I would like it to start there. Is there a way to start it at 0 or have it covered up by a white line in the negative direction so that it doesn't show? I added it with the following code.

theme(axis.line.x.bottom = element_line(colour = "black")

Additionally, is there any way to add a little bit of padding to the top / move the x-axis down a bit so that it's not right inline with the last element?

enter image description here

Many thanks.

  • It will depend on what code/data you're using. Can you demonstrate the issue with some simpler data? – Jon Spring Apr 28 '23 at 04:39
  • It is atypical for a vanilla ggplot to see the x axis extend beyond the right edge of the y-axis labels. Are you using geom_text to place those manually? – Jon Spring Apr 28 '23 at 06:23

1 Answers1

1

One potential option is to adjust the x axis scale 'expansion' to 0, filling that whitespace, and effectively making the x axis border start at zero:

Minimal, reproducible example:

library(tidyverse)

data("ChickWeight")

# example
ChickWeight %>%
  mutate(Chick = factor(Chick, levels = 50:1)) %>%
  filter(Chick %in% 1:20) %>%
  ggplot(aes(x = weight, y = Chick)) +
  geom_col(position = "identity") +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank(),
        axis.line.x.bottom = element_line(colour = "black"))


Potential solution:

ChickWeight %>%
  mutate(Chick = factor(Chick, levels = 50:1)) %>%
  filter(Chick %in% 1:20) %>%
  ggplot(aes(x = weight, y = Chick)) +
  geom_col(position = "identity") +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank(),
        axis.line.x.bottom = element_line(colour = "black")) +
  scale_x_continuous(expand = expansion(mult = c(0, 0.25)))

Created on 2023-04-28 with reprex v2.0.2


Or, if you wanted to keep that whitespace for some reason, you could also draw a line on your plot using geom_segment(), e.g.

library(tidyverse)

data("ChickWeight")

ChickWeight %>%
  mutate(Chick = factor(Chick, levels = 50:1)) %>%
  filter(Chick %in% 1:20) %>%
  ggplot(aes(x = weight, y = Chick)) +
  geom_col(position = "identity") +
  geom_segment(aes(x = 0, xend = max(ChickWeight$weight),
                   y = 0.425, yend = 0.425), inherit.aes = FALSE) +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank())

Created on 2023-04-28 with reprex v2.0.2

Do either of these approaches solve your problem?

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • This might solve it, but I see the OP's current x-axis current extends to the left beyond the y-axis labels, which suggest they are using some sort of atypical theme or layout to have that problem in the first place. – Jon Spring Apr 28 '23 at 06:21
  • Ahh, yes, I see the problem now - you're absolutely right @JonSpring. Perhaps OP used `geom_text()` to put the labels there, or maybe the plot is generated from another package? I'm not sure what organism has 25 chromosomes (some sort of plant maybe?) but if the plot is created by a package using an 'atypical' layout these solutions probably won't work... I guess we have to wait to see if OP adds more details/info – jared_mamrot Apr 28 '23 at 06:32