Dataframe with different lengths

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Dataframe with different lengths

Pedro páramo
Hi all,

I am trying to draw a plot with cumsum values but each "line" has different
lengths

Ilibrary(dplyr)
library(tibble)
library(lubridate)
library(PerformanceAnalytics)
library(quantmod)
library(ggplot2)

getSymbols('TSLA')

I want to create the variables:

a<-cumsum(dailyReturn(TSLA, subset = c('2019')) )
b<-cumsum(dailyReturn(TSLA, subset = c('2020')) )
c<-cumsum(dailyReturn(TSLA, subset = c('2018')) )

Each value, on a,b,c has two columns date, and values.

The thing is I want to plot the three lines in one plot with the maximum
values of a,b,c in this case a has 252 values, and plot the other two lines
could be in the axis I should put (x <- 1:252) on the axis but I was not
able for the moment.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Dataframe with different lengths

R help mailing list-2
Hi Pedro,

I see you use dplyr and ggplot2. Are you looking for something like
this:

```
library(ggplot2)
library(dplyr)

test_data <- data.frame(
   year = c(rep("2018", 10), rep("2019", 8), rep("2020", 6)),
   value = sample(c(1:100), 24)
)

test_data <- test_data %>%
   group_by(year) %>%
   mutate(cumsum_value = cumsum(value),
          x_pos = 1:n())

ggplot(test_data) +
   aes(x = x_pos, y = cumsum_value, colour = year) +
   geom_point()
```

Best,
Ulrik

On 2020-07-22 13:16, Pedro páramo wrote:

> Hi all,
>
> I am trying to draw a plot with cumsum values but each "line" has
> different
> lengths
>
> Ilibrary(dplyr)
> library(tibble)
> library(lubridate)
> library(PerformanceAnalytics)
> library(quantmod)
> library(ggplot2)
>
> getSymbols('TSLA')
>
> I want to create the variables:
>
> a<-cumsum(dailyReturn(TSLA, subset = c('2019')) )
> b<-cumsum(dailyReturn(TSLA, subset = c('2020')) )
> c<-cumsum(dailyReturn(TSLA, subset = c('2018')) )
>
> Each value, on a,b,c has two columns date, and values.
>
> The thing is I want to plot the three lines in one plot with the
> maximum
> values of a,b,c in this case a has 252 values, and plot the other two
> lines
> could be in the axis I should put (x <- 1:252) on the axis but I was
> not
> able for the moment.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.