0

I am currently trying to customize the legend for one of my plots. Unfortunately, my result doesn't look the way I want it to right now. The legend label should be on line with the linetype. But currently it looks like this:

enter image description here

The "Toni" and "Lennart" should be on one level with the strokes and I just can't figure out why they are shifted.

My code looks like this:

    colors <- c(Farbe,Farbe)

  
  PlotLine <- ggplot(pp, aes(x, y, color = id, linetype = id))+
    theme(axis.line = element_line(colour = Farbe, size = 18),
          panel.background = element_rect(fill = "transparent", color = NA),
          plot.background = element_rect(fill= "transparent", color = NA),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          legend.position = c(0.75,1),
          legend.key.size = unit(2, 'cm'),
          legend.title = NULL,
          legend.key= element_blank(),
          legend.background = element_rect(fill = "transparent", colour = NA), # get rid of legend bg
          legend.box.background = element_rect(fill = "transparent", colour = NA),
          axis.title.x=element_blank(),
          #axis.text.x=element_blank(),
          axis.ticks.x=element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank(),
          axis.line.x=element_blank(),
          axis.line.y=element_blank(),
          legend.direction="horizontal"
    )+
    geom_path(size=2) +
    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,
                       guide = guide_legend(
                         direction = "horizontal",
                         label.hjust = 0.0))+
    scale_linetype_manual(labels = paste("<span style='color:",
                                         colors,
                                         "'>",
                                         unique(c(Nutzer1, Nutzer2)),
                                         "</span>"),
                          values=c("solid","dotted"))+
    xlab("")+
    theme(text=element_text(size=72,  family="roboto", face="bold"), axis.ticks = element_blank())+
    ylab("")+
    theme(axis.text.x = element_text(size= 90, colour = "black", margin = margin(t = 10, b = -5)))+
    theme(axis.text.y = element_blank(), legend.text = element_markdown(size = 72))+
    labs(color=' ',linetype=' ')

I apologize for the cluttered code, I haven't had a chance to clean this up :D

Unfortunately, it is currently not readily possible to export the data so you can reproduce the diagram, I hope it works that way.

Thank you!

LePyka
  • 161
  • 8
  • Please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Your code works fine. My guess is that your legend labels `Nutzer1`, ... include some line breaks and trailing spaces. – stefan Oct 13 '21 at 05:27

0 Answers0