How to remove extra segment on y-axis in combined ggplot2 plots?

I’m using ggplot2 in R to create a set of plots, which I then arrange into a single composite figure using grid.arrange from the gridExtra package. Although I managed to position the plots closely together, the barplots in the top-left and bottom-right positions show an unwanted extra segment at the bottom of their y-axes. I need help removing these segments.

Here’s a simplified version of the data and the code I’m using:

set.seed(444)  


var1 <- rnorm(100, mean = 50, sd = 10)  
var2 <- runif(100, min = 0, max = 100)
categories <- c("Category A", "Category B", "Category C")
var3 <- sample(categories, 100, replace = TRUE)

jesper_data_random <- data.frame(
  Forces = var1,
  Separation = var2,
  Coloring = var3
)

And here is the code I am using for the plot:


main_plot <- jesper_data_random %>%
  ggplot(aes(
    x = Separation,
    y = Forces,
    colour=Coloring
  )) +
  geom_point() +
  labs(x="Distance", y="Force")+
  scale_x_continuous(position="top",
                     expand = expansion(mult = c(0.005, 0.005))
                     ) +
  scale_y_continuous(position="right",
                     expand = expansion(mult = c(0.005, 0.005))
                     ) +
  theme(
    plot.margin = margin(0,0,-70,-70)
    ) +
  theme_bw() +
  coord_cartesian(clip = "off")


xplot <- ggplot(jesper_data_random, aes(x = Separation)) +
  geom_histogram(aes(fill = Coloring, color = Coloring), bins = 30) +
  scale_y_continuous(
    expand = expansion(mult = c(0.005, 0.005))
    ) +
  scale_x_continuous(
    expand = expansion(mult = c(0.005, 0.005))
    ) +
  theme_classic() +
  theme(
    axis.ticks.x = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    plot.margin = margin(0,40,0,-29)
  )

xplot <- xplot + scale_y_reverse()


yplot <- ggplot(jesper_data_random, aes(x = Forces)) +
  geom_histogram(aes(fill = Coloring, color = Coloring), bins = 30) +
   scale_y_continuous(
     expand = expansion(mult = c(0.005, 0.005))
   ) +
  
   scale_x_continuous(
     expand = expansion(mult = c(0.005, 0.005))
   ) +
  theme_classic() +
  theme(
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    plot.margin = margin(35,0,-23,0)
  ) +
  rotate()

yplot <- yplot + scale_y_reverse()


# Cleaning the plots
main_plot <- main_plot + rremove("legend")
yplot <- yplot +  rremove("legend") 
xplot <- xplot + rremove("legend")


blank_plot <- ggplot() + 
  theme_void() + 
  theme(plot.background = element_blank(),
        panel.grid = element_blank(),
        #plot.margin = unit(c(0,0,0,0), "lines"),
        plot.margin = margin(0,0,0,0))

grid.arrange(grobs = list(yplot, main_plot, blank_plot, xplot), ncol = 2)

The barplots xplot and yplot have an extra segment at the bottom of their y-axes, which I want to remove. How can I adjust the y-axis to eliminate this segment, or is there another way to clean up the axis appearance in these plots?

In the picture I have indicated the segments I would like to remove

Initially, I suspected that the problem might be related to the plot margins. After extensively adjusting the margin settings, I confirmed that the margins are set correctly, and they allow the plots to be positioned closely in the combined structure. However, the issue with the extra segment on the y-axis persisted. Upon further investigation, I noticed that this segment only appears after applying scale_y_reverse(). I’ve searched extensively but haven’t found a solution to remove this unwanted segment. Any insights or suggestions would be greatly appreciated.

New contributor

carcarmor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật