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