Search code examples
powerbivisualizationpowerbi-desktopvega-litedeneb

How to fix y-axis after plotting y-variable with a log scale in Vega-Lite


I want to accomplish the following: plot my y-variable using a log scale with large values and have the y-axis adjust for these values. what's the problem: all the values are at the top of the graph and I don't know how to get them to automatically resize. the y-values are quite large so the log values are large if that's helpfulenter image description here

{
  "data": {"name": "dataset"},
    "transform": [
    {
      "filter": "datum.virus !== null" 
    },
  ],
  "layer": [
     {
      "mark": {
        "type": "point",
        "tooltip":true,
        "filled": true
      },
      "encoding": {
        "x": {
          "field": "date",
          "title": "Date",
          "type": "temporal"
        },
        "y": {
          "field": "virus",
          "title": "(Normalized Levels (N1/PMMOV Concentrations)",
          "type": "quantitative",
          **"scale":{"type":"log","base": 10, "nice": true}** need to log transform y-variable
        },
         "tooltip": [
          {
            "field": "virus",
            "type": "quantitative"
          },
          {
            "field": "date",
            "type": "temporal",
            "format": "%B %d %Y"
          },
          {
            "field":"siteno",
            "type":"nominal"
          },
          {
            "field": "virus_o",
            "type": "nominal"
          }
        ]
      }
  
    },
    {
      "name": "Regression Line",
      "transform": [
        {
          "loess": "virus",
          "on": "date",
          "bandwith":0.25
        }
      ],
      "mark": {
        "type": "line",
        "color": "black"
      },
      "encoding": {
        "x": {
          "field": "date",
          "type": "temporal",
            "axis": {
        "format": "MMMM yyyy",
        "formatType": "pbiFormat"
      }
        },
        "y": {
          "field": "virus",
          "type": "quantitative"
        }
      }
    }
    
    ]
}

Solution

  • Your loess was generating negative numbers which will break a log scale. Try this.

    enter image description here

    {
      "data": {"name": "dataset"},
      "transform": [
        {
          "filter": "datum['virus'] !== null"
        },
        {"filter": "datum['virus'] !== 0"}
      ],
      "encoding": {
        "x": {
          "field": "date",
          "title": "Date",
          "type": "temporal"
        },
        "y": {
          "field": "virus",
          "title": "(Normalized Levels (N1/PMMOV Concentrations)",
          "type": "quantitative",
          "scale": {"type": "log"}
        }
      },
      "layer": [
        {
          "mark": {
            "type": "point",
            "tooltip": true,
            "filled": true
          }
        },
        {
          "mark": {
            "type": "line",
            "color": "black"
          },
          "transform": [
            {
              "filter": "datum['virus'] != null"
            },
            {
              "filter": "datum['virus'] != 0"
            },
            {
              "loess": "virus",
              "on": "date",
              "as": ["a", "b"],
              "bandwidth": 0.3
            },
            {"filter": "datum['b'] >1"}
          ],
          "encoding": {
            "x": {"field": "a"},
            "y": {"field": "b"}
          }
        }
      ]
    }