Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active August 14, 2022 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgasoft/488d98f82a74297262dca3450c6397ba to your computer and use it in GitHub Desktop.
Save helgasoft/488d98f82a74297262dca3450c6397ba to your computer and use it in GitHub Desktop.
ECharts | R | jitter values; renaming grouped variable; replace axis labels
# (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))
)
})
@helgasoft
Copy link
Author

helgasoft commented Nov 8, 2021

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.

image

For more advanced jitter - see the Giant Pumpkins 🎃🎃.

@christophsax
Copy link

Thanks a lot! Looks super interesting!

@helgasoft
Copy link
Author

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

image

@helgasoft
Copy link
Author

helgasoft commented Apr 19, 2022

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

image
If you like this solution, please consider granting a Github star ⭐ to echarty.

@helgasoft
Copy link
Author

helgasoft commented Apr 20, 2022

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

image
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