Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@helgasoft
helgasoft / app.R
Last active January 23, 2024 22:41
Coding Helper for echarty
#'----------- echarty coding helper ------------
#' Please report bugs and ask questions in https://github.com/helgasoft/echarty/issues
#' run in RStudio with command demo(coder), or demo(coder, package='echarty')
stopifnot('session non-interactive'= interactive())
library(shiny)
library(shinyjs)
library(shinybusy)
library(dplyr)
@helgasoft
helgasoft / dset.2.series.R
Created September 26, 2023 23:47
R | ECharts | dataset columns referenced in two series
set.seed(22)
data <- tibble(x = 2 * pi * (1:100)/100) |>
mutate(
y1_bar = 0.5 * (sin(x)) + 1,
y2_bar = 0.5 * (sin(x)) - 1,
deltaY1 = rnorm(100, sd = 0.1),
deltaY2 = rnorm(100, sd = 0.1),
y1 = y1_bar + deltaY1,
y2 = y2_bar + deltaY2,
sizeY1 = abs(deltaY1),
@helgasoft
helgasoft / notes
Last active April 5, 2024 22:14
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,
@helgasoft
helgasoft / svg.timeline.R
Last active January 22, 2023 14:22
R | ECharts | custom SVG map with timeline
library(echarty)
url <- 'https://test.softpower.tw/echartstw/example/svg/football.svg'
svg <- url |> readLines(encoding='UTF-8') |> paste0(collapse="")
p <- ec.init(preset=FALSE,
backgroundColor= '#228b22',
geo= list("map"="football"),
title= list(text ='2014 World Cup', textStyle=list( color='#FFFF00'),
subtext='original JS code', subtextStyle= list(color='#FFFF00'),
sublink='https://test.softpower.tw/echartstw/example/map19.html'
),
@helgasoft
helgasoft / rasta.leaf.R
Created December 22, 2022 02:00
Raster overlay on Leaflet map
# Expanding on the great code from Milos Popovic
# new display as layer on Leaflet map, instead of ggplot
# original: https://github.com/milos-agathon/mapping-raster-files-with-terra-in-r
################################################################################
# Mapping OSM and satelitte data with terra in R
# Milos Popovic
# 2022/09/22
################################################################################
@helgasoft
helgasoft / diamonds.noanim.R
Last active June 27, 2022 18:41
R | ECharts | large scatter without animation; loess, lm interpolation
library(ggplot2) # for diamonds data =56K recs
library(dplyr)
data <- diamonds |> select(price, carat, cut) |> group_by(cut)
library(echarty)
p <- data |>
ec.init( # series & dataset are preset here
dataZoom= list(type='inside'),
tooltip= list(show= TRUE))
p$x$opts$series <- lapply(p$x$opts$series, function(s) {
@helgasoft
helgasoft / pumpkins.boxplot.R
Created December 14, 2021 02:07
R | ECharts | boxplot + scatter overlay with vertical layout
#' inspired by https://juliasilge.com/blog/giant-pumpkins/
#' advantage over ggplot is interactivity - zoom brush and tooltips
#' horizontal layout code at https://helgasoft.github.io/echarty/gallery.html#boxplot
library(tidyverse)
pumpkins_raw <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-19/pumpkins.csv")
pumpkins <-
pumpkins_raw |>
separate(id, into = c("year", "type")) |>
mutate(across(c(year, weight_lbs), parse_number)) |>
@helgasoft
helgasoft / area.item.style.R
Last active April 8, 2022 22:06
R | ECharts | areaStyle and itemStyle, click event
library(dplyr)
library(lubridate)
# add event for clicking on blank area
jscode <- c('','',"chart.getZr().on('click', function(e) {
if (!e.target) alert('blank area: '+JSON.stringify(e.event)); });")
set.seed(421)
df1 <- tibble(date= as_date("2021-11-01") + months(0:11),
x= 12*10^6 + rnorm(12,0,2000000),
y= 12*10^6 + rnorm(12,0,1000000))
@helgasoft
helgasoft / rich.colors.R
Last active February 19, 2022 18:08
R | ECharts | logo, title and background, CORS issues
library(echarty)
radial_gradient <- htmlwidgets::JS(
"new echarts.graphic.RadialGradient(0.3, 0.3, 0.8)"
# ,[{ offset: 0, color: 'red'}, { offset: 1, color: 'red'}])"
)
color_stops <- htmlwidgets::JS(
"[{ offset: 0, color:'gold' }, // color at 0%
{ offset: 0.5, color:'MediumSlateBlue' }, // color at 50%
{ offset: 1, color:'skyblue' // color at 100%
}]"
@helgasoft
helgasoft / jitter.categorical.R
Last active August 14, 2022 22:55
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