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