Time series of plots using different colors based on coefficient

I would like to build a separate line that will be multi-colored, and the colors are based on the corresponding value in the coefficient. For example, a temporary series of daily stock closing prices, where the days when it has grown by more than a certain amount are blue and the days that it has done a lot are red, and on other days it is in boring black.

My data is in the xts object (with a coefficient placed there with as.numeric(myfactor) ), and I would like to use the functions quantmod chartSeries or chart_Series . But if this is not possible, then just use plot .

Some sample data:

 library(xts) x = xts( data.frame( v=(rnorm(50)+10)*10, type=floor(runif(50)*4) ), order.by=as.Date("2001-01-01")+1:50) 

And I can build it like this:

 library(quantmod) chartSeries(x$v) addTA(x$type, type='p') 

Which looks like this: plot using chartSeries

those. I felt it would be easier to match the information in the bottom diagram with the top if colored line segments are used.

+5
source share
2 answers

So here is a ggplot . This is a bit more active, but offers more sophisticated formatting options.

 library(quantmod) sp500 <- getSymbols("^GSPC", from="2015-01-01", auto.assign=FALSE) sp500 <- Cl(sp500) # extract close sp500 <- merge(sp500, dailyReturn(sp500)) # add daily returns sp500 <- merge(lag(Cl(sp500),1), sp500) # merge with close lagged by 1 day names(sp500) <- c("ymin", "ymax", "return") library(ggplot2) library(scales) df <- with(sp500, data.frame(xmin=c(lag(index(sp500),1)), xmax=index(sp500), ymin, ymax, return)) df$status <- with(df,ifelse(return>0.01,"up",ifelse(return< -0.01,"down","neutral"))) ggplot(df) + geom_segment(aes(x=xmin, xend=xmax, y=ymin, yend=ymax, color=status)) + scale_color_manual(values=c(up="green", down="red", neutral="grey50"), breaks=c("up","down"), labels=c("Gain > 1%", "Loss > 1%")) + scale_x_date(breaks=date_breaks("months"), labels=date_format("%b"))+ labs(x=NULL, y="Closing Price", title="S&P 500") + theme(panel.background =element_rect(fill="black"), panel.grid = element_blank()) 

As with the other answer, the basic idea is to draw color-coded segments based on the magnitude of the gain / loss. Therefore, we start by extracting closing prices, adding a return column, and then adding another closed price column that is 1 day late. Then we create data.frame from this with two date columns also 1 day behind. Then add a ( status ) column to indicate if the gain / loss was> 1%. Then we use it to control geom_segment(...) , color-coded status . In the call to scale_color_manual(...) we set the colors to red and green and exclude the neutral color from the legend. The rest is all formatting.

+2
source

This code started as a minor mod for the example code on ?segments , and therefore the name of the chart looks strange, but I decided to leave it anyway. The logic is that the terms inside "[.]" Will "choose" colors based on the difference between consecutive values ​​formed as the difference between tail(y,-1) and head(y,-1) , with the default being " black ", and the threshold values ​​are 1 in this case, but this can be easily changed:

 set.seed(123) x <- 1:12; y <- rnorm(12) plot(x, y, main = "arrows(.) and segments(.)") s <- seq(length(x)-1) arrows(x[s], y[s], x[s+1], y[s+1], col= c("black", "red", "blue")[1+ # default=1 (tail(y,-1)-head(y,-1) < -1) + # big down (1+1) 2*(tail(y,-1)-head(y,-1) > 1) ] ) # big up (1+2) 

If you need only zero line segments, you can use the segments function rather than arrows .

enter image description here

I understand that I read the question again that you said that you already have a factor variable in your xts object, although I understand that xts objects may not be able to contain factor type columns, since they are a zoo class and coredata - it is an R-matrix (hence level attributes). But maybe the quantum guys have a workaround for this? So this will be another reason to post some data, and PLEASE use dput to represent the object. Creating xts objects from console output is a real problem.

+1
source

All Articles