Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alpha for Geom.line and Geom.path #1589

Closed
arnold-c opened this issue Jun 3, 2022 · 8 comments · Fixed by #1603
Closed

Alpha for Geom.line and Geom.path #1589

arnold-c opened this issue Jun 3, 2022 · 8 comments · Fixed by #1603

Comments

@arnold-c
Copy link

arnold-c commented Jun 3, 2022

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])
)
@Rapsodia86
Copy link

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))
)


@arnold-c
Copy link
Author

arnold-c commented Jun 3, 2022

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")
)

@Rapsodia86
Copy link

Rapsodia86 commented Jun 6, 2022

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.

@arnold-c
Copy link
Author

arnold-c commented Jun 6, 2022

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

@Rapsodia86
Copy link

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

@Brinkhuis
Copy link

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

@Rapsodia86
Copy link

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

@bjarthur
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants