Plotting different series with different lengths quantmod - r

I want to plot cumsum line for each year of a series, but the thing is each year have different observations. I have tried using Chart_series but it doesn't work.
My goal is to plotting lines as years in terms of cumulative evolution.
In the example I put only two years.
library(PerformanceAnalytics)
library(quantmod)
library(tidyverse)
library(tidyquant)
library(xts)
a<-dailyReturn(TSLA,subset='2020')
a2019<-dailyReturn(TSLA,subset='2019')
b<-cumsum(a)*100
b2019<-cumsum(a2019)*100
plot(b2019)
lines(b)

We could get a single dataset and then do a group by cumsum before plotting
library(dplyr)
library(tibble)
library(lubridate)
library(PerformanceAnalytics)
library(quantmod)
library(ggplot2)
getSymbols('TSLA')
dailyReturn(TSLA, subset = c('2019', '2020')) %>%
as.data.frame %>%
rownames_to_column('Date') %>%
mutate(Date = as.Date(Date)) %>%
group_by(Year = year(Date)) %>%
mutate(CumDaily.returns = cumsum(daily.returns) * 100) %>%
ggplot(aes(x = Date, y = CumDaily.returns, color = Year)) +
geom_line() +
theme_bw()
-output

Related

Histogramns of the data grouped by month

If I would like to aggregate the data by month, an approach is the following:
library(dplyr)
library(lubridate)
set.seed(2017)
options(digits=4)
(expenses <- data_frame(
date=seq(as.Date("2016-01-01"), as.Date("2016-12-31"), by=1),
amount=rgamma(length(date), shape = 2, scale = 20)))
Then I summarized them by month like this:
expenses %>% group_by(month=floor_date(date, "month")) %>%
summarize(amount=sum(amount))
I would like to plot an histogram of the variable amount for each month. How could I do it?
Extract the month value from date and using facets you can show histogram for every month in separate plots.
library(dplyr)
library(ggplot2)
expenses %>%
arrange(date) %>%
mutate(month = format(date, '%b %Y'),
month = factor(month, unique(month))) %>%
ggplot() + aes(amount) +
geom_histogram(bins = 10) +
facet_wrap(~month)

ggannimate - basing transition sequence on quarter

library(lubridate)
library(gganimate)
library(dplyr)
library(ggplot2)
data("crime")
#creating test data and getting quarter
TestData <- crime %>%
mutate(Quarter_year = floor_date(time, unit = 'quarter'),
Quarter_year = as.Date(Quarter_year)) %>%
group_by(Quarter_year) %>%
tally()
#Creating a simple bar graph
Graph <- TestData %>%
ggplot(aes(x = Quarter_year, y = n))+
geom_bar(stat = "identity") +
coord_flip()+
theme_minimal()
Animated_Graph <- Graph+
transition_time(Quarter_year)+
ggtitle("Test: {frame_time}")
animate(Animated_Graph)
Using the great package gganimate I want to set my frame time based on a dates quarter.
However, when I pass through a transition time, the animation creates a frame for each day between quarters, even though they are not in the dataset
transition_time(Quarter_year)+
ggtitle("Test: {frame_time}")
Is it possible to keep transition using only dates that appear in the dataset?
Thanks.

ggplot2 overlayed line chart by year?

Starting with the following dataset:
$ Orders,Year,Date
1608052.2,2019,2019-08-02
1385858.4,2018,2018-07-27
1223593.3,2019,2019-07-25
1200356.5,2018,2018-01-20
1198226.3,2019,2019-07-15
837866.1,2019,2019-07-02
Trying to make a similar format as:
with the criteria: X-axis will be days or months, y-axis will be sum of Orders, grouping / colors will be by year.
Attempts:
1) No overlay
dataset %>%
ggplot( aes(x=`Merge Date`, y=`$ Orders`, group=`Merge Date (Year)`, color=`Merge Date (Year)`)) +
geom_line()
2) ggplot month grouping
dataset %>%
mutate(Date = as.Date(`Date`) %>%
mutate(Year = format(Date,'%Y')) %>%
mutate(Month = format(Date,'%b')) -> dataset2
ggplot(data=dataset2, aes(x=Month, y=`$ Orders`, group=Year, color=factor(Year))) +
geom_line(size=.75) +
ylab("Volume")
The lubridate package is your answer. Extract month from the Date field and turn it into a variable. This code worked for me:
library(tidyverse)
library(lubridate)
dataset <- read_delim("OrderValue,Year,Date\n1608052.2,2019,2019-08-02\n1385858.4,2018,2018-07-27\n1223593.3,2019,2019-07-25\n1200356.5,2018,2018-01-20\n1198226.3,2019,2019-07-15\n837866.1,2019,2019-07-02", delim = ",")
dataset <- dataset %>%
mutate(theMonth = month(Date))
ggplot(dataset, aes(x = as.factor(theMonth), y = OrderValue, group = as.factor(Year), color = as.factor(Year))) +
geom_line()

Getting Cumulative Sum Over Time

I have data with goals scored for each player each season:
playerID <- c(1,2,3,1,2,3,1,2,3,1,2,3)
year <- c(2002,2000,2000,2003,2001,2001,2000,2002,2002,2001,2003,2003)
goals <- c(25,21,27,31,39,34,42,44,46,59,55,53)
my_data <- data.frame(playerID, year, goals)
I would like to plot each player's cumulative number of goals over time:
ggplot(my_data, aes(x=year, y=cumsum_goals, group=playerID)) + geom_line()
I have tried using summarize from dplyr, but this only works if the data is already sorted by year (see player 1):
new_data <- my_data %>%
group_by(playerID) %>%
mutate(cumsum_goals=cumsum(goals))
Is there a way to make this code robust to data where years are not in chronological order?
We can arrange by playerID and year, take cumsum and then plot
library(dplyr)
library(ggplot2)
my_data %>%
arrange(playerID, year) %>%
group_by(playerID) %>%
mutate(cumsum_goals=cumsum(goals)) %>%
ggplot() + aes(x=year, y= cumsum_goals, color = factor(playerID)) + geom_line()

Having trouble converting variable to factor for ggplot

I am trying to plot data from the nycflights13 data set. I want month and dep_delay variable to be factors rather than continuous. I am getting a error with no explanation and am stuck. Here's my code:
library(ggplot2)
library(dplyr)
library(nycflights13)
f <- group_by(flights, month) %>%
summarise(delay = mean(dep_delay, na.rm = TRUE)) %>%
ggplot(mutate(month = as.factor(unlist(month))) +
geom_bar(aes(month, delay, fill=month),stat = "identity")
You can't do the mutate inside the ggplot call like that. It does not get properly parsed inside, as the ggplot call gets the data, but cannot carry out the mutate step.
Do it in an outside call:
f <- group_by(flights, month) %>%
summarise(delay = mean(dep_delay, na.rm = TRUE)) %>%
mutate(month = as.factor(month)) %>%
ggplot() +
geom_bar(aes(month, delay, fill=month),stat = "identity")

Resources