Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put a line break in a Vega-lite axis title?

Tags:

vega-lite

I'm new to Vega-lite and I'm trying to figure out if line breaks are possible in axis titles. I have a long axis titles, e.g.:

"Long axis title is too long to fit under the graph"

I've tried:

"Long axis title is too\n long to fit under the graph" and
"Long axis title is too\
long to fit under the graph"

The "\n" doesn't seem to do anything. The "\[enter]" just adds extra space to the line.

My x and y encoding looks like this:

encoding: {
      x: {field: 'a',
          type: 'ordinal',
          sort: {"encoding": "x"},  
          axis: {"title": "Knowledge of the elder\
          categories would melt\
          your psyche",
          "titleFontSize": 30,
          }
          },
      y: {field: 'b', 
          type: 'quantitative',
          axis: {"title": "Your puny mortal mind\ncannot comprehend the units\nof the multiverse!",
          "titleFontSize": 14,
          }
          }
    }

I'm not getting error messages, but I'm not getting line breaks either. I either get no change (from \n), or weird spacing (from [enter]).

Thank you!

like image 512
Thomas Stow Avatar asked Jul 19 '19 16:07

Thomas Stow


1 Answers

In Vega-Lite 4.0 or newer, multiline text can be specified in titles by passing an array of strings. For example:

{"data": {
    "values": [
      {"a": "A", "b": 28},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": 91},
      {"a": "E", "b": 81},
      {"a": "F", "b": 53},
      {"a": "G", "b": 19},
      {"a": "H", "b": 87},
      {"a": "I", "b": 52}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {
      "field": "a",
      "type": "ordinal",
      "title": ["First line of title", "second line of title"]
    },
    "y": {"field": "b", "type": "quantitative"}
  }
}

enter image description here

like image 158
jakevdp Avatar answered Oct 26 '22 23:10

jakevdp