0

enter image description here

this is my second time asking question here. I hope I do it in a right way! I am plotting three subset of my data with ggplot and I am not putting any command for x-axis like every two years or something. My subsets data set are same size(they are 10 years long) but it gives me for two of them on x-axis every 5 years and for one every two years. how can I have same x-axis?

here is my code:

before2000<- wml%>%
  filter(between(YYYYMM, 193001, 193912))

before2000$YYYYMM<-  parse_date(as.character( before2000$YYYYMM), "%Y%m")
before2000$WML<-cumprod( before2000$WML+1)
before2000$WMLScaled<-cumprod( before2000$MOM_BS+1)
before2000<-before2000%>%
  pivot_longer(cols=c(WML,WMLScaled), 
               names_to = "key", values_to = "values")

ggplot(data =before2000, aes(x= YYYYMM, y=values, color= key ))+
  geom_line()+
  labs(title = "Panel A: Riskmanaged momentum: 1930:01 to 1939:12",
       y="Cumlative Return", x="")+
  theme_classic()



after2000<- wml%>%
  filter(between(YYYYMM, 200001, 201012))

after2000$YYYYMM<-  parse_date(as.character( after2000$YYYYMM), "%Y%m")
after2000$WML<-cumprod( after2000$WML+1)
after2000$WMLScaled<-cumprod( after2000$MOM_BS+1)
after2000<-after2000%>%
  pivot_longer(cols=c(WML,WMLScaled), 
               names_to = "key", values_to = "values")

ggplot(data =after2000, aes(x= YYYYMM, y=values, color= key))+
  geom_line()+
  labs(title = "Panel B: Riskmanaged momentum: 2000:01 to 2010:12",
       y="Cumlative Return", x="")+
  theme_classic()


after2010<- wml%>%
  filter(between(YYYYMM, 201101, 202012))

after2010$YYYYMM<-  parse_date(as.character( after2010$YYYYMM), "%Y%m")
after2010$WML<-cumprod( after2010$WML+1)
after2010$WMLScaled<-cumprod( after2010$MOM_BS+1)
after2010<-after2010%>%
  pivot_longer(cols=c(WML,WMLScaled), 
               names_to = "key", values_to = "values")

ggplot(data =after2010, aes(x= YYYYMM, y=values, color= key ))+
  geom_line()+
  labs(title = "Panel C: Riskmanaged momentum: 2011:01 to 2020:12",
       y="Cumlative Return", x="")+
  theme_classic()
Phil
  • 7,287
  • 3
  • 36
  • 66
Neg
  • 1
  • 1
    You didn't supply any data, so your problem isn't reproducible. However, you likely just need to add `scale_x_date()` with an appropriate `date_breaks` argument. So e.g. `+ scale_x_date(date_breaks = "2 years")`. See `?scale_x_date`. – Axeman Nov 22 '21 at 20:14
  • Thanks a lot . Now it gives me two years but with this on x-Axis "YYY-MM-DD". – Neg Nov 23 '21 at 09:19
  • Read `?scale_x_date` and the linked `?strptime`. You can define the `date_labels` argument, e.g. `date_labels = '%Y' should give only the year (including century). – Axeman Nov 23 '21 at 19:28

0 Answers0