Put letter outside the plot

Hello!

I have done a plot and I would like to put a and b letters outside the black box but I am not able to do it in R. When I put the coordinates outside the plot the letters do not appear... Does someone know how to fix it?

This is my code plot:
PlotCon <- ggplot(df_combined, aes(x = Time, y = Intensity, color = Tau)) +
geom_line(linewidth = 0.7) +
facet_wrap(~ Source, ncol = 2) +
scale_x_log10(
limits = c(1e-3, 1e1),
breaks = 10^(seq(-3, 2, by = 1)),
labels = trans_format("log10", math_format(10^.x)),
expand = c(0, 0)
) +
scale_y_continuous(limits = c(0, 2), breaks = seq(0, 2, by = 0.5), expand = c(0, 0)) +
scale_color_manual(values = tau_colors) +
labs(
x = "Time (s)",
y = expression("Intensity a.u."),
color = "Tau Values"
) +
theme_classic(base_size = 13, base_family = "Times") +
theme(
panel.border = element_rect(color = "black", fill = NA, linewidth = 0.2),
axis.line = element_line(color = "black", linewidth = 0.2),
axis.ticks = element_line(color = "black"),
panel.grid = element_blank(),
axis.text = element_text(size = 12),
panel.spacing = unit(1.5, "cm"),
strip.text = element_blank(),
legend.position = "right"
)

print(PlotCon)

What I would like:

I have tried - but does not work

PlotCon <- PlotCon +
coord_cartesian(ylim = c(0, 2.3), clip = "off") # extra space above 2
PlotCon <- PlotCon +
coord_cartesian(clip = "off") +
theme(panel.overflow = "visible") # new in ggplot2 3.4+

Hi @mariamendi,

I think what you are looking for is:

PlotCon <- PlotCon + labs(tag = "a)")

I don't know how this works in combination with facet_wrap(). Maybe you find something in the docs or try to find a solution via theme(strip.text = element_text()) for the positioning of the letters.

So, I think easiest would be not to combine the dataframes (as you probably did: data = df_combined) but leave them separate, create two plots (avoid duplication of the code by creating a function) and combine them with patchwork like:

library(ggplot2)
library(patchwork)

create_con_plot <- function(df, tag, print_legend = TRUE){

p <- ggplot(
    data = df,
    aes(x = Time, y = Intensity, color = Tau)
) +
    geom_line(linewidth = 0.7) +
    # facet_wrap(~ Source, ncol = 2) +
    scale_x_log10(
        limits = c(1e-3, 1e1),
        breaks = 10^(seq(-3, 2, by = 1)),
        labels = trans_format("log10", math_format(10^.x)),
        expand = c(0, 0)
    ) +
    scale_y_continuous(
        limits = c(0, 2),
        breaks = seq(0, 2, by = 0.5),
        expand = c(0, 0)
    ) +
    scale_color_manual(values = tau_colors) +
    labs(
        x = "Time (s)",
        y = expression("Intensity a.u."),
        color = "Tau Values",
        tag = tag
    ) +
    theme_classic(base_size = 13, base_family = "Times") +
    theme(
    panel.border = element_rect(color = "black", fill = NA, linewidth = 0.2),
    axis.line = element_line(color = "black", linewidth = 0.2),
    axis.ticks = element_line(color = "black"),
    panel.grid = element_blank(),
    axis.text = element_text(size = 12),
    # panel.spacing = unit(1.5, "cm"),
    # strip.text = element_blank(),
    legend.position = "right"
)

# Allow to print only one legend
if (!print_legend){
    p <- p + theme(legend.position = "none")
}

return(p)

}

plt_a <- create_con_plot(df_a, tag  = "a)",  print_legend = FALSE) 
plt_b <- create_con_plot(df_b, tag = "b)")

plt <- plt_a | plt_b

plt

Hope this works and helps you :wink:

Cheers

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.