I make a plot with a log10()
transformed x axis and I want to add labels to it. Let's assume I am making a scatter plot and for some reason I want to label the points. I do not want the points and letters to overlap, therefore I shift the labels a little to the right. R Code:
n <- 5
df <- data.frame(x= seq(.1, 3, length.out= n), y= 1:n, lab= letters[1:n])
library(ggplot2)
ggplot(df) +
geom_point(aes(x, y, col= lab), show.legend= FALSE) +
geom_text(aes(x + 1, y, label= lab, col= lab), show.legend= FALSE) +
scale_x_continuous(transform= "log10")
Because of the transformation the shift between the first point (.1,1) and its label ("a") seems much bigger than the other. How to add labels that all have the same visual shift from their points? I tried all kind of things, but the visual gap between point and label always varies.
A possible solution, that will work with any plotting package, is to take advantage of the fact that multiplying on a linear scale is the same as adding on a log scale. If you multiply the x-coordinate by a constant on the original scale, and then log-transform, you will get labels that are each shifted by the same amount on the new log scale. If the constant is >1, they will be shifted to the right. For example:
ggplot(df) +
geom_point(aes(x, y, col = lab), show.legend = FALSE) +
geom_text(aes(x * 1.1, y, label = lab, col = lab), show.legend = FALSE) +
scale_x_continuous(transform = "log10")
In this case, turning off clipping is not necessary because the scale will expand to accommodate the labels.