0

I am currently trying to assign certain colours to a legend or the text of the legend in ggplot 2. There are two entries in the legend, each of which should have a certain colour.

The plot currently looks like this:

The goal is that the text of the legend has the same colour as the corresponding graph.

Important note: The colours of the graphs are defined in extra variables and do not have to be extracted from the plot. The variables for the Legend-text color are stored in "Nutzer1Farbe" and "Nutzer2Farbe".

If anyone has a solution to my problem, I would be very grateful!

Have a nice weekend

2
  • @norie I already saw thsi question and tried to implement the solutions given. Sadly non of the solutions worked for me. But as I am relatively inexperienced in the area, it may also be due to the adaptation of the solutions to my code. However, after my countless attempts, nothing suggested has worked.
    – LePyka
    Commented Jul 10, 2021 at 16:20
  • Why did you remove all the code & data from the post?
    – camille
    Commented Jul 20, 2021 at 4:40

1 Answer 1

1

try with ggtext and next code:

library(ggtext)
library(tidyverse)
#Code
datengesamt <- datengesamt %>% 
  # Convert to datetime
  mutate(month = as.POSIXct(month))

plot.new()
ps <- xspline(datengesamt[,1], datengesamt[,2], 1, draw=FALSE)
pg <- xspline(datengesamt[,1], datengesamt[,3], 1, draw=FALSE)

pp <- list("Person 1" = data.frame(ps), "Person 2" = data.frame(pg)) %>% 
  bind_rows(.id = "id") %>% 
  mutate(x = lubridate::as_datetime(x))
#Plot
colors <- c('red','blue')
ggplot(pp, aes(x, y, color = id, linetype = id))+
  theme(axis.line = element_line(colour = Farbe),
        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.95,0.95),
        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),
  )+
  geom_path(size=0.5) +
  scale_color_manual(labels = paste("<span style='color:",
                                   colors,
                                   "'>",
                                   unique(pp$id),
                                   "</span>"),
                    values = colors)+
  scale_linetype_manual(labels = paste("<span style='color:",
                                          colors,
                                          "'>",
                                          unique(pp$id),
                                          "</span>"),
                        values=c(Nutzer1Format,Nutzer2Format))+
  scale_y_continuous(breaks = scales::pretty_breaks(n = 2))+
  scale_x_datetime(date_labels = "%Y", breaks = scales::pretty_breaks(n = 3), expand = expansion(mult = c(0.02, 0.03)))+
  xlab("Jahr")+
  ylab("# Nachrichten")+
  theme(axis.text.x = element_text(colour = Farbe))+
  theme(axis.text.y = element_text(angle = 90,colour = Farbe),
        legend.text = element_markdown())+
  labs(color='id',linetype='id')

Output:

enter image description here

2
  • Works good - but i struggle a bit to keep the label text right. The text should be stored in the variables "Nutzer1" and "Nutzer2" - but i keep trying :) Thanks!
    – LePyka
    Commented Jul 10, 2021 at 19:41
  • After some trying i found the solution. Thank u very much! :) Changed " unique(pp$id)" to "unique(c(Nutzer1, Nutzer2))"
    – LePyka
    Commented Jul 10, 2021 at 19:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.