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