Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lines on top and bottom of ggplot plot area

Tags:

r

ggplot2

themes

I want to add a line on the top and bottom of my plots (bottom line below the x label and axis) created using ggplot2. So far I have added a rectangle around the plot, but I do not want the lines on the sides.

x <- 1:10
y <- rnorm(10,mean = x)
df <- data.frame(x,y)
library(ggplot2)
ggplot(data = df, mapping = aes(x,y)) + geom_point() +
  theme(plot.background = element_rect(size = 1, color = 'blue'))

I hope you guys have a solution.

like image 770
Markus Avatar asked Feb 03 '26 14:02

Markus


1 Answers

Will something similar to this work?

x <- 1:10
y <- rnorm(10,mean = x)
df <- data.frame(x,y)
ggplot(data = df, mapping = aes(x,y)) + geom_point() +
    annotate(geom = 'segment', 
             y = Inf, 
             yend = Inf, 
             x = -Inf, 
             xend = Inf, 
             size = 2) +
    theme(axis.line.x = element_line(size = 1))

enter image description here

like image 51
forestfanjoe Avatar answered Feb 06 '26 02:02

forestfanjoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!