I am trying to make a graph using ggplot2
with a log scale on the y-axis to plot small values, including negative values. It currently looks similar to this:
df <- data.frame(matrix(ncol = 2, nrow = 4))
names(df) <- c("x", "y")
df$x <- as.factor(c(1, 2, 3, 4))
df$y <- c(0.1, 0.2, -0.3, 0.05)
ggplot(df, aes(x = x, y= y)) +
geom_point() +
scale_y_log10()
The plot that comes out of that looks like this:
As you can see, one value is missing. This is a know problem, and I've seen a solution for a similar question with larger values on the x-axis: I need ggplot scale_x_log10() to give me both negative and positive numbers as output which would work out to:
weird <- scales::trans_new("signed_log",
transform=function(x) sign(x)*log(abs(x)),
inverse=function(x) sign(x)*exp(abs(x)))
df <- data.frame(matrix(ncol = 2, nrow = 4))
names(df) <- c("x", "y")
df$x <- as.factor(c(1, 2, 3, 4))
df$y <- c(0.1, 0.2, -0.3, 0.05)
ggplot(df, aes(x = x, y= y)) +
geom_point() +
scale_y_continuous(trans=weird)
But this solution doesn't work for me. As that post warns, this solution doesn't work for small numbers so it comes up with nonsense:
How can I use a log scale for these small and negative values? In this example, all values are small and a log scale is not needed, but I have several graphs where some values are much larger and the log scale is needed to distinguish the small values from each other.