javascriptrchartsvisualizationecharts4r

Is there a way to create a heat map with multiple colour scales in R?


I am using the echarts4R library in R to create an interactive heat map (see image attached). The data is structured as below:

league_name attribute value
Bundesliga pace 70
Bundesliga shooting 60
Bundesliga passing 64
Bundesliga dribbling 70
Bundesliga defending 62
Bundesliga physic 71
La Liga pace 70
La Liga shooting 62
La Liga passing 66
La Liga dribbling 70

Here is the code I used, where player_att_plot is the data frame with the structure above:

player_att_plot %>% 
e_charts(league_name) %>%
 e_heatmap(attribute,value, itemStyle = list(emphasis = list(shadowBlur = 10))) %>% 
e_visual_map(value,inRange = list(color=c("white","green"))) 

Basically I want the heat map to have a color scale specific to each attribute, as opposed to the whole heat map.Heatmap with echarts4R

I tried splitting the data frame and plot into separate series and then plotting, but was unsuccessful. There might be another package that might be more suited for the example, any guidance on this would be greatly appreciated!


Solution

  • The usual approach here would be to use the alpha aesthetic:

    library(ggplot2)
    
    ggplot(df, aes(league_name, attribute, fill = attribute, alpha = value)) +
      geom_tile(color = "gray") +
      theme_minimal(base_size = 16) +
      coord_cartesian(expand = FALSE) +
      theme(panel.grid = element_blank()) +
      ggsci::scale_fill_startrek() +
      guides(fill = "none")
    

    enter image description here

    Obviously you can use whichever fill palette you prefer.


    Data used

    set.seed(1)
    
    df <- data.frame(league_name = rep(c("Bundesliga", "La Liga", "Ligue 1",
                                         "Premier League", "Serie A"), each = 6),
                     attribute = factor(rep(c("pace", "shooting", "passing", 
                                            "dribbling", "defending", "physic"), 5),
                                        c("pace", "shooting", "passing", 
                                          "dribbling", "defending", "physic")),
                     value = sample(59:72, 30, TRUE))