question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Smooth line in facet plot

See original GitHub issue

I’m trying to generate a faceted scatter plot with a line for the mean. This code works:

scatter = alt.Chart(data).mark_point().encode(
    x="Position",
    y="Value",
    color="Cluster",
)

average = alt.Chart(data).mark_line(color="black").encode(
    x="Position",
    y="mean(Value)")

combined = scatter + average

combined.facet("Cluster")

However, as my data is sparse in some areas, I get very spiky lines that don’t capture the trend well. My quick fix is to round the values of data.Position for the line, which works okay, but I’m wondering if there’s a clever way to make use of statsmodel’s lowess smooth? I can only see doing that in pandas, adding a new column with smooths per cluster.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Mar 29, 2020

Sorry, lost track of this. You are computing the running average of all points; I think you want to compute the running average within each group, which you can do by adding a groupby argument to your window transform.


combined_facet = (scatter + average).facet(
    "Cluster:N"
).transform_window(
    running_avg="mean(Value)",
    frame=[-10,10],
    sort=[alt.SortField("Position","ascending")],
    groupby=['Cluster']
).configure_mark(opacity=0.8)
0reactions
kuchenrollecommented, Oct 10, 2019

As I mentioned, I used the sort argument. For no sorting, however, can the average be higher than all data points.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ggplot2: Single smooth line for several line plots within facets
I want to plot multiple facets with several lines each. In every facet, for the set of line plots, I would like to...
Read more >
Smooth lines with geom_smooth() + Facets with facet_wrap() | R
5 minutes is enough to create a professional-looking and ready for publication chart. In this video i show how to add smoothing lines...
Read more >
Applying smooth to part of a facetted plot - Google Groups
I've got a facet_wrap() plot with ~42 facets. A few of those have only one data point per facet; the rest have between...
Read more >
How to Plot a Smooth Line using GGPlot2 - Datanovia
This article descrbes how to easily plot smooth line using the ggplot2 R package. You will learn how to add: regression line, smooth...
Read more >
How to Plot a Smooth Line using ggplot2 in R ? - GeeksforGeeks
We can plot a smooth line using the “loess” method of the geom_smooth() function. The only difference, in this case, is that we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found