0

I`m trying to plot a function using the ggplot2 package in R but I'm stuck with having the y-axis at its "usual" position. So right now my plot looks like this: enter image description here

although it should look like this:

enter image description here

Sorry for the bad painting btw, I hope you get the idea.

Do you have any ideas, how to create while with keeping the arrow at the end of the y-axis?

My code:

library(ggplot2)
a <- 1
x <- seq(-2,4,1/100)
y <- pnorm(x,a,1)

dat <- data.frame(x=x,y=y)
dat2 <- data.frame(x=a,y=seq(0,1,1))
ggplot(dat,aes(x=x,y=y))+ 
  geom_line()+
  theme_minimal()+
  labs(y="F")+
  geom_line(data = dat2, aes(x=x,y=y),linetype = "dashed")+
  geom_text(aes(x=a+0.1,y=0.4),label = "a")+
  theme(axis.line.x = element_line(arrow=arrow(length = unit(3, 'mm'))), 
        axis.line.y=element_line(arrow = arrow(length = unit(3, 'mm'))
    )
  ) +
  scale_x_continuous(expand = c(0, 0), limits = c(-2, 4)) +
  scale_y_continuous(expand = c(0, 0),limits = c(0, 1))

Thanks a lot!

trqkiz
  • 19
  • 3
  • 1
    That's not really a thing `ggplot` likes to do. See possible work arounds at these places: https://stackoverflow.com/questions/17753101/center-x-and-y-axis-with-ggplot2 https://stackoverflow.com/questions/62345433/how-to-center-axes-in-ggplot2 – MrFlick Apr 22 '21 at 17:20
  • that worked out for me, thanks! – trqkiz May 04 '21 at 18:07

1 Answers1

-1

Just change the limits on your x-axis. Change your code to:

scale_x_continuous(expand = c(0, 0), limits = c(0, 4))
Pgreene
  • 1
  • 1