Skip to content

Presidential Opinion ​

Opinion poll data on historical U.S. presidents. Image marks are used to show presidential pictures. The dropdown menu toggles the opinion metric shown.

Loading Example... ⏳

Credit: Adapted from an Observable Plot example.

Specification ​

js
import * as vg from "@uwdata/vgplot";

await vg.coordinator().exec([
  vg.loadParquet("presidents", "data/us-president-favorability.parquet")
]);

const $sign = vg.Param.value(1);

export default vg.vconcat(
  vg.plot(
    vg.ruleY([0]),
    vg.image(
      vg.from("presidents"),
      {
        x: "First Inauguration Date",
        y: vg.sql`"Very Favorable %" + "Somewhat Favorable %" + ${$sign} * ("Very Unfavorable %" + "Somewhat Unfavorable %")`,
        src: "Portrait URL",
        r: 20,
        preserveAspectRatio: "xMidYMin slice",
        title: "Name"
      }
    ),
    vg.xInset(20),
    vg.xLabel("First inauguration date →"),
    vg.yInsetTop(4),
    vg.yGrid(true),
    vg.yLabel("↑ Opinion (%)"),
    vg.yTickFormat("+f")
  ),
  vg.menu({
    label: "Opinion Metric",
    options: [{label: "Any Opinion", value: 1}, {label: "Net Favorability", value: -1}],
    as: $sign
  })
);
yaml
meta:
  title: Presidential Opinion
  description: >
    Opinion poll data on historical U.S. presidents.
    Image marks are used to show presidential pictures.
    The dropdown menu toggles the opinion metric shown.
  credit: Adapted from an [Observable Plot example](https://observablehq.com/@observablehq/plot-image-medals).
data:
  presidents: { file: data/us-president-favorability.parquet }
params:
  sign: 1
vconcat:
- plot:
  - mark: ruleY
    data: [0]
  - mark: image
    data: { from: presidents }
    x: "First Inauguration Date"
    y:
      sql: '"Very Favorable %" + "Somewhat Favorable %" + $sign * ("Very Unfavorable %" + "Somewhat Unfavorable %")'
    src: "Portrait URL"
    r: 20
    preserveAspectRatio: "xMidYMin slice"
    title: "Name"
  xInset: 20
  xLabel: First inauguration date →
  yInsetTop: 4
  yGrid: true
  yLabel: ↑ Opinion (%)
  yTickFormat: +f
- input: menu
  label: Opinion Metric
  options:
    - { label: "Any Opinion", value: 1 }
    - { label: "Net Favorability", value: -1 }
  as: $sign
json
{
  "meta": {
    "title": "Presidential Opinion",
    "description": "Opinion poll data on historical U.S. presidents. Image marks are used to show presidential pictures. The dropdown menu toggles the opinion metric shown.\n",
    "credit": "Adapted from an [Observable Plot example](https://observablehq.com/@observablehq/plot-image-medals)."
  },
  "data": {
    "presidents": {
      "file": "data/us-president-favorability.parquet"
    }
  },
  "params": {
    "sign": 1
  },
  "vconcat": [
    {
      "plot": [
        {
          "mark": "ruleY",
          "data": [
            0
          ]
        },
        {
          "mark": "image",
          "data": {
            "from": "presidents"
          },
          "x": "First Inauguration Date",
          "y": {
            "sql": "\"Very Favorable %\" + \"Somewhat Favorable %\" + $sign * (\"Very Unfavorable %\" + \"Somewhat Unfavorable %\")"
          },
          "src": "Portrait URL",
          "r": 20,
          "preserveAspectRatio": "xMidYMin slice",
          "title": "Name"
        }
      ],
      "xInset": 20,
      "xLabel": "First inauguration date →",
      "yInsetTop": 4,
      "yGrid": true,
      "yLabel": "↑ Opinion (%)",
      "yTickFormat": "+f"
    },
    {
      "input": "menu",
      "label": "Opinion Metric",
      "options": [
        {
          "label": "Any Opinion",
          "value": 1
        },
        {
          "label": "Net Favorability",
          "value": -1
        }
      ],
      "as": "$sign"
    }
  ]
}
py
import vgplot as vg

presidents = vg.parquet("data/us-president-favorability.parquet")

sign = vg.param(1)

view = vg.vconcat(
    vg.plot(
        vg.rule_y(data=[0]),
        vg.image(
            presidents,
            x="First Inauguration Date",
            y=vg.sql(
                '"Very Favorable %" + "Somewhat Favorable %" + $sign * ("Very Unfavorable %" + "Somewhat Unfavorable %")'
            ),
            src="Portrait URL",
            r=20,
            preserve_aspect_ratio="xMidYMin slice",
            title="Name",
        ),
        vg.x_inset(20),
        vg.x_label("First inauguration date →"),
        vg.y_inset_top(4),
        vg.y_grid(True),
        vg.y_label("↑ Opinion (%)"),
        vg.y_tick_format("+f"),
    ),
    vg.menu(
        label="Opinion Metric",
        options=[
            vg.option("Any Opinion", value=1),
            vg.option("Net Favorability", value=-1),
        ],
        bind=sign,
    ),
)