Skip to content

Commit

Permalink
Theme_colormaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattriks committed Mar 23, 2018
1 parent 0db3756 commit 0b1e39d
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 106 deletions.
28 changes: 26 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@
This is a log of major changes in Gadfly between releases. It is not exhaustive.
Each release typically has a number of minor bug fixes beyond what is listed here.

# Version 0.6.1 (WIP)
# Version 0.6.6
* Add `:ggplot` named theme (#1118)
* Replace `Theme(discrete_color_scale=, continuous_color_scale=)` with `Theme(discrete_colormap=, continuous_colormap=)` (#1118)
* Change `Theme(grid_strokedash=)` to `Theme(grid_line_style=)` and include in docs (#1106)
* Add `Geom.ellipse` (#1103)

# Version 0.6.5
* Add arguments `Guide.colorkey(labels=, pos=)` (#1085, #1087)
* Enable the color aesthetic for `Geom.violin` (#1081)
* Configure `Theme(plot_padding=[left, right, top, bottom])` (#1078)
* Add `Geom.hair` (#1076)

# Version 0.6.4

# Version 0.6.3

# Version 0.6.2
* Add `Geom.rect` (#993)
* Add `Geom.vectorfield` (#992)

# Version 0.6.1
* Improve `Stat.smooth` (#983)

# Version 0.6.0

* Add `Geom.density2d` (#959)
* Dramatically speed up precompilation by removing old, duplicate code (#958)
* Add `Geom.abline` (#957)
* Add `Geom.density2d` (#959)
* Drop support for Julia 0.4 (#954)

# Version 0.5.3
Expand All @@ -17,6 +38,9 @@ Each release typically has a number of minor bug fixes beyond what is listed her
* Various doc improvements (#923, #933, #943)
* Improved Juno support (#920, @MikeInnes)

# Version 0.4.4
* Add `Geom.segment` and `Geom.vector` (#823)

# Version 0.4.1

* Add transformed continuous color scales (`Scale.color_{log,log10,log2,asinh,sqrt}`).
Expand Down
4 changes: 3 additions & 1 deletion docs/src/lib/scales/scale_color_continuous.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Author = "David Chudzicki"

Create a continuous color scale that the plot will use.

This can also be set as the `continuous_color_scheme` in a [`Theme`](@ref)
There are 2 ways to change the color palette in a continuous color scale:
* for single plots, by using `Scale.color_continuous(colormap=)`, see Examples below
* for a set of plots, by using `Gadfly.push_theme(style(continuous_colormap=))`, see [Themes](@ref)

## Arguments

Expand Down
29 changes: 10 additions & 19 deletions docs/src/lib/scales/scale_color_discrete_hue.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ alias for [Scale.color_discrete_hue](@ref).
## Examples

```@setup 1
using Gadfly
using Colors
using Colors, Gadfly
srand(1234)
```

## Examples
There are 2 ways to change the discrete color scale:

You can set a discrete color scale of your choice in a plot.
* For a single plot, provide a color mapping function, i.e `f` above, to `Scale.color_discrete`:

```@example 1
function gen_colors(n)
cs = distinguishable_colors(n,
[colorant"#FE4365", colorant"#eca25c"], # seed colors
Expand All @@ -42,30 +40,23 @@ function gen_colors(n)
end
using RDatasets
iris = dataset("datasets", "iris")
plot(iris, x=:SepalLength, y=:SepalWidth, color=:Species,
Geom.point, Scale.color_discrete(gen_colors))
```

You can force the use of a discrete scale on data that would otherwise receive a continuous scale:
* For a set of plots, provide a color mapping function to `style(discrete_colormap=)`, and use `Gadfly.push_theme()`. See [Themes](@ref).

```@example 1
plot(x=rand(12), y=rand(12), color=repeat([1,2,3], outer=[4]),
Scale.color_discrete())
Gadfly.push_theme( style(discrete_colormap=gen_colors) )
Gadfly.pop_theme() # hide
```

To set a default color scale for plots, you can set it in the current [Theme](@ref) using `push_theme`, using `style` to modify the current theme.
Also, you can force the use of a discrete scale on data that would otherwise receive a continuous scale:

```@example 1
Gadfly.push_theme(
style(
discrete_color_scale=Scale.color_discrete(gen_colors)
)
)
Gadfly.pop_theme() # hide
plot(x=rand(12), y=rand(12), color=repeat([1,2,3], outer=[4]),
Scale.color_discrete())
```

51 changes: 45 additions & 6 deletions docs/src/man/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ These parameters can either be used with `Theme` or `style`
* `bar_highlight`: Color used to stroke bars in bar plots. If a function is
given, it's used to transform the fill color of the bars to obtain a stroke
color. (Function, Color, or Nothing)
* `discrete_color_scale`: A `DiscreteColorScale` see [Scale.color_discrete_hue](@ref)
* `continuous_color_scale`: A `ContinuousColorScale` see [Scale.color_continuous](@ref)
* `discrete_colormap`: The function `f` in [Scale.color_discrete_hue](@ref)
* `continuous_colormap`: The function `colormap` in [Scale.color_continuous](@ref)

## Examples

```@setup 1
using RDatasets
using Gadfly
using RDatasets, Gadfly
Gadfly.set_default_plot_size(12cm, 8cm)
srand(12345)
```
Expand Down Expand Up @@ -170,11 +169,11 @@ Gadfly.with_theme(:orange) do
end
```

Gadfly comes built in with 2 named themes: `:default` and `:dark`. You can also set a theme to use by default by setting the `GADFLY_THEME` environment variable *before* loading Gadfly.
Gadfly comes built in with 3 named themes: `:default`, `:dark` and `:ggplot`. You can also set a theme to use by default by setting the `GADFLY_THEME` environment variable *before* loading Gadfly.

## The Dark theme

This is one of the two themes the ship with Gadfly the other being `:default`. Here are a few plots that use the dark theme.
Here are a few plots that use the dark theme.

```@example 1
Gadfly.push_theme(:dark)
Expand Down Expand Up @@ -229,4 +228,44 @@ plot(x=X[1,:], y=X[2,:], Geom.hexbin(xbincount=100, ybincount=100))

```@example 1
Gadfly.pop_theme()
nothing # hide
```

## The ggplot theme

Here are examples of the ggplot theme. When using named themes, note how you can change the `order` of key colors (see [Scale.color_discrete_hue](@ref)), or use a transformed color axis (see [Scale.color_continuous](@ref)), in the usual way:


```@setup 2
using RDatasets, Gadfly
Gadfly.set_default_plot_size(17cm, 8cm)
```

```@example 2
Gadfly.push_theme(:ggplot)
nothing # hide
```
```@example 2
pa = plot(dataset("datasets","iris"),
x=:PetalWidth, y=:SepalLength, color=:Species, Geom.point,
Scale.color_discrete(order=[3,2,1]),
Guide.colorkey(title="Iris", pos=[0.1, 9.3])
)
pb = plot(dataset("ggplot2","diamonds"), x=:Price, y=:Carat,
Geom.histogram2d(xbincount=25, ybincount=25),
Scale.color_log10,
Scale.x_continuous(format=:plain)
)
hstack(pa, pb)
```

```@example 2
Gadfly.pop_theme()
nothing # hide
```




6 changes: 2 additions & 4 deletions src/Gadfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1200,10 +1200,8 @@ get_scale{t,var}(::Val{t}, ::Val{var}, theme::Theme) = default_aes_scales[t][var
get_scale(t::Symbol, var::Symbol, theme::Theme) = get_scale(Val{t}(), Val{var}(), theme)

### Override default getters for color scales
get_scale(::Val{:categorical}, ::Val{:color}, theme::Theme=current_theme()) =
theme.discrete_color_scale
get_scale(::Val{:numerical}, ::Val{:color}, theme::Theme=current_theme()) =
theme.continuous_color_scale
get_scale(::Val{:categorical}, ::Val{:color}, theme::Theme=current_theme()) = Scale.color_discrete()
get_scale(::Val{:numerical}, ::Val{:color}, theme::Theme=current_theme()) = Scale.color_continuous()


function scale_exists(t::Symbol, var::Symbol)
Expand Down
24 changes: 2 additions & 22 deletions src/scale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -442,19 +442,9 @@ DiscreteColorScale(f; levels=nothing, order=nothing, preserve_order=true) =

element_aesthetics(scale::DiscreteColorScale) = [:color]

function default_discrete_colors(n)
convert(Vector{Color},
distinguishable_colors(n, [LCHab(70, 60, 240)],
transform=c -> deuteranopic(c, 0.5),
lchoices=Float64[65, 70, 75, 80],
cchoices=Float64[0, 50, 60, 70],
hchoices=linspace(0, 330, 24),
)
)
end

# Common discrete color scales
function color_discrete_hue(f=default_discrete_colors;
function color_discrete_hue(f=Gadfly.current_theme().discrete_colormap;
levels=nothing,
order=nothing,
preserve_order=true)
Expand Down Expand Up @@ -541,18 +531,8 @@ ContinuousColorScale(f, trans=identity_transform; minvalue=nothing, maxvalue=not
ContinuousColorScale(f, trans, minvalue, maxvalue)

function continuous_color_scale_partial(trans::ContinuousScaleTransform)
lch_diverge2 = function(l0=30, l1=100, c=40, h0=260, h1=10, hmid=20, power=1.5)
lspan = l1 - l0
hspan1 = hmid - h0
hspan0 = h1 - hmid
function(r)
r2 = 2r - 1
return LCHab(min(80, l1 - lspan * abs(r2)^power), max(10, c * abs(r2)),
(1-r)*h0 + r * h1)
end
end

function(; minvalue=nothing, maxvalue=nothing, colormap=lch_diverge2())
function(; minvalue=nothing, maxvalue=nothing, colormap=Gadfly.current_theme().continuous_colormap)
ContinuousColorScale(colormap, trans, minvalue=minvalue, maxvalue=maxvalue)
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ input_aesthetics(stat::Histogram2DStatistic) = [:x, :y]
output_aesthetics(stat::Histogram2DStatistic) = [:xmin, :ymax, :ymin, :ymax, :color]

default_scales(::Histogram2DStatistic, t::Gadfly.Theme=Gadfly.current_theme()) =
[t.continuous_color_scale]
[Scale.color_continuous()]

const histogram2d = Histogram2DStatistic

Expand Down Expand Up @@ -1219,7 +1219,7 @@ function apply_statistic(stat::HexBinStatistic,
Scale.apply_scale(color_scale, [aes], data)
end

default_scales(::HexBinStatistic, t::Gadfly.Theme) = [t.continuous_color_scale]
default_scales(::HexBinStatistic, t::Gadfly.Theme) = [Scale.color_continuous()]


immutable StepStatistic <: Gadfly.StatisticElement
Expand Down Expand Up @@ -1376,7 +1376,7 @@ const contour = ContourStatistic

default_scales(::ContourStatistic, t::Gadfly.Theme=Gadfly.current_theme()) =
[Gadfly.Scale.z_func(), Gadfly.Scale.x_continuous(), Gadfly.Scale.y_continuous(),
t.continuous_color_scale]
Scale.color_continuous()]

function apply_statistic(stat::ContourStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
Expand Down Expand Up @@ -1729,7 +1729,7 @@ input_aesthetics(stat::VecFieldStatistic) = [:z, :x, :y, :color]
output_aesthetics(stat::VecFieldStatistic) = [:x, :y, :xend, :yend, :color]
default_scales(stat::VecFieldStatistic, t::Gadfly.Theme=Gadfly.current_theme()) =
[Gadfly.Scale.z_func(), Gadfly.Scale.x_continuous(), Gadfly.Scale.y_continuous(),
t.continuous_color_scale ]
Scale.color_continuous() ]

const vectorfield = VecFieldStatistic

Expand Down
Loading

0 comments on commit 0b1e39d

Please sign in to comment.