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

Stat.x_jitter with multiple Geoms #1254

Closed
jerlich opened this issue Feb 28, 2019 · 6 comments · Fixed by #1389
Closed

Stat.x_jitter with multiple Geoms #1254

jerlich opened this issue Feb 28, 2019 · 6 comments · Fixed by #1389

Comments

@jerlich
Copy link

jerlich commented Feb 28, 2019

Example:

using Gadfly
x = 1:10
y = x + randn(10)
ymin = y - rand(10)*2
ymax = y + rand(10)*2
j = Stat.x_jitter
plot(x=x,y=y,ymin=ymin, ymax=ymax, Geom.point, Geom.line, Geom.errorbar, j)

This jitters just the error bars. TO get the desired effect one must

plot(x=x,y=y,ymin=ymin, ymax=ymax, Geom.point,j, Geom.line,j, Geom.errorbar, j)

Is that the "right" way to do this? If so, then it isn't a bug, just missing documentation, but I thought I would make this issue for others to find.

Would it be appropriate for me to make a PR adding this an example to the docs?

@Mattriks
Copy link
Member

The right way to do this is:

plot(x=x, y=y, ymin=ymin, ymax=ymax, 
    layer(Geom.point, Geom.line, Geom.errorbar, Stat.x_jitter)
)

So the Gadfly rules are:

  1. Within a layer, all Geoms will use the layer Stat (if its specified) e.g. see Stat.smooth
  2. For Geoms outside layers (as in your example above), Gadfly makes a new layer for each Geom, and each Stat is added to the newest layer.

Yes these rules could be added to docs somewhere, maybe here.

And a future development could be chained Stats (#570)

@bjarthur
Copy link
Member

i had no idea it worked this way. documenting would be great!

@jerlich
Copy link
Author

jerlich commented Mar 3, 2019

I made this example which highlights several features of Gadfly. Do you think it might go into the docs somewhere?

using DataFrames, Gadfly

# Make the data for the points and error bars.
n_points = 30
gshift = 2
sigmoid(x) = 1 ./ (1 .+ exp.(-x))
x = randn(n_points) * 3  # This gives a nice spread f
group = rand(n_points) .< 0.5
y = sigmoid(x)
y[group.==1] = sigmoid(x[group.==1] .+ gshift)
ymin = y - rand(n_points)./5
ymax = y + rand(n_points)./5
df = DataFrame(x=x, y=y, ymin=ymin, ymax=ymax, group=group)

# Make the data for the smooth lines.
finex = -10:0.001:10
finey1 = sigmoid(finex)
finey2 = sigmoid(finex .+ gshift)

# Since the two layers share a legend they must have the same group levels.
df2 = DataFrame(x = [finex;finex], y = [finey1; finey2], group = [finey1.*0; finey1.^0])

color_list = ["green","red"] 
# These colors can be any color in Colors.color_list.
# Using Colors
# for clr in color_list
#   @assert clr in keys(Colors.color_list)
# end

c = Scale.color_discrete_manual(color_list..., levels=[true, false])
t = Theme(errorbar_cap_length=0mm)
jitter = Stat.x_jitter(range=0.5)
g = Guide.colorkey(title="My Legend", labels=["Greenies", "Reddies"])

p = plot(
        layer(df, x=:x, y=:y, ymin=:ymin, ymax=:ymax, color=:group, Geom.point, Geom.errorbar,jitter),
        layer(df2,x=:x, y=:y, color=:group, Geom.line),
        c,t,g, Guide.title("The jitter moves the points off the line")
        )
# By jittering the layer, we jitter Geom.point and Geom.errorbar together.

@Mattriks
Copy link
Member

Mattriks commented Mar 3, 2019

Here's another way, containing the 2 rules in my previous post:
(note Stat.func doesn't have a gallery example)

n_points = 30
x = randn(n_points)*3
gshift = rand([0,2], n_points)
y = sigmoid(x+gshift)
df = DataFrame(x=x, y=y, ymin=y-rand(n_points)./5, ymax=y+rand(n_points)./5, g=gshift)

plot(y=[sigmoid, x->sigmoid(x+2)], xmin=[-10], xmax=[10],  Geom.line, Stat.func(100), color=[0,2],
    layer(df, x=:x, y=:y, ymin=:ymin, ymax=:ymax, color=:g, Geom.point, Geom.errorbar,
      Stat.x_jitter(range=0.5)),
    Scale.color_discrete_manual(["red","green"]..., levels=[0,2]), 
    Guide.colorkey(title="Function", labels=["Sigmoid(x)", "Sigmoid(x+2)"]),
    Guide.xlabel("x"),
    Theme(errorbar_cap_length=0mm, key_position=:inside)
)

issue1254a

Also red/green is not a good color combination

@tlnagy
Copy link
Member

tlnagy commented Mar 4, 2019

@Mattriks if you're ever in San Francisco, I'll get you some 🍺. Great work as always.

@bjarthur
Copy link
Member

bjarthur commented Mar 4, 2019

juliacon is in DC this summer, my home town. if either of you planned on going, i'd come up for a day.

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