Increase resolution of color scale for values close to zero

后端 未结 2 2037
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 17:47

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 retur

相关标签:
2条回答
  • 2020-12-01 18:06

    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")
    

    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())
    

    enter image description here

    0 讨论(0)
  • 2020-12-01 18:19

    how about:

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

    or

    p + scale_colour_gradient2(low="blue",high="red",mid="darkgrey")
    
    0 讨论(0)
提交回复
热议问题