Last active
August 14, 2022 22:55
-
-
Save helgasoft/488d98f82a74297262dca3450c6397ba to your computer and use it in GitHub Desktop.
ECharts | R | jitter values; renaming grouped variable; replace axis labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (elegant) ggplot2 code to replicate in echarty | |
library(ggplot2) | |
iris |> | |
ggplot(aes(x = Species, y = Petal.Length)) + | |
geom_jitter(width = 0.1) | |
library(echarty); library(dplyr) | |
# with categorical axis | |
iris |> group_by(Species) |> ec.init(xAxis= list(type='category')) |> | |
ec.upd({ | |
series <- lapply(series, function(s) { | |
s$encode=list(x='Species', y='Petal.Length'); s }) | |
}) | |
# with jittered values and formatted value axis | |
iris |> mutate(Species=as.numeric(Species)) |> ec.init( | |
yAxis= list(name= 'Petal.Length'), | |
series= list(list(type= 'scatter', encode= list(x='Species', y='Petal.Length'))) | |
) |> ec.upd({ | |
dataset[[1]]$source <- lapply(dataset[[1]]$source, function(xx) { | |
if (is.character(xx)) return(xx) | |
as.list(jitter(unlist(xx), amount=0.15)) | |
}) | |
cats <- paste0("['','",paste0(attributes(iris$Species)$levels, collapse="','"),"']") | |
jcode <- paste0("function(val) { return (val % 1)==0 ? ",cats,"[val] : '';}") | |
xAxis <- list(type= 'value', name= 'Species', | |
axisTick= list(show= FALSE), | |
axisLabel= list(formatter= htmlwidgets::JS(jcode)) | |
) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chart with multiple axes, based on experiments by @XR97
If you like this solution, please consider granting a Github star ⭐ to echarty.