0

I have a dataset of dates and stock returns. I am trying to create a line plot returns by semiannual periods.

I mutated my dates to semiannual periods using

dataTable <- dataTable %>% mutate(Semiannual = semester(Date, with_year = TRUE))

See a sample of my data along with the new Semiannual column created.

        Date      Return    Semiannual
1  2014-11-30    163.07848     2014.2
2  2014-06-30     10.48201     2014.1
3  2014-03-31    110.00537     2014.1
4  2012-12-31    -95.01492     2012.2
5  2017-07-31      3.30668     2017.2
6  2012-10-31     -0.48359     2012.2
7  2015-12-31     -0.50400     2015.2
8  2016-06-30    974.87289     2016.1
9  2016-02-28     49.37500     2016.1
10 2017-01-31      1.62813     2017.1

This resulted in a table of values ending with .1 as the first semiannual period and values ending with .2 as the second semiannual period.

I will group the data by Semiannual period then plot it.

enter image description here

When I plot the data, the x axis is distorted. I have a huge data gap between 2012.2 and 2014.1.

Do you have any ideas on how to go about making this even? I thought about using 2012.0, 2012.5, 2013, etc.. but it tends to get confusing when I am looking at it later on. Is there any way that I could change my labels to "2011 S2", "2012 S1", "2012 S2", etc. (See, I already got confused translating it back to that)

My goal is to achieve a plot with an x-axis that looks similar to this:

Goal Axis

Or even if it was 2012 S1, 2012 S2, just evenly distanced.

Any help would be much appreciated.

Thank you!

figNewton
  • 107
  • 1
  • 7
  • The package `lubridate` might be helpful for you. If you could provide a subset of your data and a minimal example to help demonstrate what you're trying to do, that would make pointing you in the right direction easier. – phalteman Nov 01 '18 at 16:17
  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 01 '18 at 16:24
  • Updated! Thanks! – figNewton Nov 01 '18 at 17:50

1 Answers1

0

There is no reproducible example here, so I sim a dataset here to answer this question.

library(tidyverse)
library(data.table)
library(lubridate)
set.seed(123)
data.table(
    date = today() + 1:1000
    ,value = rnorm(1000,0,10)
    # sim stock price
) %>% 
    mutate(
        date = floor_date(date, unit = 'halfyear')
        # I suggest you maintain the date in yyyy-mm-dd type
    ) %>% 
    group_by(date) %>% 
    summarise(value = mean(value)) %>% 
    ggplot(aes(date,value)) +
        geom_line() + 
        scale_x_date(breaks = '6 month')

enter image description here

Jiaxiang
  • 619
  • 6
  • 16
  • Thank you - I'm guessing I edited it while you pasted your answer, so I apologize for that. – figNewton Nov 01 '18 at 18:15
  • @rrpking, and I think there are another ways to do it, like `tibbletime` package or make `date` into a ordered factor variable. I am still working on it. When I finish it, I will post here. – Jiaxiang Nov 01 '18 at 18:23