Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coloring by a temporal field doesn't use the full range of colors available #9286

Open
1 task done
nicolaskruchten opened this issue Mar 15, 2024 · 5 comments
Open
1 task done
Labels

Comments

@nicolaskruchten
Copy link

Bug Description

This spec (editor link) shows that Vega-Lite (Vega?) doesn't seem to use the full range of the plasma color scheme when coloring by a temporal field which is a shame:

CleanShot 2024-03-15 at 15 54 06@2x

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Stock prices of 5 Tech Companies over Time.",
  "data": {"url": "data/stocks.csv"},
  "mark": {"type": "point"},
  "encoding": {
    "x": {"timeUnit": "year", "field": "date"},
    "y": {"aggregate": "mean", "field": "price", "type": "quantitative"},
    "color": {
      "field": "date",
      "type": "temporal",
      "scale": {"scheme": "plasma"}
    }
  }
}

If applicable, include error messages and screenshots, videos, or working examples (e.g. by clicking share in the Vega-Editor.

Checklist

  • I checked for duplicate issues.
@PBI-David
Copy link
Contributor

This might be a Vega issue. Here is a corrected Vega spec changing the type to scale type to linear and the legend formatType to time.

image



{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "Stock prices of 5 Tech Companies over Time.",
  "background": "white",
  "padding": 5,
  "width": 200,
  "height": 200,
  "style": "cell",
  "data": [
    {
      "name": "source_0",
      "url": "data/stocks.csv",
      "format": {"type": "csv", "parse": {"date": "date"}, "delimiter": ","},
      "transform": [
        {
          "field": "date",
          "type": "timeunit",
          "units": ["year"],
          "as": ["year_date", "year_date_end"]
        },
        {
          "type": "aggregate",
          "groupby": ["year_date", "date"],
          "ops": ["mean"],
          "fields": ["price"],
          "as": ["mean_price"]
        },
        {
          "type": "filter",
          "expr": "(isDate(datum[\"date\"]) || (isValid(datum[\"date\"]) && isFinite(+datum[\"date\"]))) && isValid(datum[\"mean_price\"]) && isFinite(+datum[\"mean_price\"])"
        }
      ]
    }
  ],
  "marks": [
    {
      "name": "marks",
      "type": "symbol",
      "style": ["point"],
      "from": {"data": "source_0"},
      "encode": {
        "update": {
          "fill": {"value": "transparent"},
          "stroke": {"scale": "color", "field": "date"},
          "ariaRoleDescription": {"value": "point"},
          "description": {
            "signal": "\"date (year): \" + (timeFormat(datum[\"year_date\"], timeUnitSpecifier([\"year\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"}))) + \"; Mean of price: \" + (format(datum[\"mean_price\"], \"\")) + \"; date: \" + (timeFormat(datum[\"date\"], '%b %d, %Y'))"
          },
          "x": {"scale": "x", "field": "year_date"},
          "y": {"scale": "y", "field": "mean_price"}
        }
      }
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "time",
      "domain": {"data": "source_0", "field": "year_date"},
      "range": [0, {"signal": "width"}]
    },
    {
      "name": "y",
      "type": "linear",
      "domain": {"data": "source_0", "field": "mean_price"},
      "range": [{"signal": "height"}, 0],
      "nice": true,
      "zero": true
    },
    {
      "name": "color",
      "type": "linear",
      "domain": {"data": "source_0", "field": "date" },
      "range": {"scheme": "plasma"}
    }
  ],
  "axes": [
    {
      "scale": "x",
      "orient": "bottom",
      "gridScale": "y",
      "grid": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "tickMinStep": {
        "signal": "datetime(2002, 0, 1, 0, 0, 0, 0) - datetime(2001, 0, 1, 0, 0, 0, 0)"
      },
      "domain": false,
      "labels": false,
      "aria": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "gridScale": "x",
      "grid": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "domain": false,
      "labels": false,
      "aria": false,
      "maxExtent": 0,
      "minExtent": 0,
      "ticks": false,
      "zindex": 0
    },
    {
      "scale": "x",
      "orient": "bottom",
      "grid": false,
      "title": "date (year)",
      "format": {
        "signal": "timeUnitSpecifier([\"year\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})"
      },
      "labelFlush": true,
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(width/40)"},
      "tickMinStep": {
        "signal": "datetime(2002, 0, 1, 0, 0, 0, 0) - datetime(2001, 0, 1, 0, 0, 0, 0)"
      },
      "zindex": 0
    },
    {
      "scale": "y",
      "orient": "left",
      "grid": false,
      "title": "Mean of price",
      "labelOverlap": true,
      "tickCount": {"signal": "ceil(height/40)"},
      "zindex": 0
    }
  ],
  "legends": [
    {
      "stroke": "color",
      "format": "%b %d, %Y",  "formatType":"time",
      
      "title": "date"
    }
  ]
}

@nicolaskruchten
Copy link
Author

Thanks! But if Vega is able to do this, it seems like Vega-Lite could be modified/fixed to output this kind of thing, right?

OTOH, looking at the compiled Vega for my example, it seems like it's just getting the range wrong.

@PBI-David
Copy link
Contributor

Yes, here is the VL. The reason I said I thought it was a Vega issue is the range is being computed incorrectly by Vega when the type is time.

image

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Stock prices of 5 Tech Companies over Time.",
  "data": {"url": "data/stocks.csv"},
  "mark": {"type": "point"},
  "encoding": {
    "x": {"timeUnit": "year", "field": "date"},
    "y": {"aggregate": "mean", "field": "price", "type": "quantitative"},
    "color": {
      "field": "date",
      "type": "quantitative",
      "scale": {"scheme": "plasma"}, "legend":{"formatType": "time"}
    }
  }
}

@nicolaskruchten
Copy link
Author

Ah I see, thanks for that. Should I file a bug in the Vega repo?

@PBI-David
Copy link
Contributor

I would do as they might know why the range is being calculated incorrectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants