我想在这个情节中做出小回报更加明显.最合适的功能似乎是scale_colour_gradient2
,但这会消除最常发生的小回报.使用limits
帮助但我无法弄清楚如何设置oob(越界)所以它只有一个"饱和"值而不是灰色.日志转换只是让小值突出.还有其他人想出如何优雅地做到这一点吗?
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)
Henrik.. 8
这是另一种可能性,使用scale_colour_gradientn
.colours
使用设置映射values = rescale(...)
使得对于接近零的值,分辨率更高.我在这里看了一些色标:http://colorbrewer2.org.我选择了一种5级分歧颜色方案RdBu,从红色到蓝色通过近白色.可能有更适合您需求的其他尺度,这只是为了展示基本原则.
# 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")
使用rescale
,-10对应蓝色#0571B0; -1 =浅蓝色#92C5DE; 0 =浅灰色#F7F7F7; 1 =浅红色#F4A582; 10 =红色#CA0020.介于-1和1之间的值在浅蓝色和浅红色之间插入,等等.因此,映射不是线性的,并且对于小值,分辨率更高.
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())
这是另一种可能性,使用scale_colour_gradientn
.colours
使用设置映射values = rescale(...)
使得对于接近零的值,分辨率更高.我在这里看了一些色标:http://colorbrewer2.org.我选择了一种5级分歧颜色方案RdBu,从红色到蓝色通过近白色.可能有更适合您需求的其他尺度,这只是为了展示基本原则.
# 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")
使用rescale
,-10对应蓝色#0571B0; -1 =浅蓝色#92C5DE; 0 =浅灰色#F7F7F7; 1 =浅红色#F4A582; 10 =红色#CA0020.介于-1和1之间的值在浅蓝色和浅红色之间插入,等等.因此,映射不是线性的,并且对于小值,分辨率更高.
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())