Data:
dat <-
structure(
list(
x0 =
c(52, 55.5, 50.5, 42, 65, 81, 58, 63.5,
77.5, 66.5, 89.5, 98.5, 52, 97.5, 153.5, 84.5, 126, 162.5, 136.5,
189.5, 315, 365, 418, 448.5, 212.5, 380.5, 279.5, 277, 332.5,
353, 175.5, 437, 50, 53, 49, 39, 62, 76.5, 57.5, 61, 72.5, 63,
89, 96, 50.5, 93.5, 152, 82.5, 124, 153.5, 132.5, 190, 316, 366.5,
439.5, 450, 211, 379, 280, 277.5, 329, 362.5, 175.5, 438.5, 206,
250, 181, 280, 182, 748, 963, 1340.5, 734.5, 753, 849, 236, 268.5,
194, 230, 466, 249, 182.5, 204.5, 174.5, 250.5, 218.5, 228, 260.5,
206.5, 255.5, 195.5, 308, 417.5, 231.5, 201, 171, 153, 168, 3861,
1994, 155.5, 171.5, 169, 176, 181, 184.5, 182.5, 196, 192, 206,
202.5, 206.5, 207.5, 218.5, 230, 230.5, 234.5, 238, 251, 251,
251, 256, 260.5, 271, 281.5, 305.5, 416, 466.5, 740, 753, 760.5,
846.5, 969.5, 1352.5, 1945.5, 3819.5),
y0 = c(-2, 1, -5,-10, -2, -12, -6, -1, -11, -5, -1, -1, -2, 1, 1, -1, -2, -15,
-5, 9, 8, 10, 64, 11, 1, 5, -3, 8, 5, 28, 13, 16, 2, 6, -2, -4,
4, -3, -5, 4, -1, 2, 0, 4, 1, 9, 4, 3, 2, 3, 3, 8, 6, 7, 21,
8, 4, 8, -4, 7, 12, 9, 13, 13, -14, 0, -8, -8, 0, 20, 32, 37,
19, 24, 8, -4, -3, -10, 2, -12, -10, -5, -7, -1, 1, 1, -6, 5,
-3, -3, -9, -4, -1, -11, -4, 0, 0, 6, 8, -66, -5, -1, 4, -4,
-8, -9, -1, -14, -2, -14, -7, -11, -5, 1, -10, -9, -7, -8, -14,
-2, 0, -4, 5, -8, -11, 1, 2, -13, 8, 10, 9, 13, 19, 13, 31, 91)),
class = "data.frame", row.names = c(NA, -136L))
Looking at the linear regression below, the points are too concentrated and the scales are inappropriate.
library(ggplot2)
library(dplyr)
library(scales)
library(patchwork)
# axes limits, breaks, and labels
x_limlow <- 30
x_limup <- 4000
x_brk <- c(30,50,100,200,500,1000,2000,4000)
x_lab <- c(30,50,100,200,500,1000,2000,4000)
y_limlow <- -100
y_limup <- 150
y_brk <- c(-100,-50,-25,-10,0,10,25,50,100,150)
y_lab <- c(-100,-50,-25,-10,0,10,25,50,100,150)
# plot no transformation
plot_no_trans <-
ggplot(dat, aes(x0, y0)) +
ggtitle("no transformation") +
scale_x_continuous(limits = c(x_limlow, x_limup), expand = c(0,0), breaks = x_brk, labels = x_lab) +
scale_y_continuous(limits = c(y_limlow, y_limup), expand = c(0,0), breaks = y_brk, labels = y_lab) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid.major = element_line(colour = "grey75", linewidth = 0.1)) +
theme(plot.margin = unit(c(0.2, 0.5, 0.1, 0.2), "cm")) +
geom_smooth(method = "lm", se = FALSE, colour = "green2", size = 1) +
geom_hline(yintercept = 0) +
geom_point(color = "green4", size = 2) +
# linear regression title
annotate(
geom = "label",
x = x_limlow + (0.05 * (x_limup - x_limlow)),
y = y_limlow + (0.95 * (y_limup - y_limlow)),
label = c("linear regression"),
size = 4, color = "black", fill = "white", label.size = NA, hjust = 0, vjust = 0.5) +
# linear regression equation
geom_segment(
aes(
x = x_limlow + (0.05 * (x_limup - x_limlow)),
xend = x_limlow + (0.15 * (x_limup - x_limlow)),
y = y_limlow + (0.85 * (y_limup - y_limlow)),
yend = y_limlow + (0.85 * (y_limup - y_limlow))),
size = 1, color = "green2", linetype = "solid") +
annotate(geom = "label",
x = x_limlow + (0.16 * (x_limup - x_limlow)),
y = y_limlow + (0.85 * (y_limup - y_limlow)),
label = c("y = ax + b"),
size = 4, color = "black", fill = "white", label.size = NA, hjust = 0, vjust = 0.5) +
# number of points
annotate(
geom = "label",
x = x_limlow + (0.05 * (x_limup - x_limlow)),
y = y_limlow + (0.75 * (y_limup - y_limlow)),
label = c("n = 136"),
size = 4, color = "black", fill = "white", label.size = NA, hjust = 0, vjust = 0.5)
plot_no_trans
If I transform the x0 axis with coord_trans(x = "log10"), the x scale is better but it shifts the texts and the segment to the right:
# plot x0 log10 transformation
plot_x0_log10 <-
plot_no_trans +
ggtitle("x0 log10 transformation") +
coord_trans(x = "log10")
plot_x0_log10
Now, if I also modulus(0.5) transform the y0 axis using coord_trans, scales are even better, but it also shifts the texts and the segment to the top, unfortunately:
# plot x0 log10 transformation and y0 modulus(0.5) transformation
modulus0.5 <- scales::modulus_trans(0.5)
plot_x0_log10_y0_modulus <-
plot_no_trans +
ggtitle("x0 log10 and y0 modulus(0.5) transformation") +
coord_trans(x = "log10", y = modulus0.5)
plot_x0_log10_y0_modulus
Question:
How to position the texts and segment at the same positions (as on the initial non-transformed graph) on the 3 graphs while using coord_trans?
Thanks for help


