0

I have this barplot I made with ggplot but it's floating above the x-axis a little bit.

I want to move the 0 on the y-axis down a bit so that it's in-line with the x-axis. How can this be done?

code

library(ggplot2)

# Make data
fake_colony_data <- round(runif(24, 100, 500),0) 
fake_colony_data[13:24] <- round(runif(12, 20, 100),0)
fake_colony_data[c(FALSE, TRUE)] <- fake_colony_data[c(FALSE, TRUE)]*0
transf_data <- data.frame(selection=rep(c('NTC 200', 'ZEO 50'), each=12),
                 biorep=rep(c('1', '1(-ve)', '2', '2(-ve)', '3', '3(-ve)', '4', '4(-ve)', '5', '5(-ve)', '6', '6(-ve)'), times=2),
                 colonies=fake_colony_data)

# Plot
ggplot(data=transf_data, aes(fill=selection, x=biorep, y=colonies)) +
  geom_bar(position = "dodge", stat="identity") + 
  theme_classic() + 
  labs(title="Transformations", x="Experiment", y = "No. of colonies") + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  scale_fill_manual(values=c("darkblue",
                             "green"))

enter image description here

  • 2
    The space is a result of the default expansion of the scale. To avoid that use e.g. `scale_y_continous(expand = expansion(mult = c(0, .05))`. – stefan Jan 19 '23 at 00:10

1 Answers1

1

Try with scale_y_continuous(expand = expansion(mult = 0)), you can check ?scale_x_continuous and ?expansion for more edetails.

ggplot(data=transf_data, aes(fill=selection, x=biorep, y=colonies)) +
  geom_bar(position = "dodge", stat="identity") + 
  theme_classic() + 
  labs(title="Transformations", x="Experiment", y = "No. of colonies") + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  scale_fill_manual(values=c("darkblue","green")) +
  scale_y_continuous(expand = expansion(mult = 0))

margusl
  • 7,804
  • 2
  • 16
  • 20