Home > Mobile >  How to add legend and color by group in ggplot without melting data?
How to add legend and color by group in ggplot without melting data?

Time:08-24

I have 3 datasets, of which one of them contains a two different variables. Observe:

        order_dates            Value
1     2022-08-14 12:15:10       1
2     2022-08-14 12:15:11       5
3     2022-08-14 12:15:13       2
4     2022-08-14 12:20:10       3

        order_dates            Value
1     2022-08-14 12:15:10       4
2     2022-08-14 12:15:11       1
3     2022-08-14 12:15:13       8
4     2022-08-14 12:20:10       1


        order_dates            Value     Type
1     2022-08-14 12:15:10       5         1
2     2022-08-14 12:15:11       13        1
3     2022-08-14 12:15:13       9         1
4     2022-08-14 12:20:10       7         1
5     2022-08-14 12:15:10       6         2
6     2022-08-14 12:15:11       0         2
7     2022-08-14 12:15:13       9         2
8     2022-08-14 12:20:10       4         2

I would like 4 lines on a chart. 2 from the first two datasets and 2 from the last dataset with group = 'type'. I would like all 4 lines to have different colors and a legend as well. Is there any way to do this without combining the dataset?

CodePudding user response:

First off, I advise against "manually" drawing 4 lines and would encourage to use the more ggplot-canonical way of combining melting data.

If you must, you can do the following:

library(ggplot2)
ggplot()   
    aes(order_dates, Value)  
    geom_line(data = df1, aes(colour = "df1_label"))  
    geom_line(data = df2, aes(colour = "df2_label"))  
    geom_line(data = df3, aes(colour = as.factor(Type)))

enter image description here

You can adjust the labels for the colour aesthetic with scale_colour_manual().

For example

ggplot()   
    aes(order_dates, Value)  
    geom_line(data = df1, aes(colour = "df1_label"))  
    geom_line(data = df2, aes(colour = "df2_label"))  
    geom_line(data = df3, aes(colour = as.factor(Type)))   
    scale_colour_manual(
        values = c("orange", "grey", "blue", "red"),
        labels = c(
            "df1_label" = "label for df1",
            "df2_label" = "label for df2",
            "1" = "label for df3, type = 1",
            "2" = "label for df3, type = 2"))

produces enter image description here


Source data

df1 <- structure(list(order_dates = structure(c(1660443310, 1660443311, 
1660443313, 1660443610), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Value = c(1L, 5L, 2L, 3L)), class = "data.frame", row.names = c("1", 
"2", "3", "4"))
df2 <- structure(list(order_dates = structure(c(1660443310, 1660443311, 
1660443313, 1660443610), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Value = c(4L, 1L, 8L, 1L)), class = "data.frame", row.names = c("1", 
"2", "3", "4"))
df3 <- structure(list(order_dates = structure(c(1660443310, 1660443311, 
1660443313, 1660443610, 1660443310, 1660443311, 1660443313), class = c("POSIXct", 
"POSIXt"), tzone = ""), Value = c(5L, 13L, 9L, 7L, 6L, 0L, 9L
), Type = c(1L, 1L, 1L, 1L, 2L, 2L, 2L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7"))
  • Related