I am trying to create faceted density plots with one (or more) of the variables transformed by my own function log10p_trans
:
library(ggplot2)
library(tidyr)
library(reshape)
library(forcats)
library(scales)
log10p_trans <- trans_new(name = "log10p_trans",
transform = function(x) log10(x + 1),
inverse = function(x) exp(x) - 1)
rex <- data.frame("value1" = rexp(100, rate = 1),
"value2" = rexp(100, rate = 2))
rex_pivot <- data.frame(pivot_longer(rex, cols = 1:2))
ggplot(rex_pivot, aes(x = value, y = ..density..)) +
geom_density() +
facet_wrap(facets = ~name, scales = "free_x") +
ggh4x::scale_x_facet(
name == "value1",
trans = log10p_trans)
However, the problem is that the labels (in the left panel) are really off and also “end” way earlier than they should. Also, the grid lines don’t seem to be the logarithmic ones:
What can I do to fix that?