juliaseabornscattergadfly

How to add point size to a Gadfly (Julia) scatter plot based on a variable


I am trying to reproduce this Seaborn plot using Gadfly.

The code I have so far is:

using CSV, DataFrames, Gadfly

download("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/mpg.csv", "mpg.csv");
mpg = DataFrame(CSV.File("mpg.csv"));

p = plot(
     mpg, 
     x = :horsepower, 
     y = :mpg, 
     color = :origin, 
     #size = :weight, 
     alpha = [0.5], 
     Geom.point,
     Theme(background_color = "white"),
     )

How do I add the size of the circles to match the car weight and add this as a second legend?

enter image description here


Solution

  • See detailed answer by Evan Fields on Discourse:

    p = plot(
        mpg,
        x = :horsepower,
        y = :mpg,
        color = :origin,
        size = :weight,
        alpha = [0.5],
        Geom.point,
        Theme(background_color = "white"),
        Scale.size_area(maxvalue=maximum(mpg.weight)),
    )
    

    enter image description here