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

Geom.label positioning with stacked bar plot #1547

Closed
tomyun opened this issue Aug 4, 2021 · 3 comments
Closed

Geom.label positioning with stacked bar plot #1547

tomyun opened this issue Aug 4, 2021 · 3 comments

Comments

@tomyun
Copy link

tomyun commented Aug 4, 2021

using DataFrames
using Gadfly

df = DataFrame(
    x = [:A, :A, :A, :B, :B, :B],
    y = [0.2, 0.3, 0.5, 0.1, 0.6, 0.3],
    l = ["0.2", "0.3", "0.5", "0.1", "0.6", "0.3"],
    c = [:a, :b, :c, :a, :b, :c],
)
plot(df, x = :x, y = :y, color = :c, label = :l, Geom.label, Geom.bar(position = :stack))

label

Couldn't find a way to get correct position for labels in stacked bar plot.

@Mattriks
Copy link
Member

Mattriks commented Aug 5, 2021

The trick here is to use Stat.dodge. Note for Geoms+Stats outside layers order matters.
I also had to add Scale.x_discrete, not sure why and I should investigate.

p = plot(df, x=:x, y=:y, color=:c, label=:l, 
    Geom.label(position=:centered), Stat.dodge(position=:stack),
    Geom.bar(position=:stack), Scale.x_discrete
)

issue1547

@tomyun
Copy link
Author

tomyun commented Aug 5, 2021

Stat.dodge with Scale.x_discrete worked great for me. Thanks for the tip!

@tomyun tomyun closed this as completed Aug 5, 2021
@Aina29
Copy link

Aina29 commented Dec 12, 2021

@Mattriks how to do it when using Geom.subplot_grid()?

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

No branches or pull requests

3 participants