Over the past week I have been reading the documentation and playing with Julia’s Gadfly package. I thought it would be helpful to fellow R users coming from the world of ggplot2 to put together a quick reference guide to show the translation from one to the other.
The coding and style for creating data visualizations should look quite familiar since Gadfly was based on Hadley Wickham’s ggplot2 and Leland Wilkinson’s book, The Grammar of Graphics. ggplot2 is also based on The Grammar of Graphics book, so you should recognize and see a lot of similarities throughout this and future posts. There are some differences in implementation that may be due to the current level of maturity of Gadfly.
For the purpose of versioning in this post, I am using ggplot2 v3.3.3 and Gadfly v1.3.1. I may update this post or create a new post later as Gadfly matures.
Loading Packages
The first thing we need to do is install and load the packages in each language.
R
# Install the package
## Stable version
install.packages("ggplot2")
## install development version from GitHub:
## install.packages("devtools")
devtools::install_github("tidyverse/ggplot2")
Once the package has been added you can now include it in your R scripts.
library(ggplot2)
Julia
To install Gadfly, you can use 2 methods. The first uses the interactive package management system, accessed by typing “]” in the REPL interface. From here you can type add
the the package name. The second method uses the Pkg module to download/install the package. Both produce the same results.
Additional packages to install are the Compose, Cairo and Fontconfig packages. The Compose package is a vector graphics library that is comparable to R’s grid package. Compose provides the backend support to render (write) graphics to SVG files. Cairo supports rendering graphics to PDF, Postscript or PNG and Fontconfig provides font configuration bindings.
# Install the packages
] add Gadfly, Compose, Cairo, Fontconfig
# after adding, press backspace to return to Julia REPL
# Alternatively
using Pkg
Pkg.add("Gadfly")
Pkg.add("Compose")
Pkg.add("Cairo") # PDF/PNG Support
Pkg.add("Fontconfig")
Once the packages have been added you can now include it in your Julia scripts or REPL interface using
using Gadfly
using Compose
using Cairo # If planning to output PDF, PS, PNG
Basics of Plotting
Both R and Julia have rich, highly tailorable capabilities to plot and export data visualizations. Both operate on the foundation that plots have data, layers and aesthetics.
For the basis of continuity between the languages, I will be using the same sample dataset. For this example, the dataset is the “mpg” dataset provided in the ggplot2 package. The equivalent packages in ggplot2 and general R datasets are also available in Julia. Checkout the list of datasets available in the RDatasets package.
R
The basis of ggplot2 revolves around the ggplot() function. It is required to create any graphic within the ggplot2 package. Although, ggplot2 has qplot
/quickplot()
functions, they are alternatives to the base plot()
function.
The ggplot()
function accepts data and aesthetics but by itself, it does not determine the output layers. . Whatever is provided to the function becomes the foundation of the graphic. If you provide just ggplot()
the output will just be a blank space. Everything else (geoms) is a layer on top of it. You can add layers by using +
or %+%
. In my R code, you will notice just the +
.
In this example, we are creating a scatter plot of points between engine displacement and highway mpg. We color the points by the class of car.
The first line shows the absolute minimum and produces a black image. The second line will produce a graphic containing just the x-y axis and chart area. It will not plot the data without a specified layer to tell it how to plot the data provided.
# This is the absolute minimum, but produces a blank image
ggplot()
# Will produce a graphic with axis, but no data will be plotted
ggplot(data = mpg,
aes(x = displ, y = hwy, colour = class))
Once we add the geom_point()
layer, we tell it how to plot the data, as points. Alternatively, we can leave ggplot()
blank and define the geom with the data. I personally prefer this method because its easier to control the aesthetics at the layer level. It is easier to change layer positions to make complex graphic layer exactly how I want them to. As you add more and more geom layers this will become more apparent.
# This will plot the data as points because the layer
ggplot(data = mpg,
aes(x = displ, y = hwy, colour = class)) +
geom_point()
# alternatively, this produces the same graphic as above, but has more flexibility, in my opinion
ggplot() +
geom_point(data = mpg,
aes(x = displ, y = hwy, colour = class))
Julia
Similarly in Julia, the basis of all plots in Gadfly will be the plot()
function. The function takes data, elements and mappings. Everything about the plot is included inside the function rather than added as separate entities. This seems odd after plotting in R/ggplot2 for over 7 years at this point, but I have adjusted. Since I typically put one parameter per line this almost doesn’t matter. The code stays cleaner that way and easier to see what you are providing in either R or Julia.
plot()
In Julia you can add to a plot but its different than R. We can add a feature to a plot incrementally, using push!()
. I do not like this method as much but can see how a few instances where this may be good because it give you a chance to see how the plot builds after each step.
p = plot()
push!(p, layer(mpg, x = :Displ, y = :Hwy, color = :Class, Geom.point))
push!(p, Theme(background_color="white"))
To produce the same graphic as above in Julia we get the same dataset. The column names start with a capital letters in the Julia implementation. Without specifying the background color, the background is transparent/absent by default so I defined that element to mirror the normal output from R. Having the background default to empty may not be so bad, but will have to play with it some more.
By default plot()
plots Geom.point if nothing is explicitly provided. I can still call it explicitly though to keep some semblance to R.
mpg = dataset("ggplot2", "mpg")
p = plot(mpg,
x = :Displ,
y = :Hwy,
color = :Class,
Geom.point,
Theme(background_color="white")
)
The graphs are essentially the same minus some aesthetic and theme differences.
Rendering/Saving Graphics
For rendering the plots in the section above I used the code in each block below.
R
In ggplot2, we can use a convenient function for saving a plot, ggsave
. We do not need a special function or package to write out a plot to PNG or PDF. By default the dpi is set to 300.
p %>%
ggsave(filename = './basic_plot_scatter_r.png',
width = 6, height = 4, units = 'in')
Please reference ggsave
for more details.
Julia
The core structure for saving a plot to a graphics file is to assign the plot function to a variable of your choice. Then you can write out the plot to your desired graphics output like SVG, PDF, Postscript, or PNG. The SVG output is provided natively with the Compose package. Support for PDF, and PNG files can be found by using the Cairo package.
In the PDF and PNG functions, I need to define the dpi to be 300, so the plots are on par with the R version. According to the documentation the default dpi is set to 96.
using Compose
# draw the plot to a graphics file (in this case SVG)
draw(SVG("foo.svg", 6inch, 4inch), p)
# Or Pipe the plot variable to write the graphic output
p |> SVG("./Julia/R_to_Julia/basic_plot_scatter_jl.SVG",
width = 6inch, height = 4inch)
using Cairo #PDF, PS, PNG
p |> PNG("./Julia/R_to_Julia/basic_plot_scatter_jl.png",
width = 6inch, height = 4inch
, dpi=300)
Please reference Compose and Cairo for more details.
Aesthetics
Aesthetics is how we tailor the visual appearance of the geometries we plot. In R we call them out explicitly if we want to. In Julia there is no specific function to call out the aesthetics of a layer.
R
In R, we use can define aesthetics in two ways. The first is to define the x-y coordinate values inside the aes()
function while leaving out color, shape, size, alpha (transparency). Other other is to define all the aesthetics values inside the aes()
function. The following code blocks demonstrate both methods You can apply each method to each geom layer as well, though defining everything inside the aes()
will not always work if the aesthetic parameters don’t align with other layers. You can also refer to this portion of documentation to give you a broad overview of each aesthetic.
Method 1
In this method, I decided to keep color inside the aes()
to color the data along the “Class” categorical values. You can place “color=” outside the function as well, just like the other aesthetic attributes. Here, I can explicitly define the shapes in the plot without conforming to categorical values. There are methods using scale_[aesthetic attr]_manual()
functions to allow for user defined aesthetic definitions.
You can see the corresponding aesthetic values in the code and where that gets expressed in the graphic.
p <-
ggplot() +
geom_point(data = mpg,
aes(x = displ,
y = hwy,
color = class),
shape = 'triangle',
size = 3,
alpha = 0.4)
# Saving/Rendering Plots
p %>%
ggsave(filename = './aesthetics_plot_scatter_alt_r.png',
width = 6, height = 4, units = 'in')
Method 2
In this second method, we put all the attributes inside the aes()
function. As we can see in the image below the effects of that modification from the one above. Each aesthetic attribute gets called out in the guide to the left. This method works well when you have multiple column values defining an aesthetic feature, creating rich multi-dimensional graphics. When you start to add more layers this can be problematic if the layers do not share the same values along the same aesthetic attributes. That gets too involved for the scope of this post though.
You can see the corresponding aesthetic values in the code and where that gets expressed in the graphic.
p <-
ggplot() +
geom_point(data = mpg,
aes(x = displ,
y = hwy,
color = class,
size = 1.5,
alpha = 0.4),
shape = 'triangle')
# Saving/Rendering Plots
p %>%
ggsave(filename = './aesthetics_plot_scatter_alt_r.png',
width = 6, height = 4, units = 'in')
For more information on shapes, size, alpha and color/fill, please reference the linked pages. Other aesthetics information here and manually controlling aesthetics.
Julia
Method 1
In Julia, we can call out the aesthetics without having to specifically denote it like in R. We can also define the aesthetics are the layer level as well. As you can see, the color field was set to the column “Class”, the size was set to 0.25 and the alpha value was set to 40%. Including the shape aesthetic attribute with the size attribute caused an error so I’ll demonstrate this in two code blocks. This appears to be a possible bug in the package code.
p = plot(
layer(
mpg,
x = :Displ,
y = :Hwy,
color = :Class,
# shape = [Shape.dtriangle],
size = [0.5],
alpha = [0.4],
Geom.point
),
Theme(background_color="white")
)
p |> PNG("./Julia/R_to_Julia/aesthetics_plot_scatter_jl.PNG")
Method 2
In this example I have commented out the size attribute and expressed the shape aesthetic.
p = plot(
layer(
mpg,
x = :Displ,
y = :Hwy,
color = :Class,
shape = [Shape.dtriangle],
# size = 0.5,
alpha = [0.9],
Geom.point
),
Theme(background_color="white")
)
p |> PNG("./Julia/R_to_Julia/aesthetics_plot_scatter_shapes_jl.PNG")
Alternative Method
Alternatively you can use the following to adjust the size. The down side is it applies to all layers. Also notice that I can adjust the coordinate range explicitly to make the graph look more like the ggplot2 version.
p = plot(
layer(
mpg,
x = :Displ,
y = :Hwy,
color = :Class,
shape = [Shape.dtriangle],
alpha = [0.4],
Geom.point
),
Theme(background_color="white",
point_size=12pt),
Coord.cartesian(xmin=1, xmax=8, ymin=10, ymax=50)
)
p |> PNG("./Julia/R_to_Julia/aesthetics_plot_scatter_combined_jl.PNG")
For more information on aesthetics information here for points and Theme information.
Geometries and Statistics
Geometry and statistical layers are what we use to plot or express characteristics about our data. After reviewing both libraries and references, I have compiled a table to show the translation between ggplot2 and Gadfly.
R
In R, we use geom_
and stat_
followed by some method for plotting the data. This is great for looking at the code and seeing what is what, especially when you have several layers of data and a few different geometries.
Please reference the geometries documentation for more details. Another reference.
Julia
There is almost a 1:1 relationship between what you can use in ggplot2 and what is available in Gadfly. Just like ggplot2, we use Geom.
or Stat.
following by the plotting method/type.
Please reference the geometries documentation and statistics documentation for more details.
Geometries/Stats Reference Table
The following table shows the relationship as of the current versions. Each geometry being hyperlinked to its documentation in ggplot2 and Gadfly.
Guides and Themes
Themes and guides give the user the ultimate control over the look and feel of their graphics. Throughout my back and forth between the two packages it seems that the organizations are not all equivalent. I have put together a table to roughly match up the elements of the Guides and Themes for them. I have broken up that table for ease of sectioning.
R
In ggplot2 , themes are a powerful way to customize the non-data components of your plots: i.e. titles, labels, fonts, background, grid lines, and legends. Themes can be used to give plots a consistent customized look.
Please reference the guides documentation and themes documentation for more details. Additional reference.
Julia
You can view the gallery of guides here and the library references to guides her.
Gadfly maintains a stack of themes and applies theme values from the top-most 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.
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
or with_theme
.
Please reference the guides documentation and themes documentation for more details.
Guide and Theme Reference Table
Major Plot Settings
The following should be inserted into the functions theme()
in ggplot2 and Theme()
in Gadfly, unless annotated otherwise.
R ggplot2 | Description | Julia Gadfly |
facet_wrap() facet_grid() | Facet Plots | Geom.subplot_grid |
A discrete scale for use with Geom.subplot_grid | Gadfly.Scale.xgroup Gadfly.Scale.ygroup | |
labeller() | Construct labelling specification | Gadfly.Scale.group_discrete |
label_value() label_both() label_context() label_parsed() label_wrap_gen() | Useful labeller functions | |
label_bquote() | Label with mathematical expressions | |
ggtitle() labs() xlab() ylab() | Modify axis, legend, and plot labels | Gadfly.Guide.title Gadfly.Guide.xlabel Gadfly.Guide.ylabel |
annotate() | Create an annotation layer | Gadfly.Guide.annotation |
guide_colorbar() guides(color = “none”) | Enable control of the auto-generated colorkey. | Gadfly.Guide.colorkey |
guides(shape = “none”) scale_shape() | Enable control of the auto-generated shapekey. | Gadfly.Guide.shapekey |
guides(size = “none”) scale_size_discrete() | Enable control of the sizekey. | Gadfly.Guide.sizekey |
Manual Definition of discrete color keys | Gadfly.Guide.manual_color_key Gadfly.Guide.manual_discrete_key |
Theme Element Settings
The following should be inserted into the functions theme()
in ggplot2 and Theme()
in Gadfly, unless annotated otherwise.
R ggplot2 | Description | Julia Gadfly | Description |
default_color | If the color aesthetic is not mapped to anything, this is the color that is used. (Color) | ||
all point elements | point_size point_size_min point_size_max point_shapes point_label_font point_label_font_size point_label_color | Size of points in the point, boxplot, and beeswarm geometries. Minimum size of points in the point geometry. Maximum size of points in the point geometry. Shapes of points in the point geometry. Font used for labels in Geom.label. Font size used for labels. Color used for labels. | |
line | all line elements | line_width line_style | Width of lines in the line geometry. Style of lines in the line geometry. |
rect | all rectangular elements | ||
text | all text elements | minor_label_font Minor_label_font_size minor_label_color | Font used for minor labels such as tick labels and entries in keys. Font size used for minor labels. Color used for minor labels. |
title | all title elements: plot, axes, legends | major_label_font major_label_font_size major_label_color minor_label_font Minor_label_font_size minor_label_color | Font used for major labels such as guide titles and axis labels. Font size used for major labels. Color used for major labels. Font used for minor labels such as tick labels and entries in keys. Font size used for minor labels. Color used for minor labels. |
aspect.ratio | aspect ratio of the panel | Not Theme() feature. See Gadfly.Coord.cartesian |
Theme Axis Settings
The following should be inserted into the functions theme()
in ggplot2 and Theme()
in Gadfly, unless annotated otherwise.
R ggplot2 | Description | Julia Gadfy | Description |
---|---|---|---|
axis.title, axis.title.x, axis.title.y, axis.title.x.top, axis.title.x.bottom, axis.title.y.left, axis.title.y.right | labels of axes. Specify all axes’ labels, labels by plane, or individually for each axis. axis.title.*. | See Gadfly.Guide.xlabel Gadfly.Guide.ylabel | |
axis.text, axis.text.x, axis.text.y, axis.text.x.top, axis.text.x.bottom, axis.text.y.left, axis.text.y.right | tick labels along axes. Specify all axis tick labels, tick labels by plane, or individually for each axis. axis.text.*. | See Gadfly.Guide.xticks Gadfly.Guide.yticks | |
axis.ticks, axis.ticks.x, axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right | tick marks along axes. Specify all tick marks, ticks by plane, or individually for each axis . axis.ticks.*. | See Gadfly.Guide.xticks Gadfly.Guide.yticks | |
axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right | length of tick marks | ||
axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right | lines along axes Specify lines along all axes, lines for each plane, or individually for each axis. axis.line.*. | grid_line_style grid_line_width grid_line_order grid_color grid_color_focused | Style of grid lines. Width of grid lines. Context order of the grid lines. Color of grid lines. In the D3 backend, mousing over the plot makes the grid lines emphasized by transitioning to this color. |
Theme Panel Settings
The following should be inserted into the functions theme()
in ggplot2 and Theme()
in Gadfly, unless annotated otherwise.
R ggplot2 | Description | Julia Gadfly | Description |
---|---|---|---|
panel.background | background of plotting area, drawn underneath plot | panel_fill panel_opacity | Background color used in the main plot panel. Opacity of the plot background panel. |
panel.border | border around plotting area, drawn on top of plot so that it covers tick marks and grid lines. This should be used with fill = NA | panel_line_width Panel_stroke | Border line width for main plot panel. Border color of the main plot panel. |
panel.spacing, panel.spacing.x, panel.spacing.y | spacing between facet panels | plot_padding | Padding around the plot. |
panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y | grid lines. | ||
panel.ontop | option to place the panel over the data layers |
Theme Plot Settings
The following should be inserted into the functions theme()
in ggplot2 and Theme()
in Gadfly, unless annotated otherwise.
R ggplot2 | Description | Julia Gadfly | Description |
---|---|---|---|
plot.background | background of the entire plot | background_color | Background color for the entire plot. If nothing, no background. |
plot.title | plot title | See Gadfly.Guide.title | |
plot.title.position, plot.caption.position | Alignment of the plot title/subtitle and caption. | No explicit equivalent. | |
plot.subtitle | plot subtitle | No equivalent. Could use \n to create new line with additional text | |
plot.caption | caption below the plot | No explicit equivalent. | |
plot.tag | upper-left label to identify a plot | No explicit equivalent. | |
plot.tag.position | The position of the tag as a string | No explicit equivalent. | |
plot.margin | margin around entire plot | plot_padding | Padding around the plot. |
Theme Strip Settings
At the time of writing this post, it was not apparent that an equivalent existed for Julia’s Gadfly. For more information reference the theme()
function strip.*
parameters. The strip.*
parameters control aspects of facet plot features.
Theme Legend Settings
The following should be inserted into the functions theme()
in ggplot2 and Theme()
in Gadfly, unless annotated otherwise.
R ggplot2 | Description | Julia Gadfly | Description |
---|---|---|---|
legend.background | background of legend | ||
legend.margin | the margin around each legend | ||
legend.spacing, legend.spacing.x, legend.spacing.y | the spacing between legends | ||
legend.key | background underneath legend keys | ||
legend.key.size, legend.key.height, Legend.key.width | size of legend keys | ||
legend.text | legend item labels | key_label_font key_label_font_size key_label_color | Font used for key entry labels. Font size used for key entry labels. Color used for key entry labels. |
legend.text.align | alignment of legend labels | ||
legend.title | title of legend | key_title_font key_title_font_size key_title_color | Font used for titles of keys. Font size used for key titles. Color used for key titles. |
legend.title.align | alignment of legend title | guide_title_position | Placement of the title of color key guides. |
legend.position | the position of legends | key_position | Where key should be placed relative to the plot panel. |
legend.direction | layout of items in legends (“horizontal” or “vertical”) | ||
legend.justification | anchor point for positioning legend inside plot or the justification according to the plot area when positioned outside the plot | ||
legend.box | arrangement of multiple legends | ||
legend.box.just | justification of each legend within the overall bounding box, when there are multiple legends | ||
legend.box.margin | margins around the full legend area, as specified using margin() | ||
legend.box.background | background of legend area | ||
legend.box.spacing | The spacing between the plotting area and the legend box | ||
colorkey_swatch_shape | The shape used in color swatches in the color key guide. Either :circle or :square | ||
key_swatch_shape | Shape used in keys for swatches | ||
key_swatch_color | Default color used in keys for swatches. Currently works for Guide.shapekey and Guide.sizekey. | ||
key_swatch_size | Size of key swatches, will override Theme(point_size=). Currently works for Guide.shapekey . | ||
key_max_columns | Maximum number of columns for key entry labels. | ||
key_color_gradations | How many gradations to show in a continuous color key. |
Other Gadfly Theme Settings
The following should be inserted into the function Theme()
in Gadfly, unless annotated otherwise. Some of these have equivalents in the Scales section.
Julia Gadfly | Description |
label_placement_iterations | Number of annealing iterations. Used by Geom.label(position=:dynamic) |
label_out_of_bounds_penalty | Penalty for a label not being contained within the plot frame. Used by Geom.label(position=:dynamic) |
label_hidden_penalty | Penalty for making a label hidden to avoid overlaps. Used by Geom.label(position=:dynamic) |
label_visibility_flip_pr | Probability of proposing a visibility flip during label layout. Used by Geom.label(position=:dynamic) |
label_padding | Padding between marker and label. Used by Geom.label(position=:dynamic) |
discrete_sizemap | The function f in Scale.size_discrete2. |
continuous_sizemap | The function f in Scale.size_radius and Scale.size_area. |
discrete_colormap | A new Theme field, in development. Currently only works with Guide.manual_discrete_key and Guide.manual_color_key. (Function) |
alphas | Alpha palette. The default palette is [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]. Customize using a Vector of length one or greater, with 0.0≤values≤1.0 |
discrete_color_scale | A DiscreteColorScale see Scale.color_discrete_hue |
continuous_color_scale | A ContinuousColorScale see Scale.color_continuous |
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) |
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) |
stroke_color | |
highlight_width | Width of lines drawn around plot geometry like points, and boxplot rectangles. (Measure) |
discrete_highlight_color | Color used to outline plot geometry. This is a function that alters (e.g. darkens) the fill color of the geometry. (Function) |
continuous_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 as Geom.ribbon and Geom.polygon. This is a function that alters the fill color of the geometry. (Function) |
middle_color middle_width | Color altering function used to draw the midline in boxplots. (Function) Width of the middle line in boxplots. (Measure) |
Coordinate Systems
The coordinate system determine how x and y axis are represented on the plot. We can use the functions to limit x and y value ranges, or specify how to project or trans coordinates.
R
There are more coordinate systems available in R’s ggplot2. R has more general support for spatial mapping and plotting.
Please reference the coordinates documentation for more details. Another reference.
Julia
The coordinate system in Julia is confined to just 2-D Cartesian right now, but would be the mechanism to implement polar, barycentrie, etc and even projection of their 3-D counterparts. As you will notice in the table below, the Coord.cartesian()
function contains multiple ggplot2 coordinate functions, including a raster feature.
Coord.cartesian(;
xmin=nothing,
xmax=nothing,
ymin=nothing,
ymax=nothing,
xflip=false,
yflip=false,
aspect_ratio=nothing,
fixed=false,
raster=false
)
Please reference the coordinates documentation for more details.
Coordinate Reference Table
R ggplot2 | Description | Julia Gadfly |
---|---|---|
coord_cartesian() | Cartesian coordinates | Gadfly.Coord.cartesian |
coord_fixed() | Cartesian coordinates with fixed “aspect ratio” | Gadfly.Coord.cartesian |
coord_flip() | Cartesian coordinates with x and y flipped | Gadfly.Coord.cartesian |
coord_map() coord_quickmap() | Map projections | no equivalent |
coord_polar() | Polar coordinates | no equivalent |
coord_trans() | Transformed Cartesian coordinate system | no equivalent |
Scales
Scales in each language provide the user with control over the details of how the data values are translated into visual properties
R
Please reference the scales documentation for more details. Another reference1, reference2.
Julia
Please reference the scales documentation for more details. To view some of the gallery examples of implementation refer here.
Scales Reference Tables
X-Y Axis Scale Settings
Manual and Identity Scale Settings
Since these settings persist across the other sections, I have combined them here.
Color Scale Settings
There are a lot more variations in ggplot2. Some of these can be created with the methods provided in Gadfly, but may require more tinkering to get an exact 1:1 translation. Also the use of color and colour are interchangeable in ggplot2. Gadfly does not differientiate between color and fill. Their methods control the color of the point, shape, line, and polygon. You can use, stroke()
, Theme(stroke_color)
and/or Theme(highlight_width, highlight_color)
in Gadfly to control the color of the point, shape, line or polygon border.
Alpha, Shape, and Size Scale Settings
R ggplot2 | Description | Julia Gadfly |
scale_alpha() scale_alpha_continuous() scale_alpha_binned() scale_alpha_discrete() scale_alpha_ordinal() | Alpha transparency scales | Gadfly.Scale.alpha_continuous Gadfly.Scale.alpha_discrete |
scale_shape() scale_shape_binned() | Scales for shapes, aka glyphs | Gadfly.Scale.shape_discrete Gadfly.Scale.shape_identity |
scale_size() scale_radius() scale_size_binned() scale_size_area() scale_size_binned_area() | Scales for area or radius | Gadfly.Scale.size_area Gadfly.Scale.size_radius Gadfly.Scale.size_discrete2 Gadfly.Scale.size_continuous |
scale_linetype() scale_linetype_binned() scale_linetype_continuous() scale_linetype_discrete() | Scale for line patterns | Gadfly.Scale.linestyle_discrete Gadfly.Theme( line_width, line_style, ) |
Shapes
Shapes can be applied to point geometries. The following show the shapes in each language
R
There is a lot of information about shapes and even defining custom inputs for your shapes in this documentation and the reference material for scale_shape
.
Additional reference1.
Julia
Here is the list of the 15 shapes that can be used when combined with Geom.point
.
- Gadfly.Shape.circle (default)
Gadfly.Shape.cross
Gadfly.Shape.diamond
Gadfly.Shape.dtriangle
Gadfly.Shape.hexagon
Gadfly.Shape.hline
Gadfly.Shape.ltriangle
Gadfly.Shape.octagon
Gadfly.Shape.rtriangle
Gadfly.Shape.square
Gadfly.Shape.star1
Gadfly.Shape.star2
Gadfly.Shape.utriangle
Gadfly.Shape.vline
Gadfly.Shape.xcross
Please reference the shapes documentation for more details on each and their arguments.
Final Graphic
In this part, I will try to recreate a one-to-one version of the ggplot2 version in Gadfly. This will demonstrate several implementations of the methods covered above.
First task was to define the color pallete and the level/order for the classes. I then used Scale.color_discrete_manual()
to define these features for the plot. I modified the Guide.xticks
/Guide.yticks
to resemble the ggpplots, though I was not able to replicate the minor ticks and gridlines that are typical in ggplot2. Using the Theme()
, I was able to cahnge the panel background to match the same color, as well as define the grid line style and color. You will notice the discrete_highlight_color = identity
, which makes the point border the same as the fill color, rather than a white outline. Here is the code in Julia.
pallete = [
"#F8766D", "#C49A00", "#53B400", "#00C094",
"#00B6EB", "#A58AFF", "#FB61D7"]
class_levels = sort(unique(mpg.Class))
p = plot(
layer(
mpg,
x = :Displ,
y = :Hwy,
color = :Class,
shape = [Shape.circle],
alpha = [0.4],
Geom.point
),
Guide.title("Gadfly version of ggplot2 basic plot"),
Guide.xticks(ticks = 2:7),
Guide.yticks(ticks = 20:10:40),
Scale.color_discrete_manual(
pallete …,
levels = class_levels
),
Theme(
background_color = "white",
point_size = 1pt,
panel_fill = "#EFEFEF",
grid_color = "white",
grid_line_style = :solid,
discrete_highlight_color = identity
),
Coord.cartesian(xmin=1, xmax=8, ymin=10, ymax=50)
)
p |> PNG("./Julia/R_to_Julia/plot_scatter_ggplot2_style_jl.PNG")
Conclusion and What’s Next
This was a fun, but tedious, process to cross-reference the two packages. I certainly got a more indepth understanding of the methods in each language.
Since this post was purely referencial, i decided to minimize plotting each and every method. In future posts I may attempt to create a 1:1 comparison of the graphical outputs so users on each side can see more implementation rather than highlevel references and definitions.
Additional Resources
For official information and documentation on each package, please refer to the following links.
ggplot2
Gadfly
Other Language Conversion Posts
- PostgreSQL Table Creation and Bulk Insertion
- Julia’s Gadfly for R ggplot2 Users
- Converting R scripts to Julia (Part 2)
- Converting R scripts to Julia (Part 1)