Small change in value is invisible with scale_colour_gradient2

I would like to make the small impact in this plot more visible. The most suitable function seems to be scale_colour_gradient2 , but it does away with the small gains that occur most often. Using limits helped, but I couldn't figure out how to set oob (out of bounds), so it would only have a "saturated" value, not a gray one. And the log conversion just made the small values ​​stand out. Has anyone else figured out how to do this elegantly?

 library(zoo) library(ggplot2) library(tseries) spx <- get.hist.quote(instrument="^gspc", start="2000-01-01", end="2013-12-14", quote="AdjClose", provider="yahoo", origin="1970-01-01", compression="d", retclass="zoo") spx.rtn <- diff(log(spx$AdjClose)) * 100 rtn.data <- data.frame(x=time(spx.rtn),yend=spx.rtn) p <- ggplot(rtn.data) + geom_segment(aes(x=x,xend=x,y=0,yend=yend,colour=yend)) + xlab("") + ylab("S&P 500 Daily Return %") + theme(legend.position="null",axis.title.x=element_blank()) # low returns invisible p + scale_colour_gradient2(low="blue",high="red") # extreme values are grey p + scale_colour_gradient2(low="blue",high="red",limits=c(-3,3)) # log transform returns has opposite problem max_val <- max(log(abs(spx.rtn))) values <- seq(-max_val, max_val, length = 11) library(RColorBrewer) p + scale_colour_gradientn(colours = brewer_pal(type="div",pal="RdBu")(11), values = values , rescaler = function(x, ...) sign(x)*log(abs(x)), oob = identity) 
+2
source share
2 answers

Here is another possibility using scale_colour_gradientn . The colours display is set using values = rescale(...) , so the resolution is higher for values ​​close to zero. I looked at some color scales here: http://colorbrewer2.org . I chose a diverging color scheme with 5 classes, RdBu, from red-blue to almost white. There may be other scales that best suit your needs, it's just to show the basic principles.

 # check the colours library(RColorBrewer) # cols <- brewer_pal(pal = "RdBu")(5) # not valid in 1.1-2 cols <- brewer.pal(n = 5, name = "RdBu") cols # [1] "#CA0020" "#F4A582" "#F7F7F7" "#92C5DE" "#0571B0" # show_col(cols) # not valid in 1.1-2 display.brewer.pal(n = 5, name = "RdBu") 

Using rescale , -10 matches the blue # 0571B0; -1 = cyan # 92C5DE; 0 = light gray # F7F7F7; 1 = light red # F4A582; 10 = red # CA0020. Values ​​between -1 and 1 are interpolated between light blue and light red, etc. Thus, the display is not linear, and the resolution for small values ​​is higher.

 library(ggplot2) library(scales) # needed for rescale ggplot(rtn.data) + geom_segment(aes(x = x, xend = x, y = 0, yend = yend, colour = yend)) + xlab("") + ylab("S&P 500 Daily Return %") + scale_colour_gradientn(colours = cols, values = rescale(c(-10, -1, 0, 1, 10)), guide = "colorbar", limits=c(-10, 10)) + theme(legend.position = "null", axis.title.x = element_blank()) 

enter image description here

+7
source

What about:

 p + scale_colour_gradient2(low="blue",high="red",mid="purple") 

or

 p + scale_colour_gradient2(low="blue",high="red",mid="darkgrey") 
0
source

All Articles