简体   繁体   中英

R gradient color legend in bar plot with highcharter

With the library highcharter in R, I would like to reuse the default legend of the heatmap (a gradient color with some value) but with a bar plot. How is it possible ?

Many thanks

Regards Sam

Yes, here it is. The first figure is what I want and the second one is what I have done. The two axis are inverted but it is not important. Now, I would like to draw the two legends :

  1. one with a gradient of color which corresponds to the colors of the circles (slot 'Color' in my dataframe example)
  2. one with the different sizes if circles (value are in slot 'Count' of the dataframe example)

What I would like to draw

What I have done

The dataframe I used for the example is the following :

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

the function I wrote for that second plot is the following :

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

Thanks for any help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM