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.dodge #1240

Merged
merged 1 commit into from
Jan 29, 2019
Merged

Stat.dodge #1240

merged 1 commit into from
Jan 29, 2019

Conversation

Mattriks
Copy link
Member

@Mattriks Mattriks commented Jan 8, 2019

  • I've updated the documentation to reflect these changes
  • I've added an entry to NEWS.md
  • I've added and/or updated the unit tests
  • I've run the regression tests
  • I've built the docs and confirmed these changes don't cause new errors

This PR:

Examples

Examples of dodged bar plots with errors and labels:

using Compose, DataFrames, RDatasets, Statistics
salaries = dataset("car","Salaries")
salaries.Salary /= 1000.0
salaries.Discipline = ["Discipline $(x)" for x in salaries.Discipline]
df = by(salaries, [:Rank,:Discipline], :Salary=>mean, :Salary=>std)
[df[i] = df.Salary_mean.+j*df.Salary_std for (i,j) in zip([:ymin, :ymax], [-1, 1.0])]
df[:label] = string.(round.(Int, df.Salary_mean))

p1 = plot(df, x=:Discipline, y=:Salary_mean, color=:Rank, 
    Scale.x_discrete(levels=["Discipline A", "Discipline B"]),
    ymin=:ymin, ymax=:ymax, Geom.errorbar, Stat.dodge,
    Geom.bar(position=:dodge), 
    Scale.color_discrete(levels=["Prof", "AssocProf", "AsstProf"]),
    Guide.colorkey(title="", pos=[0.76w, -0.38h]),
    Theme(bar_spacing=0mm, stroke_color=c->"black")
)
p2 = plot(df, y=:Discipline, x=:Salary_mean, color=:Rank, 
    Coord.cartesian(yflip=true), Scale.y_discrete,
    label=:label, Geom.label(position=:right), Stat.dodge(axis=:y),
    Geom.bar(position=:dodge, orientation=:horizontal), 
    Scale.color_discrete(levels=["Prof", "AssocProf", "AsstProf"]),
    Guide.yticks(orientation=:vertical), Guide.ylabel(nothing)
)
hstack(p1,p2)

dodge1
Here's an example of a stacked bar plot with labels:

p3 = plot(df, x=:Discipline, y=:Salary_mean, color=:Rank, 
    Scale.x_discrete(levels=["Discipline A", "Discipline B"]),
    label=:label, Geom.label(position=:centered), Stat.dodge(position=:stack),
    Geom.bar(position=:stack)
)

dodge2

@codecov-io
Copy link

codecov-io commented Jan 8, 2019

Codecov Report

Merging #1240 into master will increase coverage by 0.11%.
The diff coverage is 89.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1240      +/-   ##
==========================================
+ Coverage   85.67%   85.79%   +0.11%     
==========================================
  Files          35       35              
  Lines        4085     4112      +27     
==========================================
+ Hits         3500     3528      +28     
+ Misses        585      584       -1
Impacted Files Coverage Δ
src/geom/bar.jl 91.85% <100%> (ø) ⬆️
src/geom/errorbar.jl 91.89% <83.33%> (-5.34%) ⬇️
src/statistics.jl 90.64% <91.66%> (+0.02%) ⬆️
src/scale.jl 94.51% <0%> (+0.04%) ⬆️
src/guide.jl 84.87% <0%> (+1.21%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aa8dff9...1a355f6. Read the comment docs.

@bjarthur
Copy link
Member

bjarthur commented Jan 8, 2019

this looks great as usual. is there a better name for barerror though? the difference with errorbar might not be obvious to a newcomer.

@Mattriks
Copy link
Member Author

I think Geom.barerror is easy to remember. I've also tweaked Stat.dodge so that Geom.barerror and Geom.errorbar behave in the same way for simple bar plots (without stacking/dodging):

df2 = by(dataset("car","Salaries"), [:Rank], :Salary=>mean, :Salary=>std)
[df2[i] = df2.Salary_mean.+j*df2.Salary_std for (i,j) in zip([:ymin, :ymax], [-1, 1.0])]

p1 = plot(df2, x=:Rank, y=:Salary_mean, color=:Rank,
    Scale.x_discrete, Guide.xlabel(nothing),
    ymin=:ymin, ymax=:ymax, Geom.errorbar, Geom.bar,
     Guide.colorkey(title="", pos=[0.05w,-0.4h])
)
p2 = plot(df2, x=:Rank, y=:Salary_mean, color=:Rank,
    Scale.x_discrete, Guide.xlabel(nothing),
    ymin=:ymin, ymax=:ymax, Geom.barerror, Geom.bar,
     Guide.colorkey(title="", pos=[0.05w,-0.4h])
)
hstack(p1, p2)

dodge3
Note that Geom.barerror will also work with points, lines, etc.

@Mattriks
Copy link
Member Author

This PR is ready to be merged, if there are no other suggestions.

@bjarthur
Copy link
Member

@tlnagy can you please comment?

Stat.dodge is fantastic, and it's great to close these old issues.

i still really don't like Geom.barerror though. just too confusing with Geom.errorbar. and it's only a convience function for Stat.dodge + Geom.errorbar.

i'd suggest just removing Stat.barerror. the rest of this PR is ready to merge. can always add it later if we decide the convenience is nice. but difficult to remove later since we're at 1.0, and removing it would be a breaking change.

@tlnagy
Copy link
Member

tlnagy commented Jan 22, 2019

I strongly agree with @bjarthur here that Stat.barerror will lead to confusion and I personally don't see the need for it.

Stat.dodge is fantastic, and it's great to close these old issues.

Agreed. This is a great contribution @Mattriks!

@Mattriks
Copy link
Member Author

Ok, I removed Geom.barerror, added some minor fixes, updated documentation, and rebased - done!

@bjarthur bjarthur merged commit 6240ca0 into GiovineItalia:master Jan 29, 2019
@bjarthur
Copy link
Member

thanks!

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