I struggle with putting the abline in the legend. It works fine with the points and their colour, but as far as I succeed is the six lines overlapping with the points... Can I just somehow add another legend element with the black line labeled as "LMWL"? I know that I can post-edit this in Corel or any software but I would like to do it in the code...
ggplot(df_merge_iso, aes(x = d18O, y = d2H, color = category, shape = category, label = month)) +
labs(
x = expression(paste(delta^{18}, "O (\u2030)")),
y = expression(paste(delta^{2}, "H (\u2030)"))) +
geom_point(size = 1, stroke = 1) +
theme_minimal() +
theme(
axis.text = element_text(size = 12, color = "black"),
axis.title = element_text(size = 12, color = "black")
) +
scale_color_manual(
name = "",
values = c(
"A" = "#0015f0",
"B" = "#6AC4FF",
"C" = "#4B8CEB",
"D" = "#FF7E5F",
"E" = "#FFB300",
"F" = "#E75007"
)) +
scale_shape_manual(
name = "",
values = c(
"A" = 2,
"B" = 1,
"C" = 21,
"D" = 3,
"E" = 4,
"F" = 6
)) +
geom_text_repel(
vjust = -1, hjust = 0.5, size = 2, show.legend = FALSE,
box.padding = 0.2
) +
geom_abline(slope = lmwl_slope_wgt, intercept = lmwl_intercept_wgt,
colour = "black", linetype = "solid", size = 0.5)
If you want a legend you have to map on aesthetics. In case you want to add a legend entry to the already present color
and shape
legend you could do by mapping e.g. the constant "LMWL"
on color
and shape
in geom_abline
which also requires to move intercept
and slope
inside aes()
. Additionally you have to provide a color and a shape for LMWL in the scales.
Using some fake example data:
library(ggrepel)
#> Loading required package: ggplot2
library(ggplot2)
df_merge_iso <- data.frame(
d18O = 1:6,
d2H = 1:6,
category = LETTERS[1:6],
month = letters[1:6]
)
lmwl_slope_wgt <- .4
lmwl_intercept_wgt <- 2
p <- ggplot(df_merge_iso, aes(x = d18O, y = d2H, color = category, shape = category, label = month)) +
labs(
x = expression(paste(delta^{
18
}, "O (\u2030)")),
y = expression(paste(delta^{
2
}, "H (\u2030)"))
) +
geom_point(size = 1, stroke = 1) +
theme_minimal() +
theme(
axis.text = element_text(size = 12, color = "black"),
axis.title = element_text(size = 12, color = "black")
) +
scale_color_manual(
name = "",
values = c(
"A" = "#0015f0",
"B" = "#6AC4FF",
"C" = "#4B8CEB",
"D" = "#FF7E5F",
"E" = "#FFB300",
"F" = "#E75007",
LMWL = "black"
)
) +
scale_shape_manual(
name = "",
values = c(
"A" = 2,
"B" = 1,
"C" = 21,
"D" = 3,
"E" = 4,
"F" = 6,
LMWL = 1
)
) +
geom_text_repel(
vjust = -1, hjust = 0.5, size = 2, show.legend = FALSE,
box.padding = 0.2
)
p +
geom_abline(
aes(
color = "LMWL", shape = "LMWL",
slope = lmwl_slope_wgt, intercept = lmwl_intercept_wgt
),
linetype = "solid", size = 0.5
)
If you want a separate legend you could fake one by mapping e.g. on the linetype
aes:
p +
geom_abline(
aes(
linetype = "LMWL",
slope = lmwl_slope_wgt, intercept = lmwl_intercept_wgt
),
size = 0.5
) +
scale_linetype_manual(name = NULL, values = "solid")