Home > Blockchain >  Plotting a line in ggplot with three different subsets with different colors
Plotting a line in ggplot with three different subsets with different colors

Time:09-22

I'm trying to creat a plot with 3 colors on the same line based on the "Recorte column" which you can check on the sample bellow:

Legenda           `Hora da publicaçã~ Alcance `Curtidas e rea~ Comentários Compartilhament~ Resultados
  <chr>             <dttm>                <dbl>            <dbl>       <dbl>            <dbl> <lgl>     
1 "Lucielle Lauren~ 2020-10-13 05:49:48      93                0           0                0 NA        
2 "Lucielle Lauren~ 2020-10-13 05:57:12    2827              164          10                0 NA        
3 "Assista agora a~ 2020-10-13 15:34:43    2641               68           1                0 NA        
4 "Acompanhe agora~ 2020-10-23 09:07:32    3063               65           1                0 NA        
5 "\U0001f535 Esta~ 2020-11-12 16:29:55    4578              105          17                0 NA        
6 "Em respeito ao ~ 2020-10-23 16:48:41    4891              227          17                6 NA 

This is the column which i want to use to subset the line in 3 diferent labels, representing 3 stages in the timeline the graph is demonstrating.

> head(campanhalutudo$Recorte)
[1] "Periodo 1" "Periodo 1" "Periodo 1" "Periodo 2" "Periodo 3" "Periodo 2"

This line of code produces the base graph without any attempt to subset the line

`graflutudo <- ggplot(campanhalutudo, aes(x = `Hora da publicação`, y = Alcance), label = "periodo")   
    geom_line(color = "Purple")  
    geom_line(aes(y = mean(Alcance)), color = "red", linetype = "dotted")  
    scale_y_continuous(breaks = seq(0, 85000, by = 10000))  
    scale_x_datetime(labels = date_format("%m-%d"),
                     date_breaks = "4 days")  
    theme_light()`

enter image description here

I've tried using the following code to color the line:

    graflutudo<- ggplot(campanhalutudo, aes(x=`Hora da publicação`, y = Alcance, factor= (Recorte)), label = "periodo")   
geom_line(color = "Purple")  
geom_line(aes(y = mean(Alcance)), color = "red", linetype = "dotted")  
scale_x_datetime(labels = date_format("%m-%d"),
date_breaks = "4 days")   
scale_y_continuous(breaks = seq(0, 85000, by = 10000))  
theme_light()

It returned the same graph without any coloring other than the already used purple.

I was also thinking that maybe this isn't even the best way to showcase the 3 different time periods in the timeline, something like bars that subest the line could also do the trick, any tips are welcome.

CodePudding user response:

If you want separate purple lines for each series distinguished by the Recorte column, you can use geom_line(aes(group = Recorte), color = "purple")

If you want separate lines with different colors, you could use geom_line(aes(color = Recorte)) and combine that with something like scale_color_manual(values = c("Periodo 1" = "purple", "Periodo 2" = "darkorchid", "Periodo 3" = "darkmagenta")) if you want to specify different purple colors for the various series.

  • Related