When extending ggplot2, how should I apply scaling functions per group instead of as a whole?

I’m working on a new geom for ggplot2 that allows the user to alter the tint and shade of the colors by mapping it to an aesthetic. (If someone knows of a place this has already been implemented, please save me the headache!) I’ve got the basics of the geom figured out but I’m not sure that I’m applying the scales correctly because I’m having to rescale the data within draw_panel to use the colour groups instead of it happening automatically.

library(ggplot2)
library(rlang)
library(colorspace)
library(grid)
source("https://raw.githubusercontent.com/tidyverse/ggplot2/7fb4c382/R/utilities-grid.R")

geom_point_tintshade <- function(mapping = NULL, data = NULL,
                                 stat = "identity", position = "identity",
                                 ..., na.rm = FALSE, show.legend = NA,
                                 inherit.aes = TRUE) {
  layer(data = data, mapping = mapping, stat = stat, geom = GeomPointTintshade,
        position = position, show.legend = show.legend, inherit.aes = inherit.aes,
        params = list2(na.rm = na.rm, ...))
}
GeomPointTintshade <- ggproto("GeomPointTintshade", GeomPoint,
                              required_aes = c("x", "y"),
                              non_missing_aes = c("size", "shape", "colour", "tintshade"),
                              default_aes = aes(
                                shape = 19, colour = "black", size = 1.5, fill = NA,
                                alpha = NA, tintshade=0.5
                              ),
                              setup_data = function(data, params){
                                assign("data", data, envir = .GlobalEnv)
                                data$tintshade_group <- data$colour
                                data
                              },
                              draw_panel = function(self, data, panel_params, coord, na.rm = FALSE) {
                                coords <- coord$transform(data, panel_params)
                      # I feel like I shouldn't have to rescale tintshade here...
                                full_range <- range(coords$tintshade)
                                coords_rescaled <- tapply(
                                  X = coords$tintshade, INDEX = coords$tintshade_group,
                                  FUN = function(x) scales::rescale(rank(x), full_range)
                                  )
                                coords$tintshade <- unsplit(coords_rescaled, coords$tintshade_group)
                      # but if I don't then the rescaling isn't applied by group,
                      # instead once as a whole
                                coords$colour <- lighten(coords$colour, amount = (coords$tintshade*2)-1)
                                ggname("geom_point_tintshade",
                                       pointsGrob(
                                         coords$x, coords$y,
                                         pch = coords$shape,
                                         gp = gg_par(
                                           col = alpha(coords$colour, coords$alpha),
                                           fill = fill_alpha(coords$fill, coords$alpha),
                                           pointsize = coords$size
                                         )
                                       )
                                )
                              },
                              draw_key = function(self, ...) draw_key_point(...)
)
scale_tintshade_discrete <- function(name = waiver(), ..., range = c(0.2, 0.8)) {
  discrete_scale(
    aesthetics="tintshade", name = name, ...,
    palette = function(n) seq(range[1], range[2], length.out = n)
  )
}
metab_data <- data.frame(
  metab=rep(c("Alanine", "Threonine", "Glycine", "GBT", "Proline betaine",
              "Carnitine", "DMSP", "DMS-Ac", "Isethionate"), 3),
  metab_group=rep(rep(c("Amino acid", "Betaine", "Sulfur"), each=3), 3),
  tripl=rep(c("A", "B", "C"), each=9),
  area=runif(27)
)
ggplot(metab_data) +
  geom_point_tintshade(aes(x = tripl, y = area, color = metab_group, tintshade = metab), size=4)

Ideally I’d like to skip the setup_data and where I add the tintshade_group column and instead have the non-positional scale training perform this for me (since tintshade_group is just the colour column renamed to avoid scaling) instead of needing to recalculate it within draw_panel but I can’t figure out how to tell ggplot that there are groups to scale within instead of just mapping the values as a whole.

Right now the data in coords (pre-recalculation) looks like:

   colour tintshade      x         y PANEL group tintshade_group shape size fill alpha
1 #F8766D     0.200 0.1875 0.5387449     1     1      Amino acid    19    4   NA    NA
2 #F8766D     0.800 0.1875 0.6519035     1     3      Amino acid    19    4   NA    NA
3 #F8766D     0.575 0.1875 0.9545455     1     2      Amino acid    19    4   NA    NA
4 #00BA38     0.500 0.1875 0.6811014     1     5         Betaine    19    4   NA    NA
5 #00BA38     0.725 0.1875 0.2865486     1     6         Betaine    19    4   NA    NA
6 #00BA38     0.275 0.1875 0.3901051     1     4         Betaine    19    4   NA    NA

and I would instead like to have the initial output of coords be (note the different values in the “tintshade” column)

   colour tintshade      x         y PANEL group tintshade_group shape size fill alpha
1 #F8766D     0.200 0.1875 0.5387449     1     1      Amino acid    19    4   NA    NA
2 #F8766D     0.800 0.1875 0.6519035     1     3      Amino acid    19    4   NA    NA
3 #F8766D     0.500 0.1875 0.9545455     1     2      Amino acid    19    4   NA    NA
4 #00BA38     0.500 0.1875 0.6811014     1     5         Betaine    19    4   NA    NA
5 #00BA38     0.800 0.1875 0.2865486     1     6         Betaine    19    4   NA    NA
6 #00BA38     0.200 0.1875 0.3901051     1     4         Betaine    19    4   NA    NA

Is this possible to do with existing functions? Or am I actually doing this the correct way? Part of the motivation for this is that I’m running into downstream problems when trying to construct a legend where it’s more difficult to recalculate these values (but that may be a later question…)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật