# How to add point size to a Gadfly scatter plot based on a variable

**URL:** <https://discourse.julialang.org/t/how-to-add-point-size-to-a-gadfly-scatter-plot-based-on-a-variable/81111>\
**Category:** New to Julia\
**Tags:** plotting, gadfly\
**Created:** [May 15, 2022, 6:13pm UTC](https://discourse.julialang.org/t/how-to-add-point-size-to-a-gadfly-scatter-plot-based-on-a-variable/81111 "2022-05-15T18:13:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 15, 2022, 6:13pm UTC](https://discourse.julialang.org/t/how-to-add-point-size-to-a-gadfly-scatter-plot-based-on-a-variable/81111/1 "2022-05-15T18:13:32Z")

</div>

I am trying to reproduce [this](https://seaborn.pydata.org/examples/scatter_bubbles.html) 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](https://global.discourse-cdn.com/julialang/original/3X/1/3/13fc01478352f33232235e6ebbb4fa9a5e723ff7.png)](https://i.stack.imgur.com/QSdon.png)

---

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [May 15, 2022, 7:40pm UTC](https://discourse.julialang.org/t/how-to-add-point-size-to-a-gadfly-scatter-plot-based-on-a-variable/81111/2 "2022-05-15T19:40:00Z")

</div>

This part of the Gadfly documentation is pretty confusing, because Gadfly has several methods for setting sizes and the distinctions therein aren’t particularly explicitly laid out. In particular, size attributes can refer to (a) multiples of `Theme.point_size_{min or max}`, (b) physical distances like 2mm, (c) distances in plot-axis units.

You may have noticed that with your above snippet, but with the `#size` line not commented out, the plot looks nonsensical:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/c/ec2327cdc3be6c3f7c5beaf5535c27f57ad1a60e.png)

That’s because `extrema(mpg.weight) == (1613, 5140)`. The Gadfly docs on `Geom.point` aesthetic `size` state:

> `size` : Categorical data and vectors of `Ints` will interpolate between `Theme.point_size_{min,max}` . A continuous vector of `AbstractFloats` or `Measures` of length(x) specifies the size of each data point explicitly. A vector of length one specifies the size to use for all points. Default is `Theme.point_size`.

I’m not entirely sure how the interpolation between `Theme.point_size_{min,max}` works, but definitely larger integers in the weight attribute will give you larger circles. The above nonsense plot has a bunch of giant overlapping circles, and we’re “zoomed out” so far we can’t really see them all. Zoom in really far and you’ll see a reasonable plot.

Gadfly can size areas according to a _function_ of the data used for size, analogous to how when coloring points, a data attribute is passed through a function to get the color for each point. The relevant scale here is `Scale.size_area` (or radius, though probably you want area?).

```julia
 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)),
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/a/5ad6332f77da262309b3f6b298ff674c72b47047.png)

---

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [May 15, 2022, 8:13pm UTC](https://discourse.julialang.org/t/how-to-add-point-size-to-a-gadfly-scatter-plot-based-on-a-variable/81111/3 "2022-05-15T20:13:11Z")

</div>

> [@evanfields](#):
>
> ```julia
> 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)),
> )
> 
> ```

Thanks for your detailed answer, highly appreciated!
