-
-
Save helgasoft/e914fe6f4a9ed9407f78e41cb18b3aaa to your computer and use it in GitHub Desktop.
#' 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)) | |
) |
Wahoo! I am impressed. that is exactly what I was looking for, thank you both for your support
Very much appreciated!
I am also done with my final code thanks to your advice
my main chart looks like that
but one a small screen, it would be more convenient to have the labels inside the pie to view the entire labels
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!
There is API setting pie.label.position and default value is 'outside'. So just add position='inside'
in label.
perfect. thank you!
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.
@antoine4ucsd, please open an issue in echarty with a working code example
sure. thank you
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')
Yes, my bad. it works fine now. thank you
@antoine4ucsd, having two charts in Shiny is easy, click pie sector - get bar.