problem with legend in ggplot with multiple lines

Hey there,

I have some trouble with my ggplot graph here:

plot_m_overlap <-  ggplot(data=agg_sums, aes(x=datum))+
  geom_line(aes(y=med1_p),color = "#c381fd", size = 0.9)+
  geom_line(aes(y=med2_p), color="#4815aa",   size=  0.9)+
  geom_line(aes(y=med3_p), color="#f2626b" , size=  0.9)+
  geom_line(aes(y=med4_p), color="#feba4f",  size=  0.9)+
  geom_line(aes(y=med11_p), color="#83c3ff",  size=  0.9)+
  geom_line(aes(y=med12_p), color="#e5e500",  size=  0.9)+
  theme( text=element_text(size=12,  family="TT Times New Roman"))

plot_m_overlap <-plot_m_overlap + labs(x = "Abstimmungen", y="Prozentuale Nutzung",  title = "")+
  ylim(0, 100)

I tried a lot of things but did not succeed in creating a legend for this plot. Has anyone an idea? Would be very greatful!

Thanks,
Johanna

The usual method would be to pivot the data to a longer format from which a legend can be automatically generated by ggplot. If I understand your data layout correctly, the code might be similar to this.

library(ggplot2)
agg_sums <- data.frame(datum = 1:10,
                 med1_p = runif(10),
                 med2_p = runif(10),
                 med3_p = runif(10),
                 med4_p = runif(10))
                 
       
head(agg_sums)
#>   datum      med1_p    med2_p     med3_p     med4_p
#> 1     1 0.439338266 0.3121291 0.08993702 0.09346780
#> 2     2 0.935300715 0.6294467 0.16741870 0.01653311
#> 3     3 0.886788295 0.3093214 0.19975073 0.54193333
#> 4     4 0.374733837 0.7632503 0.16381668 0.01761119
#> 5     5 0.507845334 0.4857766 0.51790104 0.35005079
#> 6     6 0.004120674 0.9495438 0.80449728 0.80746180
library(tidyr)
aggNew <- agg_sums %>% pivot_longer(cols = med1_p:med4_p, 
                                    names_to = "MED", 
                                    values_to = "Value")
head(aggNew)
#> # A tibble: 6 x 3
#>   datum MED     Value
#>   <int> <chr>   <dbl>
#> 1     1 med1_p 0.439 
#> 2     1 med2_p 0.312 
#> 3     1 med3_p 0.0899
#> 4     1 med4_p 0.0935
#> 5     2 med1_p 0.935 
#> 6     2 med2_p 0.629

COLORS <- c(med1_p = "#c381fd", med2_p ="#4815aa",  
            med3_p = "#f2626b" , med4_p = "#feba4f")
ggplot(aggNew, aes(x = datum, y = Value, group = MED, color = MED)) +
  geom_line(size = 0.9) +
  scale_color_manual(values = COLORS)

Created on 2020-08-08 by the reprex package (v0.3.0)

2 Likes

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

yeah, it worked, thanks again :slight_smile:

Thanks a lot!! I'll try that.

Take care,
Johanna

however, now, I struggle with reorder the legend as I do not want it ordered alphabetically but easy to read (pink line first, then red line, then violet one and last the orange one). What would I have to do to fix that problem? :thinking:

You can reorder the categories in the legend using the factor() function. Here is an example, though it does not give the particular order you want.

aggNew$MED <- factor(aggNew$MED, levels = c("med3_p", "med2_p", "med4_p", "med1_p")) 
1 Like