1

When plotting a "stacked histogram", I would like the "stack order" to be the same as the legend order - Fair (first / bottom) and Ideal (last / top) - so that the colors are in order from light to dark. Like in this example.

Any idea how to do that? My code so far:

using CSV, DataFrames, Gadfly

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

palette = ["#9D95C2", "#B4ADCF", "#C9C4DB", "#DFDCE9", "#F5F3F4"]

plot(
    diamonds, 
    x = :price, 
    color = :cut, 
    Geom.histogram(bincount=50), 
    Scale.x_log10, 
    Scale.color_discrete_manual(palette..., order = [1, 2, 4, 3, 5]), 
    Theme(
        background_color = "white", 
        bar_highlight = color("black")
    ), 
)

enter image description here

René
  • 4,594
  • 5
  • 23
  • 52

1 Answers1

0

This is actually not that easy. There is a somewhat related question at How do I sort a bar chart in ascending or descending order in Julia's Gadfly? (Does anyone know a less hacky way?) I will probably try to fiddle a bit more, but what you can do is using levels on top of order.

Scale.color_discrete_manual(palette..., 
                            levels = ["Fair", "Good", "Very Good", "Premium", "Ideal"]
                            order = [1, 2, 4, 3, 5]), 

Then you have three lists, one for the colors one for the levels and one for the order. It is a nightmare, but there should be one permutation that looks similar to the seaborn example (I hope).

Zitzero
  • 78
  • 1
  • 3
  • So far it seems an impossible puzzle to solve. When I found the right order with a palette of high contract colours, changing the palette to the colours I need changes the "stack order" of the plot. – René Jun 06 '22 at 19:51
  • 1
    I see, I had the same problem trying to solve it but thought I didn't try every permutation. Maybe open an issue at Gadfly.jl? Somebody else surely will encounter the same problem at some point. – Zitzero Jun 07 '22 at 08:07
  • 1
    Done: https://github.com/GiovineItalia/Gadfly.jl/issues/1590 – René Jun 07 '22 at 17:18