I cannot move my x-axis to the Y = 0 - help

Hi there,

I am trying to move everything (X-axis line, text & ticks) to Y = 0 point?
How can I do this?
I used this: geom_hline(yintercept = 0) but I am not that happy with this solution.

Here is the code:

map(2016:2020, ~ make_returns(Data_m2, .x)) %>%
  do.call('rbind', .) %>%
  ggplot(aes(x = year_month, y = mean/100, colour = tercile)) + 
  ggplot(aes(x = year_month, y = mean/100, colour = tercile)) + 
  geom_line(aes(linetype=tercile))+
  scale_color_manual(name = "tercile", values = c("red", "blue"), labels = c("high CSR portfolio", "low CSR portfolio")) +
  scale_linetype_manual(name = "tercile", values = c("dashed", "solid"), labels = c("high CSR portfolio", "low CSR portfolio")) + 
  labs(x = "time", y = "market returns", title = "Portfolios' returns using Sustainalytics ESG total score", font.main=4, font.lab=4) +
  scale_x_date(breaks = as.Date(c("2016-01-31", "2017-01-31", "2018-01-31","2019-01-31", "2020-01-31", "2020-12-31")), date_labels = "%Y (%b)") +
  geom_vline(xintercept = as.Date(c("2017-01-31", "2017-01-31", "2018-01-31","2019-01-31", "2020-01-31")), linetype = 2) +
  scale_y_continuous(labels = scales::percent) +
  geom_hline(yintercept = 0) +
  theme_classic() + 
  theme(legend.position="bottom") +
  theme(legend.title=element_blank())

I would like to do something similar to this:

Thanks,

F

library(tidyverse)

(mydata <- data.frame(x=-12:22,
                     y=sin(-12:22)))

#problem
ggplot(mydata,
        aes(x=x,y=y)) + geom_line()

#solution
ggplot(mydata,
       aes(x=x,y=y)) + geom_line() + 
  coord_cartesian(ylim=c(0,1))

this just cuts off the negative values, I think francesco wants to keep it.
Actually it's not so easy, here is a similar, older question:

That's okay. Another question is that I would like to add more values in my y-axis (e.g. I would like to insert the +/-5, +/- 15%. How can I do that in R?
This is how my image looks like.

Thanks,

F

Try the breaks argument in scale_y_continuous().

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.