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

histogram(density = true) and x_discrete(labels = ...) incompatible ? #1163

Closed
EliasBcd opened this issue Jun 12, 2018 · 5 comments · Fixed by #1236
Closed

histogram(density = true) and x_discrete(labels = ...) incompatible ? #1163

EliasBcd opened this issue Jun 12, 2018 · 5 comments · Fixed by #1236

Comments

@EliasBcd
Copy link

EliasBcd commented Jun 12, 2018

I am trying to plot a histogram while giving labels to each histogram bar. There seem to be an incompatibility between density = true and x_discrete(label = ...)

x = [1,1,1,1, 2,2,2,2,2]
lab = Dict(1 =>"one", 2 => "two")
Gadfly.plot(x = x, Scale.x_discrete(labels = x -> lab[x]), Geom.histogram(density = true))

On my computer, the density = true is ignored when I have x_discrete, but works perfectly without latter. It looks like a bug to me, or is it a normal behavior? Thanks!

@bjarthur
Copy link
Member

@tlnagy does #1157 change this behavior?

@AsileBcd this works for me:

plot(x = x,
    Scale.x_continuous(labels = x -> lab[x]),
    Geom.histogram(density = true),
    Guide.xticks(ticks=collect(keys(lab))))

@tlnagy
Copy link
Member

tlnagy commented Jun 12, 2018

#1157 won't touch this behavior. I don't fully understand this use case. @AsileBcd could you describe why you're trying to do this and not using Geom.bar?

@Mattriks
Copy link
Member

Another example that illustrates the issue. I think there is a case that discrete variables can be proportioned, what do people think?

x_cont = [1,1,1,1,2,2,2,2,2]
x_disc = string.([1,1,1,1,2,2,2,2,2])
pa = plot(x=x_cont, Geom.histogram(density=true))
pb = plot(x=x_disc, Geom.histogram(density=true))
hstack(pa, pb)

issue1163

@EliasBcd
Copy link
Author

Sorry for the late answer.

@tlnagy : My little example is a little bit silly obviously. The general idea is inline with @Mattriks idea: instead of computing the proportion before with a count function, being able to represent directly the proportions (and potentially switch easily between counting and proportional representation). I also just think that the behavior is not the intended and should be documented as such.

@bjarthur It also works for me, but if I am picky, the alignment of the labels is not right.

x = [1,1,1,1, 2,2,2,2,2]
lab = Dict(1 =>"one", 2 => "two")
pa = plot(x = x,
    Scale.x_continuous(labels = x -> lab[x]),
    Geom.histogram(density = true),
    Guide.xticks(ticks = collect(keys(lab))))
pb = Gadfly.plot(x = x, 
    Scale.x_discrete(labels = x -> lab[x]), 
    Geom.histogram(density = true))
hstack(pa, pb)

@bjarthur
Copy link
Member

you can hack around it with

pa = plot(x = x,
           Scale.x_continuous(labels = x -> lab[x-0.5]),
           Geom.histogram(density = true),
           Guide.xticks(ticks = 0.5+collect(keys(lab))))

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