带有高图的条形图中的R渐变颜色图例

时间:2017-09-29 08:57:08

标签: r highcharts shiny

使用R中的库高级图,我想重用热图的默认图例(具有某些值的渐变颜色),但是使用条形图。 怎么可能?

非常感谢

此致 萨姆

1 个答案:

答案 0 :(得分:1)

是的,就是这样。第一个数字是我想要的,第二个是我所做的。两个轴是倒置的,但并不重要。现在,我想画出两个传说:

  1. 一个颜色渐变,对应于圆圈的颜色(我的数据框示例中的插槽'颜色')
  2. 如果圆圈(值在数据框示例的插槽'Count'中),则具有不同大小的一个
  3. What I would like to draw

    What I have done

    我用于示例的数据帧如下:

    SO <- data.frame(Description=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"), 
                 Ratio = c(0.22, 0.14, 0.14, 0.14, 0.14, 0.10, 0.10, 0.08, 0.06, 0.06, 0.06), 
                 Color = c(31, 20, 20, 13, 13, 12,  7, 13,  7,  6,  5), 
                 Count = c(11,  7,  7,  7,  7,  5,  5,  4,  3,  3,  3))
    

    我为第二个图写的函数如下:

    nRes <- nrow(SO)
    SO <- SO[order(SO$Ratio, decreasing=TRUE),]
    SO <- SO[seq(1:nRes),]
    
    colfunc <- colorRampPalette(c("red","royalblue"))
    nbColors <- 5
    
    pal <- colfunc(nbColors)
    t <- SO$Color
    d <- (max(t) - min(t))/nbColors
    base <- seq(from=min(t), to=max(t), by = d)
    tmpList <- lapply(t, function(x){
    if (x == min(t)){ ind <- 1}
    else {ind <- which(x > base)[length(which(x > base))]}
    })
    
    myColorsIndex <- unlist(tmpList)
    
    df <- data.frame(x=c(0:(nRes-1)),
                   y=SO$Ratio,
                   z=SO$Count,
                   color=pal[myColorsIndex],
                   colorSegment=pal[myColorsIndex],
                   Color = SO$Color,
                   name = SO$Description)
    
    
    highchart() %>%
    hc_chart(type = "bubble") %>%
    hc_add_series(df) %>%
    hc_legend(enabled = FALSE) %>%
    hc_xAxis(type = "category", categories = df$name)  %>%
    hc_yAxis(title = list(text = "Ratio"))  
    

    感谢您的帮助