Learn R Programming

ggpp (version 0.5.1)

stat_panel_counts: Number of observations in a plot panel

Description

stat_panel_counts() counts the number of observations in each panel. By default it adds a text label to the top right corner of each panel. Grouping is ignored.

stat_panel_counts() counts the number of observations in each panel. stat_group_counts() counts the number of observations in each group. By default they add one or more text labels to the top right corner of each panel. Grouping is ignored by stat_panel_counts(). If no grouping exists, the two statistics behave similarly.

Usage

stat_panel_counts(
  mapping = NULL,
  data = NULL,
  geom = "text_npc",
  position = "identity",
  label.x = "right",
  label.y = "top",
  na.rm = FALSE,
  show.legend = FALSE,
  inherit.aes = TRUE,
  ...
)

stat_panel_counts( mapping = NULL, data = NULL, geom = "text_npc", position = "identity", label.x = "right", label.y = "top", na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE, ... )

stat_group_counts( mapping = NULL, data = NULL, geom = "text_npc", position = "identity", label.x = "right", label.y = "top", hstep = 0, vstep = NULL, na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE, ... )

Value

A plot layer instance. Using as output data the counts of observations per plot quadrant.

A plot layer instance. Using as output data the counts of observations in each plot panel or per group in each plot panel.

Arguments

mapping

The aesthetic mapping, usually constructed with aes or aes_. Only needs to be set at the layer level if you are overriding the plot defaults.

data

A layer specific dataset. Rarely used, as you will not want to override the plot defaults.

geom

The geometric object to use display the data

position

The position adjustment to use on this layer

label.x, label.y

numeric Coordinates (in npc units) to be used for absolute positioning of the labels.

na.rm

a logical indicating whether NA values should be stripped before the computation proceeds.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes it if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and should not inherit behaviour from the default plot specification, e.g., borders.

...

other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details.

hstep, vstep

numeric in npc units, the horizontal and vertical step used between labels for different groups.

Computed variables

Data frame with one or more rows, one for each group of observations for which counts are counted in data.

npcx

x value of label position in npc units

npcy

y value of label position in npc units

count

number of observations

x

x value of label position in data units

y

y value of label position in data units

.

As shown in one example below geom_debug can be used to print the computed values returned by any statistic. The output shown includes also values mapped to aesthetics, like label in the example. x and y are included in the output only if mapped.

Data frame with one or more rows, one for each group of observations for which counts are counted in data.

x,npcx

x value of label position in data- or npc units, respectively

y,npcy

y value of label position in data- or npc units, respectively

count

number of observations as an integer

As shown in one example below geom_debug can be used to print the computed values returned by any statistic. The output shown includes also values mapped to aesthetics, like label in the example. x and y are included in the output only if mapped.

Details

This statistic can be used to automatically count observations in each panel of a plot, and by default add these counts as text labels. This statistic, unlike stat_quadrant_counts() requires only one of x or y aesthetics and can be used together with statistics that have the same requirement, like stat_density().

The default position of the label is in the top right corner. When using facets even with free limits for x and y axes, the location of the labels is consistent across panels. This is achieved by use of geom = "text_npc" or geom = "label_npc". To pass the positions in native data units, pass geom = "text" explicitly as argument. A vector with the same length as the number of panels in the figure can be used if needed.

These statistics can be used to automatically count observations in each panel of a plot, and by default add these counts as text labels. These statistics, unlike stat_quadrant_counts() requires only one of x or y aesthetics and can be used together with statistics that have the same requirement, like stat_density().

The default position of the label is in the top right corner. When using facets even with free limits for x and y axes, the location of the labels is consistent across panels. This is achieved by use of geom = "text_npc" or geom = "label_npc". To pass the positions in native data units to label.x and label.y, pass also explicitly geom = "text", geom = "label" or some other geometry that use the x and/or y aesthetics. A vector with the same length as the number of panels in the figure can be used if needed.

See Also

Other Functions for quadrant and volcano plots: geom_quadrant_lines(), stat_quadrant_counts()

Other Functions for quadrant and volcano plots: geom_quadrant_lines(), stat_quadrant_counts()

Examples

Run this code

# generate artificial data
set.seed(67821)
x <- 1:100
y <- rnorm(length(x), mean = 10)
my.data <- data.frame(x, y)

ggplot(my.data, aes(x, y)) +
  geom_point() +
  stat_panel_counts()

# We use geom_debug() to see the computed values

gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed) {
  library(gginnards)

  ggplot(my.data, aes(x, y)) +
    geom_point() +
    stat_panel_counts(geom = "debug")
}

ggplot(my.data, aes(x, y)) +
 geom_point() +
 stat_panel_counts(aes(label = sprintf("%i observations", after_stat(count)))) +
 expand_limits(y = 12.7)

ggplot(my.data, aes(y)) +
  stat_panel_counts(label.x = "left") +
  stat_density()


# generate artificial data
set.seed(67821)
x <- 1:100
y <- rnorm(length(x), mean = 10)
group <- factor(rep(c("A", "B"), times = 50))
my.data <- data.frame(x, y, group)

ggplot(my.data, aes(x, y)) +
  geom_point() +
  stat_panel_counts()

ggplot(my.data, aes(x, y, colour = group)) +
  geom_point() +
  stat_panel_counts()

ggplot(my.data, aes(x, y, colour = group)) +
  geom_point() +
  stat_group_counts()

ggplot(my.data, aes(x, y, colour = group)) +
  geom_point() +
  stat_group_counts(label.x = "left", hstep = 0.06, vstep = 0)

# We use geom_debug() to see the computed values

gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed) {
  library(gginnards)

  ggplot(my.data, aes(x, y)) +
    geom_point() +
    stat_panel_counts(geom = "debug")

  ggplot(my.data, aes(x, y, colour = group)) +
    geom_point() +
    stat_group_counts(geom = "debug")

}

ggplot(my.data, aes(x, y)) +
 geom_point() +
 stat_panel_counts(aes(label = sprintf("%i observations", after_stat(count)))) +
 expand_limits(y = 12.7)

ggplot(my.data, aes(y)) +
  stat_panel_counts(label.x = "left") +
  stat_density()

ggplot(my.data, aes(y, colour = group)) +
  stat_group_counts(label.y = "top") +
  stat_density(aes(fill = group))

Run the code above in your browser using DataLab