1

I have a rather specific problem for which I am currently looking for a solution and cannot find one.

I would like to create a legend with ggplot where the caption of the legend is below the legend - I don't know how to explain it better. I'm sure the following pictures will help:

How I would like the legend to be:

enter image description here

How the legend is current:

enter image description here

I draw the plot at the end via cowplot on a background (draw_plot), so there is also the possibility of drawing the legend "artificially" over the diagram (draw_text). However, I would have to manage to underline the text - and with the correct linetype (see diagram below). This would actually be my preferred variant - but I have no clue how this could work.

I provide you with the code for the plot and for the character on the background.

I am curious to see if anyone can find a solution! :)

Thank you and best regards


The whole Diagramm:

enter image description here


The ggplot-code (shortened):

colors <- c("#ff9a00","#ff9a00")

PlotLine <- ggplot(pp, aes(x, y, color = id, linetype = id))+
  theme(legend.direction="horizontal"
  )+
  geom_path(size=1.25) +
  scale_x_datetime(date_labels = "%Y", breaks = scales::pretty_breaks(n = 3), expand = expansion(mult = c(0.02, 0.03)))+
  scale_color_manual(labels = paste("<span style='color:",
                                    colors,
                                    "'>",
                                    unique(c(Nutzer1, Nutzer2)),
                                    "</span>"),
                     values = colors)+
  scale_linetype_manual(labels = paste("<span style='color:",
                                       colors,
                                       "'>",
                                       unique(c(Nutzer1, Nutzer2)),
                                       "</span>"),
                        values=c("solid","dotted"))+
  xlab("")+
  ylab("")+
  theme(axis.text.x = element_text(size= 10, colour = "black", margin = margin(t = 10, b = -5)))+
  theme(axis.text.y = element_blank(), legend.text = element_markdown(size = 8))+
  labs(color=' ',linetype=' ')

cowplot-code (shortend):

Ausgabe <- ggdraw() + draw_plot(PlotLine, width = 0.6, height = 0.18, x = 0.345, y = 0.094)


Data for the Lineplot:

The variables for "Nutzer1" and "Nutzer2" can be filled with anything. Sadly the referred dataframe "pp" is too long to export and post. But i guess any dummy data should do it ;)

LePyka
  • 181
  • 8

1 Answers1

0

Inside your scale_color_manual() add an argument about the "guide":

 scale_color_manual(labels = paste("<span style='color:",
                                    colors,
                                    "'>",
                                    unique(c(Nutzer1, Nutzer2)),
                                    "</span>"),
                     values = colors,
                     guide = guide_legend(
                             direction = "horizontal",
                             label.position = "top"))

This will specify that your label should be on top of the key rather than to the right as it defaults.

Tiberius
  • 11
  • 4