【问题标题】:Vega-lite rolling averages within groups组内的 Vega-lite 滚动平均值
【发布时间】:2022-11-03 07:01:16
【问题描述】:

我有一个汇总价格与用户数量的数据表。当我想显示覆盖有移动平均线的散点图时,我使用以下 Julia 函数:

function log_scatter(df::DataFrame; smooth=2, title="Price by Number of Users")
    sort(select(df, [:Price, :Users]), :Users) |> 
    @vlplot(width=640,height=512, title=title) +
    @vlplot(mark={:point, opacity=0.5}, x={field=:Users, scale={type="log"},title="Users"}, y={:Price,title="Price per User"}) +
    @vlplot(transform=[
        { groupby=[:Users], aggregate=[{ op=:mean, field=:Price, as="AvgPrice" }] },
        { frame=[-smooth,smooth], window=[{ field="AvgPrice", op=:mean, as="rolling" }] }
        ],
        mark={:line,size=2,color="red"}, x={:Users, title="Users"}, y={"rolling:q", title="Average"})
end

它产生了一个很好的情节:

不幸的是,当我想对分组做同样的事情时,我无法显示移动平均线

function log_scatter_and(df::DataFrame, other; smooth=2, title="Price by Number of Users")
    otherSym=Symbol(other)
    prices = price_and(df, other)
    sort(select(prices, [:Price, :Users, otherSym]), :Users) |> 
    @vlplot(width=640,height=512, title=title) +
    @vlplot(mark={:point, opacity=0.5}, color=otherSym, x={field=:Users, scale={type="log"},title="Users"}, y={:Price,title="Price per User"}) +
    @vlplot(transform=[
        { groupby=[:Users, otherSym], aggregate=[{ op=:mean, field=:Price, as="AvgPrice" }] },
        { frame=[-smooth,smooth], window=[{ field="AvgPrice", op=:mean, as="rolling" }] }
        ],
        mark={:line,size=2,color=otherSym}, x={:Users, title="Users"}, y={"rolling:q", title="Average"})
end

这是我尝试按年份分组时的输出

我希望显示滚动平均线以及散点图

【问题讨论】:

    标签: julia vega-lite


    【解决方案1】:

    我不擅长VegaLite,但我遇到了StatisticalGraphics,我猜它使用vegalite,你。你需要添加主分支

    using InMemoryDatasets
    using StatisticalGraphics
    using RollingFunctions
    
    ds=Dataset(y=rand(1000), x=rand(1:200,1000),year=rand(2000:2010,1000))
    sort!(ds, :x)
    modify!(groupby(ds,:year), :y=>(x->runmean(x,2))=>:rolling)
    sgplot(ds, [Line(x=:x,y=:rolling, group=:year), Scatter(x=:x,y=:y,group=:year)], nominal=:year, xaxis=Axis(type=:log))
    

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 2021-04-17
      • 2014-11-29
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多