In this post, an overview will be provided of how to add images to a chart using the echarts4r package
Published
March 11, 2023
Introduction
Charts4r is a powerful R package that offers a variety of graphing options for data visualization. This blog post focuses on creating a line chart with images overlaid on data points. The weight records by date will be used as the data source for this tutorial. To insert images into the chart, we will need to provide the paths from where they can be obtained. If the chart is being hosted online, the images must be accessible to all users. Otherwise, local paths can be used if the chart is being created locally.
The following objects are masked from 'package:base':
date, intersect, setdiff, union
library(imputeTS) #NA Handeling
Warning: package 'imputeTS' was built under R version 4.1.2
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
###---Folder where images are storedIMG_Folder <-'./images/echarts4r-demo/No-BG'IMG_Folder_Website <-paste0('image://https://www.patrickboyce.com/r-projects/', gsub('\\./', '', IMG_Folder), '/')###---Load Data Body_Weight_Records <-read.csv('./data/wlp/weight-by-date.csv') Body_Weight_Records <- Body_Weight_Records %>%mutate(Date =mdy(Date)) Photo_Records <-tibble(File_Name =list.files(IMG_Folder)) %>%mutate(Week =as.Date(substr(File_Name, 1, 10)),Echart_Path =paste0(IMG_Folder_Website, File_Name))###---Example of datahead(Body_Weight_Records) %>% knitr::kable()
Date
Weight
2012-12-01
280
2012-12-08
278
2012-12-15
276
2012-12-22
274
2012-12-29
272
2013-01-05
270
Data Preparation
Data will be aggregated by week, then, if an image exists for that week, it’s added.
###---Aggregates the data by weekBody_Weight_By_Week <- Body_Weight_Records %>%group_by(Week =floor_date(Date, unit ='week')) %>%summarise(`Body Weight`=mean(Weight))###---Creates a sequence of all weeks from the start to the end of the body weight by weekAll_Weeks <-tibble('Week'=seq.Date(min(Body_Weight_By_Week$Week), max(Body_Weight_By_Week$Week), by ='weeks'))###---Imputes the weight values for any weeks for which the data was missingBody_Weight_By_Week <- All_Weeks %>%left_join(Body_Weight_By_Week, by ='Week') %>%na_ma(k =2, weighting ='simple') ###---Adds the assosciated image path to each week if there is oneBody_Weight_By_Week <- Body_Weight_By_Week %>%left_join(Photo_Records, by ='Week') %>%#If no image is avaialble, passing "None" will stop echarts4r from plotting itmutate(Echart_Path =if_else(is.na(Echart_Path), 'none', Echart_Path)) %>%mutate(`Body Weight`=round(`Body Weight`, 1))###---Final dataBody_Weight_By_Week %>%slice_head(n=5) %>% knitr::kable()