Themes
Many parameters controlling the appearance of plots can be overridden by passing a Theme
object to the plot
function. Or setting the Theme
as the current theme using push_theme
(see also pop_theme
and with_theme
below).
The constructor for Theme
takes zero or more named arguments each of which overrides the default value of the field.
The Theme stack
Gadfly maintains a stack of themes and applies theme values from the topmost theme in the stack. This can be useful when you want to set a theme for multiple plots and then switch back to a previous theme.
push_theme(t::Theme)
and pop_theme()
will push and pop from this stack respectively. You can use with_theme(f, t::Theme)
to set a theme as the current theme and call f()
.
style
You can use style
to override the fields on top of the current theme at the top of the stack. style(...)
returns a Theme
. So it can be used with push_theme
and with_theme
.
Parameters
These parameters can either be used with Theme
or style
default_color
: When the color aesthetic is not bound, geometry uses this color for drawing. (Color)default_point_size
: Size of points in the point and boxplot geometry. (Measure)line_width
: Width of lines in the line geometry. (Measure)panel_fill
: Background color used in the main plot panel. ( Color or Nothing)panel_opacity
: Opacity of the plot background panel. (Float in [0.0, 1.0])panel_stroke
: Border color of the main plot panel. (Color or Nothing)background_color
: Background color for the entire plot. If nothing, no background. (Color or Nothing)plot_padding
: How much padding should be put around the plot as a whole (Measure)grid_color
: Color of grid lines. (Color or Nothing)grid_color_focused
: In the D3 backend, mousing over the plot makes the grid lines emphasised by transitioning to this color. (Color or Nothing)grid_line_width
: Width of grid lines. (Measure)minor_label_font
: Font used for minor labels such as guide entries and labels. (String)minor_label_font_size
: Font size used for minor labels. (Measure)minor_label_color
: Color used for minor labels. (Color)major_label_font
: Font used for major labels such as guide titles and axis labels. (String)major_label_font_size
: Font size used for major labels. (Measure)major_label_color
: Color used for major labels. (Color)key_position
: Where key should be placed relative to the plot panel. One of:left
,:right
,:top
,:bottom
, or:none
. Setting to:none
disables the key. (Symbol)key_title_font
: Font used for titles of keys. (String)key_title_font_size
: Font size used for key titles. (Measure)key_title_color
: Color used for key titles. (Color)key_label_font
: Font used for key entry labels. (String)key_label_font_size
: Font size used for key entry labels. (Measure)key_label_color
: Color used for key entry labels. (Color)key_max_columns
: Maximum number of columns for key entry labels. (Int)bar_spacing
: Spacing between bars in Geom.bar. (Measure)boxplot_spacing
: Spacing between boxplots in Geom.boxplot. (Measure)errorbar_cap_length
: Length of caps on error bars. (Measure)highlight_width
: Width of lines drawn around plot geometry like points, and boxplot rectangles. (Measure)discrete_highlight_color
andcontinuous_highlight_color
: Color used to outline plot geometry. This is a function that alters (e.g. darkens) the fill color of the geometry. (Function)lowlight_color
: Color used to draw background geometry, such asGeom.ribbon
. This is a function that alters the fill color of the geometry. (Function)lowlight_opacity
: Opacity of background geometry such as Geom.ribbon. (Float64)middle_color
: Color altering function used to draw the midline in boxplots. (Function)middle_width
: Width of the middle line in boxplots. (Measure)guide_title_position
: One of:left
,:center
,:right
indicating the placement of the title of color key guides. (Symbol)colorkey_swatch_shape
: The shape used in color swatches in the color key guide. Either:circle
or:square
(Symbol)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_scheme
: ADiscreteColorScale
see Scale.color_discrete_huecontinuous_color_scheme
: AContinuousColorScale
see Scale.color_continuous
Examples
dark_panel = Theme(
panel_fill=colorant"black",
default_color=colorant"orange"
)
plot(x=rand(10), y=rand(10), dark_panel)
Setting the font to Computer Modern to create a LaTeX-like look, and choosing a font size:
Gadfly.push_theme(dark_panel)
p = plot(x=rand(10), y=rand(10),
style(major_label_font="CMU Serif",minor_label_font="CMU Serif",
major_label_font_size=16pt,minor_label_font_size=14pt))
# can plot more plots here...
Gadfly.pop_theme()
Same effect can be had with with_theme
Gadfly.with_theme(dark_panel) do
plot(x=rand(10), y=rand(10),
style(major_label_font="CMU Serif",minor_label_font="CMU Serif",
major_label_font_size=16pt,minor_label_font_size=14pt))
end
or
Gadfly.push_theme(dark_panel)
Gadfly.with_theme(
style(major_label_font="CMU Serif",minor_label_font="CMU Serif",
major_label_font_size=16pt,minor_label_font_size=14pt)) do
plot(x=rand(10), y=rand(10))
end
Gadfly.pop_theme()
Named themes
To register a theme by name, you can extend Gadfly.get_theme(::Val{:theme_name})
to return a Theme object.
Gadfly.get_theme(::Val{:orange}) =
Theme(default_color=colorant"orange")
Gadfly.with_theme(:orange) do
plot(x=[1:10;], y=rand(10), Geom.bar)
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.
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.
Gadfly.push_theme(:dark)
plot(dataset("datasets", "iris"),
x="SepalLength", y="SepalWidth", color="Species", Geom.point)
using RDatasets
gasoline = dataset("Ecdat", "Gasoline")
plot(gasoline, x=:Year, y=:LGasPCar, color=:Country,
Geom.point, Geom.line)
using DataFrames
xs = 0:0.1:20
df_cos = DataFrame(
x=xs,
y=cos(xs),
ymin=cos(xs) .- 0.5,
ymax=cos(xs) .+ 0.5,
f="cos"
)
df_sin = DataFrame(
x=xs,
y=sin(xs),
ymin=sin(xs) .- 0.5,
ymax=sin(xs) .+ 0.5,
f="sin"
)
df = vcat(df_cos, df_sin)
p = plot(df, x=:x, y=:y, ymin=:ymin, ymax=:ymax, color=:f, Geom.line, Geom.ribbon)
using Distributions
X = rand(MultivariateNormal([0.0, 0.0], [1.0 0.5; 0.5 1.0]), 10000);
plot(x=X[1,:], y=X[2,:], Geom.hexbin(xbincount=100, ybincount=100))
Gadfly.pop_theme()
Gadfly.Theme(RGB{N0f8}(0.996,0.263,0.396),0.9mm,0.3mm,nothing,RGB{N0f8}(0.133,0.157,0.192),nothing,1.0,RGB{N0f8}(0.133,0.157,0.192),5.0mm,RGB{N0f8}(0.341,0.341,0.341),Measures.Length{:mm,Float64}[0.5mm,0.5mm],RGB{N0f8}(0.627,0.627,0.627),0.2mm,"'PT Sans Caption','Helvetica Neue','Helvetica',sans-serif",2.822222222222222mm,RGB{N0f8}(0.631,0.631,0.631),"'PT Sans','Helvetica Neue','Helvetica',sans-serif",3.880555555555555mm,RGB{N0f8}(0.631,0.631,0.631),"'PT Sans Caption','Helvetica Neue','Helvetica',sans-serif",2.822222222222222mm,RGB{N0f8}(0.298,0.251,0.294),"'PT Sans','Helvetica Neue','Helvetica',sans-serif",3.880555555555555mm,RGB{N0f8}(0.631,0.631,0.631),"'PT Sans','Helvetica Neue','Helvetica',sans-serif",2.822222222222222mm,RGB{N0f8}(0.631,0.631,0.631),40,-0.05mm,1.0mm,3.0mm,Gadfly.default_stroke_color,0.3mm,Gadfly.border_color,Gadfly.default_continuous_highlight_color,Gadfly.lowlight_color,0.6,Gadfly.default_middle_color,0.6mm,:left,:square,:right,nothing,Function[Compose.circle,Gadfly.square,Gadfly.diamond,Gadfly.cross,Gadfly.xcross,Gadfly.utriangle,Gadfly.dtriangle,Gadfly.star1,Gadfly.star2,Gadfly.hexagon,Gadfly.octagon],2.0mm,1000,10.0,0.5,0.2,4,Gadfly.Scale.DiscreteColorScale(Gadfly.gen_dark_colors,nothing,nothing,true),Gadfly.Scale.ContinuousColorScale(Gadfly.Scale.f,Gadfly.Scale.ContinuousScaleTransform(identity,identity,Gadfly.Scale.identity_formatter),nothing,nothing))