1

The code is:

library(gginnards)
library(ggpmisc)
library(ggplot2)

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

ggplot(my.data, aes(x, y)) +
  geom_quadrant_lines(colour = "blue", xintercept = 50, yintercept = 10) +
  stat_quadrant_counts(colour = "blue", xintercept = 50, yintercept = 10) +
  geom_point() +
  scale_y_continuous(expand = expansion(mult = 0.15, add = 0))

The plot was shown as below: enter image description here

I'd like to know how to change the label from n=31, n=27, n=22, n=20 to p=31%, p=27%, p=22%, p=20%.

Also how to move n=27 and n=22 to the left a little bit without changing the position of n=31 and n=20? Thanks!

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
Robin
  • 325
  • 2
  • 12

2 Answers2

1

Like many functions in ggplot extensions, stat_quadrant_counts is easy to use if you want the default options, but a bit trickier if you want to customize the output. From looking at the docs, you can see the calculated stats that are available to use for calculations inside aes() in the layer, as long as you wrap them in after_stat, so we can do something like shown below to obtain percent labels if and only if the total number of observations is 100:

ggplot(my.data, aes(x, y)) +
  geom_quadrant_lines(colour = "blue", xintercept = 50, yintercept = 10) +
  stat_quadrant_counts(colour = "blue", xintercept = 50, yintercept = 10,
                       aes(label = paste0("p = ", after_stat(count), "%"),
                           npcx = ifelse(after_stat(quadrant) > 2,
                                      after_stat(npcx) - 0.1, 
                                      after_stat(npcx)))) +
  geom_point() +
  scale_y_continuous(expand = expansion(mult = 0.15, add = 0))

enter image description here

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

Generating labels with percents of observations with stat_quadrant_counts() when the total number of observations is not 100 required some changes to its definition. This update will be available in the upcoming 'ggpp' 0.5.3. Meanwhile the development version can be installed from GitHub using remotes::install_github(repo = "aphalo/ggpp").

To change the location of labels easily it is possible to pass an argument to parameter label.x that accepts NPC coordinates in the range 0..1. (This has been possible already in the current and several earlier versions.)

In the example below, using the development version of 'ggpp' we only need to select a different predefined label to obtain true percent of observations (as shown with 125 observations instead of 100 as in the question).

library(ggpmisc)  # with ggpp version > 0.5.2

set.seed(4321)
x <- 1:125
y <- rnorm(length(x), mean = 10)
my.data <- data.frame(x, y)

ggplot(my.data, aes(x, y)) +
  geom_quadrant_lines(colour = "blue", xintercept = 50, yintercept = 10) +
  stat_quadrant_counts(colour = "blue", xintercept = 50, yintercept = 10,
                       mapping = aes(label = after_stat(pc.label)),
                       label.x = c(0.02, 0.95)) +
  geom_point() +
  scale_y_continuous(expand = expansion(mult = 0.15, add = 0))

Created on 2023-07-07 with reprex v2.0.2

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23