These geoms add reference lines (sometimes called rules) to a plot, either horizontal, vertical, or diagonal (specified by slope and intercept). These are useful for annotating plots.
geom_abline({
"mapping" = null,
"data" = null,
"...",
"slope",
"intercept",
"na.rm" = false,
"show.legend" = NA
})
geom_hline({
"mapping" = null,
"data" = null,
"...",
"yintercept",
"na.rm" = false,
"show.legend" = NA
})
geom_vline({
"mapping" = null,
"data" = null,
"...",
"xintercept",
"na.rm" = false,
"show.legend" = NA
})
mapping | ||
---|---|---|
data |
The data to be displayed in this layer. There are three options: If NULL, the default, the data is inherited from the plot data as specified in the call to A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created. A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)). | |
... |
Other arguments passed on to | |
na.rm |
If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed. | |
show.legend |
logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display. | |
xintercept, yintercept, slope, intercept |
Parameters that control the position of the line. If these are set, data, mapping and show.legend are overridden. |
These geoms act slightly differently from other geoms. You can supply the parameters in two ways: either as arguments to the layer function, or via aesthetics. If you use arguments, e.g. geom_abline(
, then behind the scenes the geom makes a new data frame containing just the data you've supplied. That means that the lines will be the same in all facets; if you want them to vary across facets, construct the data frame yourself and use aesthetics.intercept
= 0, slope
= 1)
Unlike most other geoms, these geoms do not inherit aesthetics from the plot default, because they do not understand x and y aesthetics which are commonly set in the plot. They also do not affect the x and y scales.
These geoms are drawn using with geom_line()
so support the same aesthetics: alpha
, color
, linetype
and size
. They also each have aesthetics that control the position of the line:
geom_vline()
: xintercept
geom_hline()
: yintercept
geom_abline()
: slope
and intercept
// Fixed values
var cxp = new cxplot("canvas1", mtcars, aes("wt", "mpg"));
cxp.geom_point();
cxp.geom_vline({"xintercept": 5})
var cxp = new cxplot("canvas2", mtcars, aes("wt", "mpg"));
cxp.geom_point();
cxp.geom_vline({"xintercept": [1, 2, 3, 4, 5]})
var cxp = new cxplot("canvas3", mtcars, aes("wt", "mpg"));
cxp.geom_point();
cxp.geom_hline({"yintercept": 20})
// Calculate slope and intercept of line of best fit using your own function
// (Intercept) wt
// 37.285126 -5.344472
var cxp = new cxplot("canvas4", mtcars, aes("wt", "mpg"));
cxp.geom_point();
cxp.geom_abline({"intercept": 37, "slope": -5})
// To show different lines in different facets, use aesthetics
var mean_wt = [["cyl", "wt"], [4, 2.28], [6, 3.11], [8, 4]];
var cxp = new cxplot("canvas5", mtcars, aes({"x": "mpg", "y": "wt"}));
cxp.geom_point();
cxp.geom_hline(aes({"yintercept": "wt"}, mean_wt));
cxp.facet_wrap("cyl");
// You can also control other aesthetics
var mean_wt = [["cyl", "wt"], [4, 2.28], [6, 3.11], [8, 4]];
var cxp = new cxplot("canvas6", mtcars, aes({"x": "mpg", "y": "wt"}));
cxp.geom_point();
cxp.geom_hline(aes({"yintercept": "wt", "color": "wt"}, mean_wt));
cxp.facet_wrap("cyl");