geom_abline2: Add an abline that does not exceed the extrema of the axes...

Description Usage Arguments Examples

View source: R/geom_abline2.R

Description

The default abline of ggplot2 will exceeds the breaks and goes from one set of limits to the next. This is a modified verions that respect one will not exceed the extrema of the x and y-breaks.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
geom_abline2(
  mapping = NULL,
  data = NULL,
  method = c("breaks", "ggplot2"),
  ...,
  slope,
  intercept,
  na.rm = FALSE,
  show.legend = NA
)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_().

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 ggplot().

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)).

method

Either "breaks" (default) to respect the extrema of the axes or "ggplot2" to obtain the ggplot2 behavior.

...

Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.

slope

Parameters that control the position of the line. If these are set, data, mapping and show.legend are overridden.

intercept

Parameters that control the position of the line. If these are set, data, mapping and show.legend are overridden.

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
library(ggplot2)
library(jaspGraphs)

dfRect <- data.frame(xmin = -10, ymin = -10, xmax = 10, ymax = 10)
basePlot <- ggplot(data = dfRect, aes(xmin = xmin, ymin = ymin, ymax = ymax, xmax = xmax)) +
  geom_rect(fill = NA, color = "black") +
  geom_rangeframe() +
  themeJaspRaw(legend.position = "right")

slopes <- seq(-5, 5, length.out = 15)
ints   <- seq(-2, 2, length.out = length(slopes))
basePlot +
  geom_abline2(intercept = ints - 2, slope = slopes, method = "breaks",  color = "green") +
  geom_abline2(intercept = ints + 2, slope = slopes, method = "ggplot2", color = "red") +
  ggtitle("specify 'method' manually")

dfAbline <- data.frame(
  intercept = -9:8,
  slope = 0.35,
  method = rep(c("ggplot2", "breaks"), 9)
)

basePlot +
  geom_abline2(data = dfAbline, mapping = aes(
    intercept = intercept, slope = slope, method = method, color = method
  ), show.legend = TRUE) +
  ggtitle("specify 'method' through aes")

basePlot + geom_abline2(data = dfAbline, mapping = aes(
    intercept = intercept, slope = slope, color = method
  )) +
  ggtitle("if method is not specified, 'breaks' is used as a default")

vandenman/JASPgraphs documentation built on Dec. 16, 2021, 5:37 p.m.