1

The data I'm trying to visualize has two assessments performed on the same time scale, but at different intervals (i.e. Temperature taken 4 times over 12 hours, pain assessed every hour), as an example:

df <- data.frame(
  Hour = 0:12, 
  Pain = sample(7:10, 13, TRUE), 
  Temp = c(36.8,rep(NA,3),37.2,rep(NA,3),37.4,rep(NA,3),37.0)
)

In ggplot, I'd visualize it like this:

library(ggplot2)
ggplot(df) +
    geom_col(aes(x = Hour, y = Pain)) +
    geom_point(aes(x = Hour, y = Temp/3)) +
    geom_line(data = df[!is.na(df$Temp), ], aes(x = Hour, y = Temp/3)) +
    scale_y_continuous(sec.axis = sec_axis(~.*3,name = "Temp"))

In echarts4r however, I cannot get my line to be continuous (I believe because of the NA values)

library(echarts4r)
e_chart(df, Hour) |> e_bar(Pain) |> e_line(Temp)

Is there a way to subset the dataset before e_line to remove the missing values - I've searched online and can't seem to find anything? Or should I be structuring my data differently?

1 Answers1

0

Similar to the ggplot2 approach you could fix your issue by using a subset of non-missing values for your line which could be achieved via e_data like so:

library(echarts4r)

set.seed(123)

df |>
  e_chart(Hour) |> 
  e_bar(Pain) |> 
  e_data(subset(df, !is.na(Temp)), Hour) |>
  e_line(Temp)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51