增加接近于零的值的色标分辨率 [英] Increase resolution of color scale for values close to zero
问题描述
我想让这个图中的小回报更加明显.最合适的函数似乎是 scale_colour_gradient2
,但这会消除最常发生的小回报.使用 limits
有帮助,但我不知道如何设置 oob(越界),所以它只会有一个饱和"值而不是灰色.而对数变换只是让小值脱颖而出.有没有其他人想出如何优雅地做到这一点?
I'd like to make small returns in this plot more visible. The most appropriate function seems to be scale_colour_gradient2
, but this washes out the small returns, which happen most often. Using limits
helped but I couldn't work out how to set oob (out of bounds) so it would just have a "saturated" value rather than be grey. And the log transform just made small values stand out. Has someone 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)
推荐答案
这是另一种可能性,使用 scale_colour_gradientn
.colours
的映射是使用 values = rescale(...)
设置的,因此对于接近于零的值,分辨率更高.我在这里查看了一些色阶:http://colorbrewer2.org.我选择了一个 5 级发散配色方案,RdBu,从红色到蓝色通过近白色.可能还有其他更适合您需求的秤,这只是为了展示基本原理.
Here is another possibility, using scale_colour_gradientn
. Mapping of colours
is set using values = rescale(...)
so that resolution is higher for values close to zero. I had a look at some colour scales here: http://colorbrewer2.org. I chose a 5-class diverging colour scheme, RdBu, from red to blue via near-white. There might be other scales that suit your needs better, this is 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")
使用rescale
,-10对应蓝色#0571B0;-1 = 浅蓝色#92C5DE;0 = 浅灰色#F7F7F7;1 = 浅红色#F4A582;10 = 红色 #CA0020.-1 和 1 之间的值在浅蓝色和浅红色之间插值,等等.因此,映射不是线性的,并且对于小值分辨率更高.
Using rescale
, -10 corresponds to blue #0571B0; -1 = light blue #92C5DE; 0 = light grey #F7F7F7; 1 = light red #F4A582; 10 = red #CA0020. Values between -1 and 1 are interpolated between light blue and light red, et c. Thus, mapping is not linear and resolution is higher for small values.
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())
这篇关于增加接近于零的值的色标分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!