0

I have the following data:

set.seed(12)
df <- rnorm(1260, 0.06, 0.2)

These are 5 years worth of daily returns (with 1 year = 252 working days) and what I would like to do is draw a line-chart with months on the x axis. Basically, I would have the sequence Jan:Dec repeated five times on the x-axis, with 21 days being one month.

What I did is the following:

  1. Create a column with months jan-dec repeated 5 times
date <- c("Jan", "Feb", "Mär", "Apr", "Mai", "Jun", 
          "Jul", "Aug", "Sep", "Okt", "Nov", "Dez")
date <- rep(date, 5)
  1. Draw graph
df %>%
       ggplot(aes(x = date, y = return)) +
       geom_line() +
       labs(title = "Stock return Chart", y = "return", x = "date")

Unfortunately I get the following error:

 Error: Aesthetics must be either length 1 or the same as the data (1260): x 
Jj Blevins
  • 355
  • 1
  • 13
  • can you provide a minimum viable example? Ie, provide something we can copy and paste to reproduce your error. – Eric May 27 '19 at 16:20
  • `df` is not a data frame. Suggest better name or else make it a data frame. Also it is not reproducible without a `set.seed` statement to set the random seed. It is not clear whether you want one point per day or one point per month or a monthly candlestick or barchart. – G. Grothendieck May 27 '19 at 16:56
  • That addresses only one of the points in my comment. – G. Grothendieck May 27 '19 at 16:58
  • I have provided an answer. However, I agree with @G.Grothendieck about your question saying something and providing us with another thing like `df` not being a dataframe. Putting that aside and assuming that your data actually looks like this, I should say that data cleaning comes before plotting your data. – M-- May 27 '19 at 17:05
  • @G.Grothendieck I'll try to be more clear next time. I am quite new to R and I have to figure out what classifies as data-frame and what not. – Jj Blevins May 27 '19 at 17:15

2 Answers2

2
library(tidyverse)

df %>%
    as.data.frame() %>%
    rename(price = 1) %>% 
    mutate(rnames = rownames(.)) %>% 
    ggplot(aes(x = as.numeric(rnames), y = price, 
                group = rep(1:5, times=1, each=252))) +
      geom_line() +
      labs(title = "Stock Price Chart", y = "Price", x = "date") +
      scale_x_continuous(breaks = seq(1, 1260, by = 21), labels = date)

![](https://i.imgur.com/jR6C8rI.png)

Created on 2019-05-27 by the reprex package (v0.3.0)

akrun
  • 874,273
  • 37
  • 540
  • 662
M--
  • 25,431
  • 8
  • 61
  • 93
  • I just added a new row into my df above the first one, in order for the price to start at 0 in time = 0. I tried adjusting the code but cannot do it. Is there a quick way to do it? – Jj Blevins May 27 '19 at 18:41
  • @JjBlevins ask a follow-up question, and provide a link to this one. You still need to make a reproducible example in the other thread, but referring to this one helps. There, provide your new df and let me know when you posted it. I'll take a look and reply if I could. Without a reproducible example it's hard to say what you need. (P.S. you are not supposed to ask multiple questions in one thread, that's why I am suggesting posting another one.) – M-- May 27 '19 at 18:44
  • Noted! Posted a follow-up here: https://stackoverflow.com/questions/56331202/ggplot2-line-chart-of-time-series-data-with-monthly-labels-on-x-axis-but-daily – Jj Blevins May 27 '19 at 18:57
0

Try this:

price <- rnorm(1260, 0.06, 0.2)

date.base <- c("Jan", "Feb", "Mär", "Apr", "Mai", "Jun", 
          "Jul", "Aug", "Sep", "Okt", "Nov", "Dez")
date <- rep(date.base, 5)

data.frame(date=factor(date, ordered=TRUE, levels=date.base), price=price) %>%
  ggplot(aes(x = date, y = price)) +
  geom_line() +
  labs(title = "Stock Price Chart", y = "Price", x = "date")
Eric
  • 946
  • 2
  • 12
  • 25
  • added example data. This doesn*t work for me, since days are labeled as months if I do it as in your example. – Jj Blevins May 27 '19 at 16:28
  • Sorry, my questions might have been quite unclear formulated. The x-axis should look like this: `Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez", Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez",Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez",Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez",Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez" ` – Jj Blevins May 27 '19 at 16:52