Skip to content

Instantly share code, notes, and snippets.

@JohnCoene
Last active August 26, 2020 15:23
Show Gist options
  • Save JohnCoene/b795c92a532a264a9900959a1fa3ad57 to your computer and use it in GitHub Desktop.
Save JohnCoene/b795c92a532a264a9900959a1fa3ad57 to your computer and use it in GitHub Desktop.
Every echarts4r "geom" takes a `color` argument
library(shiny)
library(dplyr)
library(echarts4r)
color_it <- function(x){
switch(x,
setosa = "#5858A0",
versicolor = "#9cd156",
virginica = "#c99535"
)
}
# var to color mapping
my_colors <- tibble::tibble(
Species = c("setosa", "versicolor", "virginica"),
color = c("#5858A0", "#c99535", "#9cd156")
)
ui <- fluidPage(
selectizeInput(
"specie",
label = "Choose a specie",
choices = c("setosa", "versicolor", "virginica"),
multiple = TRUE
),
echarts4rOutput("chart")
)
server <- function(input, output){
output$chart <- renderEcharts4r({
# requires a specie selected
validate(
need(length(input$specie) > 0, message = "Select at least one specie")
)
# filter selected and match with color
dataset <- iris %>%
filter(Species %in% input$specie) %>%
left_join(my_colors, by = "Species")
# plot
dataset %>%
group_by(Species) %>% # group by specie so one specie = one serie
e_charts(Sepal.Length) %>%
e_scatter(Sepal.Width, symbol_size = 10) %>%
e_x_axis(min = 0) %>%
e_y_axis(min = 0, max = 6) %>%
e_color(unique(dataset$color)) # apply colors
})
}
shinyApp(ui, server)
library(shiny)
library(dplyr)
library(echarts4r)
color_it <- function(x){
switch(x,
setosa = "#5858A0",
versicolor = "#9cd156",
virginica = "#c99535"
)
}
ui <- fluidPage(
selectInput(
"specie",
label = "Choose a specie",
choices = c("setosa", "versicolor", "virginica")
),
echarts4rOutput("chart")
)
server <- function(input, output){
output$chart <- renderEcharts4r({
iris %>%
filter(Species == input$specie) %>%
e_charts(Sepal.Length) %>%
e_scatter(Sepal.Width, symbol_size = 10, color = color_it(input$specie)) %>%
e_x_axis(min = 0) %>%
e_y_axis(min = 0, max = 6)
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment