Bar Charts & Histograms

Bar Charts & Histograms

Simple Bar Chart

using VegaLite, DataFrames

data = DataFrame(
    a=["A","B","C","D","E","F","G","H","I"],
    b=[28,55,43,91,81,53,19,87,52]
)

data |> @vlplot(:bar, x="a:o", y=:b)
ABCDEFGHIa020406080100b

Histogram

using VegaLite, VegaDatasets

dataset("movies") |>
@vlplot(:bar, x={:IMDB_Rating, bin=true}, y="count()")
12345678910IMDB_Rating (binned)02004006008001,000Number of Records

Aggregate Bar Chart

using VegaLite, VegaDatasets

dataset("population") |>
@vlplot(
    :bar,
    transform=[{filter="datum.year == 2000"}],
    y={"age:o", scale={rangeStep=17}},
    x={"sum(people)", axis={title="population"}}
)
05,000,00010,000,00015,000,00020,000,000population051015202530354045505560657075808590age

Grouped Bar Chart

using VegaLite, VegaDatasets

dataset("population") |>
@vlplot(
    :bar,
    transform=[
        {filter="datum.year == 2000"},
        {calculate="datum.sex == 2 ? 'Female' : 'Male'", as="gender"}
    ],
    enc={
        column="age:o",
        y={"sum(people)", axis={title="population", grid=false}},
        x={"gender:n", scale={rangeStep=12}, axis={title=""}},
        color={"gender:n", scale={range=["#EA98D2", "#659CCA"]}},
    },
    config={
        view={stroke=:transparent},
        axis={domainWidth=1}
    }
)
age02,000,0004,000,0006,000,0008,000,00010,000,00012,000,000population051015202530354045505560657075808590FemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMaleFemaleMalegender

Stacked Bar Chart

using VegaLite, VegaDatasets

dataset("seattle-weather") |>
@vlplot(
    :bar,
    x={"month(date):o", axis={title="Month of the year"}},
    y="count()",
    color={
        :weather,
        scale={
            domain=["sun","fog","drizzle","rain","snow"],
            range=["#e7ba52","#c7c7c7","#aec7e8","#1f77b4","#9467bd"]
        },
        legend={
            title="Weather type"
        }
    }
)
sunfogdrizzlerainsnowWeather typeJanFebMarAprMayJunJulAugSepOctNovDecMonth of the year020406080100120Number of Records

Horizontal Stacked Bar Chart

using VegaLite, VegaDatasets

dataset("barley") |>
@vlplot(:bar, x="sum(yield)", y=:variety, color=:site)
CrookstonDuluthGrand RapidsMorrisUniversity FarmWasecasite0100200300400500Sum of yieldGlabronManchuriaNo. 457No. 462No. 475PeatlandSvansotaTrebiVelvetWisconsin No. 38variety

Normalized Stacked Bar Chart

using VegaLite, VegaDatasets

dataset("population") |>
@vlplot(
    :bar,
    transform=[
        {filter="datum.year == 2000"},
        {calculate="datum.sex==2 ? 'Female' : 'Male'",as="gender"}
    ],
    enc={
        y={
            "sum(people)",
            axis={title="population"},
            stack=:normalize
        },
        x={
            "age:o",
            scale={rangeStep=17}
        },
        color={
            "gender:n",
            scale={range=["#EA98D2", "#659CCA"]}
        }
    }
)
FemaleMalegender051015202530354045505560657075808590age0.00.20.40.60.81.0population

Gantt Chart (Ranged Bar Marks)

using VegaLite

@vlplot(
    :bar,
    data={
        values=[
            {task="A",start=1,stop=3},
            {task="B",start=3,stop=8},
            {task="C",start=8,stop=10}
        ]
    },
    enc={
        y="task:o",
        x="start:q",
        x2="stop:q"
    }
)
0246810start, stopABCtask

A bar chart encoding color names in the data

using VegaLite

@vlplot(
    :bar,
    data={
        values=[
            {color="red",b=28},
            {color="green",b=55},
            {color="blue",b=43}
        ]
    },
    x="color:n",
    y="b:q",
    color={"color:n",scale=nothing}
)
bluegreenredcolor01020304050b

Layered Bar Chart

using VegaLite, VegaDatasets

dataset("population") |>
@vlplot(
    :bar,
    transform=[
        {filter="datum.year==2000"},
        {calculate="datum.sex==2 ? 'Female' : 'Male'",as="gender"}
    ],
    enc={
        x={"age:o", scale={rangeStep=17}},
        y={"sum(people)", axis={title="population"}, stack=nothing},
        color={"gender:n", scale={range=["#e377c2", "#1f77b4"]}},
        opacity={value=0.7}
    }
)
FemaleMalegender051015202530354045505560657075808590age02,000,0004,000,0006,000,0008,000,00010,000,00012,000,000population

Diverging Stacked Bar Chart

using VegaLite, DataFrames

data = DataFrame(
    question=["Question $(div(i,5)+1)" for i in 0:39],
    typ=repeat(["Strongly disagree", "Disagree", "Neither agree nor disagree",
        "Agree", "Strongly agree"],outer=8),
    value=[24, 294, 594, 1927, 376, 2, 2, 0, 7, 11, 2, 0, 2, 4, 2, 0, 2, 1, 7,
        6, 0, 1, 3, 16, 4, 1, 1, 2, 9, 3, 0, 0, 1, 4, 0, 0, 0, 0, 0, 2],
    percentage=[0.7, 9.1, 18.5, 59.9, 11.7, 18.2, 18.2, 0, 63.6, 0, 20, 0, 20,
        40, 20, 0, 12.5, 6.3, 43.8, 37.5, 0, 4.2, 12.5, 66.7, 16.7, 6.3, 6.3,
        12.5, 56.3, 18.8, 0, 0, 20, 80, 0, 0, 0, 0, 0, 100],
    percentage_start=[-19.1, -18.4, -9.2, 9.2, 69.2, -36.4, -18.2, 0, 0, 63.6,
        -30, -10, -10, 10, 50, -15.6, -15.6, -3.1, 3.1, 46.9, -10.4, -10.4,
        -6.3, 6.3, 72.9, -18.8, -12.5, -6.3, 6.3, 62.5, -10, -10, -10, 10, 90,
        0, 0, 0, 0, 0],
    percentage_end=[-18.4, -9.2, 9.2, 69.2, 80.9, -18.2, 0, 0, 63.6, 63.6, -10,
        -10, 10, 50, 70, -15.6, -3.1, 3.1, 46.9, 84.4, -10.4, -6.3, 6.3, 72.9,
        89.6, -12.5, -6.3, 6.3, 62.5, 81.3, -10, -10, 10, 90, 90, 0, 0, 0, 0, 100]
)

data |> @vlplot(
    :bar,
    x={:percentage_start, axis={title="Percentage"}},
    x2=:percentage_end,
    y={
        :question, axis={
            title="Question",
            offset=5,
            ticks=false,
            minExtent=60,
            domain=false
        }
    },
    color={
        :typ,
        legend={title="Response"},
        scale={
            domain=[
                "Strongly disagree",
                "Disagree",
                "Neither agree nor disagree",
                "Agree",
                "Strongly agree"
            ],
            range=["#c30d24", "#f3a583", "#cccccc", "#94c6da", "#1770ab"],
            typ=:ordinal
        }
    }
)
Strongly disagreeDisagreeNeither agree nor …AgreeStrongly agreeResponse-40-20020406080100PercentageQuestion 1Question 2Question 3Question 4Question 5Question 6Question 7Question 8Question

Simple Bar Chart with Labels

using VegaLite

@vlplot(
    data={
        values=[
            {a="A",b=28},
            {a="B",b=55},
            {a="C",b=43}
        ]
    },
    y="a:o",
    x="b:q"
) +
@vlplot(:bar) +
@vlplot(
    mark={
        :text,
        align=:left,
        baseline=:middle,
        dx=3
    },
    enc={
        text="b:q"
    }
)
28554301020304050bABCa