GiovineItalia / Gadfly.jl

Crafty statistical graphics for Julia.

Home Page:http://gadflyjl.org/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alpha for Geom.line and Geom.path

arnold-c opened this issue · comments

Apologies if this is covered elsewhere, but I couldn't find it in the issues or the documentation. Is it possible to enable Theme(alphas = []) for Geom.line and Geom.path? It would be very helpful for situations where trajectories are simulated and are overlayed e.g. stochastic disease modeling. Small reprex below (alphas works as expected when the geom is changed to Geom.point, for example).

Thanks very much

test = DataFrame(
  x = 1:100,
  y = rand(100)
)

Gadfly.plot(
  test,
  x = :x,
  y = :y,
  Geom.line,
  Theme(alphas = [0.1])
)

This is not a direct solution you are asking for, but maybe will help.
See: https://discourse.julialang.org/t/gadfly-how-to-change-width-and-opacity-of-lines-in-geom-density/37823/2
Using Colors library, you can set a style:

using Gadfly
using Colors

test = DataFrame(
    x=1:100,
    y=rand(100)
)

Gadfly.plot(
    test,
    x=:x,
    y=:y,
    Geom.line, style(
        default_color=RGBA(0.1, 0.1, 0.9, 0.3))
)


Thanks @Rapsodia8. Sorry for the follow up - I should have made a better reprex. I have more than one group that produce the colors. From the documentation it seems like I could use plot(Scale.color_discrete_manual()) to set the colors, but I've only got it working when naming the colors. Is there a way to use RGBA() (and therefore specify the alphas)?

using Gadfly, Colors

test = DataFrame(
    x=1:100,
    y=rand(100),
    z = repeat(["a", "b"], outer = 50)
)

personal_theme = Theme(
  panel_fill="white",
  background_color = "white"
  )

Gadfly.plot(
    test,
    x=:x,
    y=:y,
    color = :z,
    Geom.line,
    personal_theme,
    Scale.color_discrete_manual("red", "blue")
)

I wish RGBA() was working within scale.color_discrete_manual()

   Gadfly.plot(
       test,
       x=:x,
       y=:y,
       color=:z,
       Geom.line,
       personal_theme,
       Scale.color_discrete_manual(RGBA(0.3, 0.1, 0.6, 0.3), RGBA(0.8, 0, 0.9, 0.2))
   )

ERROR: MethodError: no method matching color_discrete_manual(::RGBA{Float64}, ::RGBA{Float64})
Stacktrace:
[1] top-level scope

Sorry, cannot help with that.
Maybe, as a solution, you could plot each line as a separate layer? I know it might be tedious, though.

Thanks for your help. I was receiving the same error. I have updated the code to plot each series in a for loop. Code below in case it's of any help to others/please feel free to point out bad practices given my inexperience with Julia.

using Gadfly, Colors, DataFrames, DataFramesMeta

test = DataFrame(
    x = 1:100,
    y = rand(100),
    z = repeat(["a", "b"], outer = 50)
  )

test_colors = (
    a = RGBA(0.106,0.62,0.467, 0.5),
    b = RGBA(0.851,0.373,0.008, 0.5)
  )

personal_theme = Theme(
    panel_fill = "white",
    background_color = "white"
  )

test_plot = Gadfly.plot(
    personal_theme,
    Guide.manual_color_key(
        "State", ["a", "b"],
        [RGB(test_colors.a), RGB(test_colors.a)]
    )
  )

for state in collect(keys(test_colors))
    str_states = String(state)
    
    test_plot = push!(
        test_plot, 
        layer(
            @subset(test, :z .== str_states),
            x = :x,
            y = :y,
            Geom.line,
            style(default_color = test_colors[state])
        )
    )
end

test_plot

Thank you for the code. I hope 'alphas' will be working with Geom.line soon.

Using color(RGBA(.. , .. , ..)) seems to work (Julia 1.7.2 and Gadfly 1.3.4).

test = DataFrame(
    x=1:100,
    y=rand(100),
    z = repeat(["a", "b"], outer = 50)
)

personal_theme = Theme(
  panel_fill="white",
  background_color = "white"
  )

Gadfly.plot(
    test,
    x=:x,
    y=:y,
    color = :z,
    Geom.line,
    personal_theme,
    Scale.color_discrete_manual(color(RGBA(0.3, 0.1, 0.6, 0.3)), color(RGBA(0.8, 0, 0.9, 0.2)))
)

Screenshot 2022-06-07 at 19 30 12

True, does not throw the error, but also does not apply the transparency. color() just changes it from RGBA to RGB.

sorry for the delay in looking into this issue. i've pushed a PR which should fix it. can you three please checkout the branch and make sure it works for you?