binscatter(df::Union{DataFrame, GroupedDataFrame}, f::FormulaTerm, n::Integer = 20; 
            weights::Union{Symbol, Nothing} = nothing, seriestype::Symbol = :scatter,

Outputs a binned scatterplot


  • df: A Table, a DataFrame or a GroupedDataFrame
  • f: A formula created using @formula. The variable(s) in the left-hand side are plotted on the y-axis. The first variable in the right-hand side is plotted on the x-axis. Add other variables for controls.
  • n: Number of bins (default to 20).

Keyword arguments

  • weights: A symbol for weights
  • seriestype: :scatter (the default) plots bins, :scatterpath adds a line to connect the bins, :linearfit adds a regression line (requires Plots 1.12)
  • kwargs...: Additional attributes for plot.


using DataFrames, Binscatters, RDatasets, Plots
df = dataset("plm", "Cigar")
binscatter(df, @formula(Sales ~ Price))

# Change the number of bins
binscatter(df, @formula(Sales ~ Price), 10)

# Residualize w.r.t. controls
binscatter(df, @formula(Sales ~ Price + NDI))
binscatter(df, @formula(Sales ~ Price + fe(Year)))

# Plot multiple variables on the y-axis
binscatter(df, @formula(Sales + NDI ~ Price))

# Plot binscatters within groups
df.Post70 = df.Year .>= 70
binscatter(groupby(df, :Post70), @formula(Sales ~ Price))

# Use keyword argument from [`plot'](@ref) to customize the plot:
binscatter(df, @formula(SepalLength ~ SepalWidth), msc = :auto)
binscatter(df, @formula(SepalLength ~ SepalWidth), seriestype = :scatterpath, linecolor = :blue, markercolor = :red)