Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active July 17, 2024 20:33
Show Gist options
  • Save helgasoft/e914fe6f4a9ed9407f78e41cb18b3aaa to your computer and use it in GitHub Desktop.
Save helgasoft/e914fe6f4a9ed9407f78e41cb18b3aaa to your computer and use it in GitHub Desktop.
echarty announcements and temporary notes
#' This gist is for echarty announcements and notes
#' Comments are temporary, periodically deleted.
#' If you like echarty, please consider granting a Github star ⭐.
library(echarty)
data.frame(x = seq.int(1, 5, 1),
y = seq.int(1, 10, 2)) |>
ec.init(
series.param= list(
type='line', symbolSize= 10,
lineStyle = list(opacity= 0.7),
itemStyle = list(opacity= 1),
areaStyle= list(color= 'red', opacity= 0.5))
)
@helgasoft
Copy link
Author

helgasoft commented Feb 6, 2024

echarty is on WebR - see the Coder.
Live R-code execution inside a single web page. No Rmd. No server. No installation. Wow!
Thanks to: @seanbirchall for the idea and @timelyportfolio for the solution 💐👑

@helgasoft
Copy link
Author

@antoine4ucsd, having two charts in Shiny is easy, click pie sector - get bar.

library(echarty); library(dplyr); library(shiny)
df <-  data.frame(
  tx_o2.factor = c("yes", "no", "yes", "no", "yes", "yes"),
  value = c(5, 3, 4, 2, 6, 1)
) |> summarise(n = n(), .by = tx_o2.factor) |>
  dplyr::mutate(
          N = sum(n),
          pct = round(n / N, 2),
          lab_pct = str_c(round(pct * 100), "%"),
          lab_n = str_c(n, " / ", N)
  )

ui <- fluidPage( ecs.output("chart"), ecs.output("chart2") )
server <- function(input, output) {
  output$chart <- ecs.render({
    df |> ec.init(
      tooltip= list(formatter= ec.clmn('name')),
      series.param= list(type='pie', radius= c("40%", "70%"), encode=list(value='pct'),
                         label= list(show=T, formatter=ec.clmn('%@ %@','tx_o2.factor','lab_pct')) )
    )
  })
  observeEvent(input$chart_click, {
    output$chart2 <- ecs.render({ 
      ll <- as.list(input$chart_click); ymax <- ll$data[[3]];
      df |> filter(tx_o2.factor==ll$data[[1]]) |> 
      ec.init( title=list(text= ll$data[[1]]), color= c('gold'), yAxis= list(type='value', max= ymax),
               series.param= list(type='bar', encode= list(x=1, y=2)) )
    })
  })
}
shinyApp(ui = ui, server = server)

@antoine4ucsd
Copy link

Wahoo! I am impressed. that is exactly what I was looking for, thank you both for your support
Very much appreciated!

@antoine4ucsd
Copy link

I am also done with my final code thanks to your advice
my main chart looks like that

image

but one a small screen, it would be more convenient to have the labels inside the pie to view the entire labels
image

current code

reactive_data() |> ec.init(
                        tooltip= list(formatter= ec.clmn('name')),
                        series.param= list(type='pie', #radius= c("40%", "70%"),
                                           encode=list(value='pct'),
                                           label= list(show=T, formatter=ec.clmn('%@ %@','group','lab_pct')) )
                )|> ec.theme('dark')

thank you again. your support saved me a lot of time!

@helgasoft
Copy link
Author

There is API setting pie.label.position and default value is 'outside'. So just add position='inside' in label.

@antoine4ucsd
Copy link

perfect. thank you!

@antoine4ucsd
Copy link

may I ask one more question...
creating a lollipop chart,

 ecs.render({ 
       data|>dplyr::rename(name=severity_4cat)|>
                dplyr::summarise(n=n(),.by=name)|>
                dplyr::mutate( N=sum(n),
                              pct = round(n / N,2),
                              lab_pct = str_c(round(pct * 100), "%"),
                              lab_n = str_c(n, " / ", N))|>
                dplyr::select(name,n)|>
                dplyr::mutate(name=factor(name,levels=c("Mild","Moderate","Severe","Critical")))|> # reordering?
                ec.init(ctype='bar'
                        ,grid= list(containLabel=TRUE)
                        ,xAxis= list(name=' ', 
                                     axisLabel= list(rotate= 66), scale=TRUE,
                                     axisTick= list(alignWithLabel= TRUE))
                        ,yAxis= list(name='count', nameLocation='center', nameRotate=90, nameGap=20)
                ) |> 
                ec.upd({
                        scat <- list()
                        series <- lapply(series, function(bar) { 
                                ss <- bar      # set matching scatter serie
                                ss <- within(ss, {
                                        type <- 'scatter'
                                        encode <- list(x='name', y='n')
                                        label <- list(show=TRUE, formatter= '{@n}')
                                        symbolSize <- 25
                                        itemStyle <- list(opacity= 1, borderWidth=2, borderColor= 'cornsilk')
                                })
                                scat <<- append(scat, list(ss))
                                bar$barWidth <- 3
                                bar$barGap <- '-100%'    # center it
                                bar })
                        series <- append(series, scat)
                }) %>% ec.theme('inspired') 

it works just fine apart from the fact that the order of the data in the x axis does not match my factor
can't find how to properly reorder...sorry about that.

@helgasoft
Copy link
Author

@antoine4ucsd, please open an issue in echarty with a working code example

@antoine4ucsd
Copy link

sure. thank you

@helgasoft
Copy link
Author

dplyr::arrange is typically used for reordering

df <-  data.frame(
  name = c("yes", "no", "yes", "no", "yes", "yes",'dn','dn','dn','dn'),
  value = c(5, 3, 4, 2, 6, 1, 8,4,2,1)
) |> dplyr::summarise(n= dplyr::n(), .by= name) |>
  dplyr::mutate(
          N = sum(n),
          pct = round(n / N, 2),
          lab_pct = str_c(round(pct * 100), "%"),
          lab_n = str_c(n, " / ", N)
  )

library(echarty)
df |> 
  dplyr::select(name,n) |> dplyr::arrange(n,name) |>   # reordering
ec.init(ctype='bar'
          ,grid= list(containLabel=TRUE)
          ,xAxis= list(name=' ', 
                       axisLabel= list(rotate= 66), scale=TRUE,
                       axisTick= list(alignWithLabel= TRUE))
          ,yAxis= list(name='count', nameLocation='center', nameRotate=90, nameGap=20)
          ,barWidth= 3, barGap= '-100%'
  ) |> 
ec.upd({
    scat <- lapply(series, function(bar) { 
      within(bar, { # set matching scatter serie
        type <- 'scatter'
        encode <- list(x='name', y='n')
        label <- list(show=TRUE, formatter= '{@n}')
        symbolSize <- 25
        itemStyle <- list(opacity= 1, borderWidth=2, borderColor= 'cornsilk')
      })
    })
    series <- append(series, scat)
})  # |> ec.theme('inspired') 

@antoine4ucsd
Copy link

Yes, my bad. it works fine now. thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment