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

Add doc example of replacing tick labels with custom strings #7045

Closed
4 tasks done
joelostblom opened this issue Nov 17, 2020 · 2 comments · Fixed by #7049
Closed
4 tasks done

Add doc example of replacing tick labels with custom strings #7045

joelostblom opened this issue Nov 17, 2020 · 2 comments · Fixed by #7049

Comments

@joelostblom
Copy link
Contributor

Occasionally it is useful to att a string label to explain numerical tick values on an axis while still using the numerical value for calculating an average, e.g. in ratings or survey responses. I couldn't find an example of this in the docs, and only after much tinkering I was able to use labelExpr and tickCounts to figure out a way to do this. I am not sure if this is the most convenient way and maybe I missed a more convenient approach. One thing I have not been able to figure out is how to set an exact number of ticks instead of it being rounded to a nice value, but would you consider adding an example similar to this in the docs (I can PR if desired)?

image

Open the Chart in the Vega Editor

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json",
  "data": {
    "name": "data-145f5f62f285527e098239bd826c1a90"
  },
  "datasets": {
    "data-145f5f62f285527e098239bd826c1a90": [
      {
        "IMDB Rating": 5.4,
        "Major Genre": "Action"
      },
      {
        "IMDB Rating": 6.2,
        "Major Genre": "Action"
      },
      {
        "IMDB Rating": 7.8,
        "Major Genre": "Action"
      },
      {
        "IMDB Rating": 5.5,
        "Major Genre": "Comedy"
      },
      {
        "IMDB Rating": 5.5,
        "Major Genre": "Comedy"
      },
      {
        "IMDB Rating": 7.3,
        "Major Genre": "Comedy"
      },
      {
        "IMDB Rating": 5.3,
        "Major Genre": "Action"
      },
      {
        "IMDB Rating": 5.4,
        "Major Genre": "Action"
      },
      {
        "IMDB Rating": 8.9,
        "Major Genre": "Action"
      },
      {
        "IMDB Rating": 8.0,
        "Major Genre": "Action"
      }
    ]
  },
  "encoding": {
    "x": {
      "aggregate": "mean",
      "axis": {
        "labelExpr": "datum.label == 0 ? 'Poor' : datum.label == 5 ? 'Neutral' : 'Great'",
        "labelFlush": false,
        "tickCount": 3
      },
      "field": "IMDB Rating",
      "scale": {
        "domain": [
          0,
          10
        ]
      },
      "title": "",
      "type": "quantitative"
    },
    "y": {
      "field": "Major Genre",
      "sort": "x",
      "title": "",
      "type": "nominal"
    }
  },
  "mark": {
    "size": 80,
    "type": "circle"
  }
}
  • Check for duplicate issues. Please file separate requests as separate issues on GitHub.
  • Describe the feature's goal, motivating use cases, and its expected behavior.
  • If you are proposing a new syntax, please provide at least one example spec, wrapped by triple backticks like this:
  • If applicable, include screenshots, GIF videos (e.g. using https://www.cockos.com/licecap/), or working example (e.g. example Vega specification for the requested feature)
@domoritz
Copy link
Member

I like the example. Here is a slight cleanup (Open the Chart in the Vega Editor):

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {
    "url": "data/movies.json"
  },
  "mark": {"size": 80, "type": "circle"},
  "encoding": {
    "x": {
      "aggregate": "mean",
      "axis": {
        "labelExpr": "datum.label == 0 ? 'Poor' : datum.label == 5 ? 'Neutral' : 'Great'",
        "labelFlush": false,
        "values": [0, 5, 10]
      },
      "field": "IMDB Rating",
      "scale": {"domain": [0, 10]},
      "title": null
    },
    "y": {"field": "Major Genre", "sort": "x", "title": null}
  }
}

Yes, please send a pull request!

@joelostblom
Copy link
Contributor Author

Great, thank you for the cleanup @domoritz ! I opened PR #7047

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