Closed milesmedina closed 2 years ago
Yes, NAs do affect the display and lines are getting interrupted. But spacing on both axes is correct, I think.
test.df |>
e_charts(x= Date) |>
e_scatter(serie= Mean, symbolSize= 8) |>
e_line(serie= Trend10) |>
e_line(serie= Trend5) |>
e_error_bar(lower= lwr, upper= upr)
Hi helgasoft,
Thanks for your response. Because the x-axis is time (dates), I need the x-axis to be spaced temporally regardless of the spacing of data within the dataframe. The appropriate spacing can be achieved without calling e_error_bar()
: The Mean
and Trend10
points are spaced evenly, and therefore the Trend10
series appears as a straight line as it should:
e_charts(x= Date) |>
e_scatter(serie= Mean, symbolSize= 8) |>
e_line(serie= Trend10) |>
e_line(serie= Trend5)
I also notice that in your plot, the error bars are slightly offset to the right of the Mean
points, when they should line up on center.
2011 to 2020 ? that's not the same data. Could you provide the entire code please? " error bars are slightly offset to the right of the Mean points" - yes, that's how they are, kind of hard to fix.
Ah, yes, you're right. I changed the Date
series after the original post to more accurately reflect the data I'm actually working with, but it is essentially the same input and doesn't change the core issue. Here is the code with the revised dates:
library(tidyr); library(dplyr); library(echarts4r)
means <- data.frame( Date = seq.Date( as.Date('2011-01-01'),
as.Date('2020-12-31'),
by = 'year'),
Value = c(4,6,5,4,6,
8,7,5,7,6),
Type = rep("Mean",10)
)
means$lwr <- means$Value-1.5
means$upr <- means$Value+1.5
trend.10 <- data.frame( Date = seq.Date( as.Date('2011-01-01'),
as.Date('2020-12-31'),
length.out=50 ),
Value = seq(4.5,6.5,length.out=50),
Type = rep("Trend10",50) )
trend.5 <- data.frame( Date = seq.Date( as.Date('2016-01-01'),
as.Date('2020-12-31'),
length.out=50 ),
Value = seq(6.7,6.2,length.out=50),
Type = rep("Trend5",50) )
trends <- full_join( trend.5, trend.10, by = c("Date","Value","Type") )
test.df <- full_join( means, trends, by = c("Date","Value","Type") )
test.df <- test.df[ order(test.df$Date), ]
test.df <- pivot_wider( test.df, names_from=Type, values_from=Value )
test.df |>
e_charts(x= Date) |>
e_scatter(serie= Mean, symbolSize= 8) |>
e_line(serie= Trend10) |>
e_line(serie= Trend5) # |>
# e_error_bar(lower= lwr, upper= upr)
ok, understand the problem, but do not know how to fix it in echarts4r. Here is a solution with a similar library.
library(echarty)
tmp <- test.df |> relocate(Date,Mean) |> mutate(Date= as.character(Date))
ec.init(load='custom', preset=FALSE,
xAxis= list(type='time', data= tmp$Date, boundaryGap= c('5%','5%')),
yAxis= list(show=TRUE),
series= list(
list(type='scatter', name='Mean', data= ec.data(tmp |> select(Date,Mean) |> na.omit())),
list(type='line', name='Trend10', data= ec.data(tmp |> select(Date,Trend10) |> na.omit())),
list(type='line', name='Trend5', data= ec.data(tmp |> select(Date,Trend5) |> na.omit()))
)) |>
ecr.ebars(tmp)
If "|> na.omit()" is removed, the lines will get broken because of NAs.
Incredible! You're a lifesaver! Thanks so much!!!
Oops I just realized I did not provide the code with the revised dates above... sorry! And thanks again I've edited the previous post with the correct code
This could be the fix for echarts4r
test.df |>
e_charts(x= Date) |>
e_scatter(serie= Mean, symbolSize= 8) |>
e_line(serie= Trend10) |>
e_line(serie= Trend5) |>
e_error_bar(lower= lwr, upper= upr) |>
e_x_axis(type='time') # change X-axis from 'category' to 'time'
Thanks! I had tried this with the e_x_axis()
call closer to the top (didn't work), but it didn't occur to me to move it down below the e_error_bar()
call
Well, the _e_xaxis() call shouldn't even be there, but _e_errorbar improperly sets the chart type to 'category', so it's is a fix for that.
Hi John,
I've only just started working with eCharts4r this week, and it's awesome! I am however seeing some unexpected behavior when adding error bars to my chart with
e_error_bar()
.The code below specifies some made-up values and produces a chart with error bars.
If you run the code without the
e_error_bar()
call, the chart looks like this, with theMean
values appropriately spaced along the x-axis (Date
):Running the code with the
e_error_bar()
call, the chart displays error bars, but the spacing of theMean
values is different, with the first five values closer together than the last five:I am guessing that the spacing of
Mean
values is related to theNA
s in theMean
column: The first fiveMean
values are more spaced out byNA
s than the last five in the dataframe itself, due to the join with theTrend5
andTrend10
values. But I don't understand why this spacing in the dataframe would affect the spacing on the axis. This only seems to happen when there is ane_error_bar()
call. The x-axis spacing is not affected if, for instance, I instead calle_line()
to display one of the trend lines (e.g.%>% e_line( serie = Trend10, collapseNulls = TRUE )
).Any advice? Thanks!