# Moving Average
```elixir
Mix.install([
{:explorer, "~> 0.3.1"},
{:vega_lite, "~> 0.1.5"},
{:kino_vega_lite, "~> 0.1.2"}
])
```
## Prepare Data
```elixir
alias Explorer.DataFrame
alias Explorer.Series
df =
Explorer.Datasets.fossil_fuels()
|> DataFrame.filter_with(&Series.equal(&1["country"], "DEMOCRATIC PEOPLE S REPUBLIC OF KOREA"))
|> DataFrame.mutate_with(fn data ->
moving_average =
data[:total]
|> Series.window_mean(3)
%{moving_average: moving_average}
end)
```
## Chart
<!-- livebook:{"attrs":{"chart_title":"Moving Average","height":300,"layers":[{"chart_type":"bar","color_field":null,"color_field_aggregate":null,"color_field_type":null,"data_variable":"df","x_field":"year","x_field_aggregate":null,"x_field_type":null,"y_field":"total","y_field_aggregate":null,"y_field_type":"quantitative"},{"chart_type":"line","color_field":null,"color_field_aggregate":null,"color_field_type":null,"data_variable":"df","x_field":"year","x_field_aggregate":null,"x_field_type":null,"y_field":"moving_average","y_field_aggregate":null,"y_field_type":"quantitative"}],"vl_alias":"Elixir.VegaLite","width":800},"kind":"Elixir.KinoVegaLite.ChartCell","livebook_object":"smart_cell"} -->
```elixir
VegaLite.new(width: 800, height: 300, title: "Moving Average")
|> VegaLite.data_from_values(df, only: ["year", "total", "moving_average"])
|> VegaLite.layers([
VegaLite.new()
|> VegaLite.mark(:bar)
|> VegaLite.encode_field(:x, "year")
|> VegaLite.encode_field(:y, "total", type: :quantitative),
VegaLite.new()
|> VegaLite.mark(:line)
|> VegaLite.encode_field(:x, "year")
|> VegaLite.encode_field(:y, "moving_average", type: :quantitative)
])
```