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 hidden or 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)) | |
) | |
}) | |
Thanks a lot! Looks super interesting!
inquiry by @Jorge-hercas on renaming grouped variable
library(echarty); library(dplyr)
p <- iris |> mutate(Species = recode(Species,
setosa= "ex1", versicolor= "ex2", virginica= "ex3")) |>
group_by(Species) |> ec.init()
p$x$opts$legend <- list(show=TRUE)
p
Get axis labels from another data column by using an invisible second axis, inquiry by @XR97
df <- data.frame( # echarty default columns 1st=X, 2nd=Y
x_value = c('a', 'b', 'c', 'd', 'e', 'f', 'g'),
y = 8:14,
x_label = c('A', 'A', 'A', 'D', 'D', 'M', 'A')
)
library(echarty) # >= v.1.4.6.04
df |> ec.init() |> ec.upd({
series <- list(list(type= 'line', encode= list(x= 'x_value', y= 'y'), xAxisIndex= 1))
xAxis <- list(
list(data= df$x_label),
list(data= df$x_value, show=FALSE )
)
})
If you like this solution, please consider granting a Github star ⭐ to echarty.
chart with multiple axes, based on experiments by @XR97
df <- data.frame( # echarty default columns are 1st=X, 2nd=Y
x_value = c('a', 'b', 'c', 'd', 'e', 'f', 'g'),
y = 8:14,
x_label = c('A', 'A', 'A', 'D', 'D', 'M', 'A'),
z = 18:24
)
library(echarty) # >= v.1.4.6.04
df |> ec.init(ctype= 'line') |> ec.upd({
xAxis <- list(
list(data= df$x_value ), # x_value
list(data= df$x_label ) # x_label
)
yAxis <- list(
list(show= TRUE), # y
list(data= df$z) # z
)
})
If you like this solution, please consider granting a Github star ⭐ to echarty.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good question @christophsax - how to jitter categorical values in ECharts ?
The short answer is cant do that. There is no way of jittering 'setosa'.
But R and ECharts being so flexible, there are hacks of course. Here is one that plays with X-axis formatting.
For more advanced jitter - see the Giant Pumpkins 🎃🎃.