0

Hell guys

I have to put the y-axis in the middle of my chart with ggplot.

This is my code:

ggplot(mtcars,aes(cyl,mpg))+geom_point()+scale_x_continuous(limits = c(-8,8))

Now, how I move the y-axis to the x = 0 point?

I thought about create a "fake" axis with, annotate(), vline(), but I dont think this is to clever.

Any suggestion? It would be great if the solutiondont use any other package. Only ggplot.

Any help?

Laura
  • 675
  • 10
  • 32
  • 1
    Answers in possible duplicates do similar to what you propose: https://stackoverflow.com/questions/17753101/center-x-and-y-axis-with-ggplot2 and https://stackoverflow.com/questions/62345433/how-to-center-axes-in-ggplot2 – aosmith Aug 06 '21 at 21:08

1 Answers1

2

Try

ggplot(mtcars,aes(cyl,mpg)) + 
 geom_point() + 
 scale_x_continuous(limits = c(-8,8)) + 
 geom_vline(xintercept = 0) + 
 geom_hline(yintercept = 0)

for this plot

enter image description here

Also look at the answers here, if you want ticks and labels on the lines.

Gregor
  • 160
  • 1
  • 6