0

I'm fairly new for #R programming, currently i meet an issue where i cannot adding legend for the geom_line graph using ggplot2, the code came as follow

########Convert data into suitable type
vEPCRaw$SYS_DATETIME <- as.POSIXct(vEPCRaw$SYS_DATETIME,format = "%Y-%m-%d %H:%M:%OS") ########convert into time format
vEPCRaw[c(6,7)] <- sapply(vEPCRaw[c(6,7)],as.numeric) ###as.numeric cannot use for multiple column

#########split to individual node
vEPCnodes <- split(vEPCRaw,vEPCRaw$NE_NAME)

vEPCnodesCP <- vEPCnodes[grepl("CP",names(vEPCnodes),fixed = TRUE)] ###########contain CP###########
vEPCnodesUP <- vEPCnodes[grepl("UP",names(vEPCnodes),fixed = TRUE)] ###########Contain UP###########

######## prep require data

day <- as.character(format(Sys.time(),"%Y/%m/%d %H:%M"))
nodename <- 'GGHT14-CP'
label <- paste(day,' ',nodename, 'CPU')

card_label = paste("card ",1:6);
######### CP 

CPplot <- ggplot()
for(i in 1:6)
{
  CPplot <- CPplot + geom_line(data = vEPCnodesCP[[1]][[i]], aes(x = SYS_DATETIME, y = CPU, colors = card_label[i]),col = i) 
}
CPplot + labs(title = label)+  
scale_x_datetime(name = "date and time",date_breaks = "1 day") + 
theme(axis.text.x=element_text(angle=60, hjust=1)) + 
scale_colour_manual(values = card_label)

The vEPCnodesCP[[1]][[i]] will be a data.frame as follow

      STT        SYS_DATETIME   NE_NAME DEVICE_CODE OBJECT_INSTANCE  CPU   MEM
13     13 2023-05-08 09:00:52 GGHT14-CP   GGHT14-CP             6/0 42.1 39.29
59     59 2023-05-08 08:55:53 GGHT14-CP   GGHT14-CP             6/0 41.0 39.29
116   116 2023-05-08 08:50:53 GGHT14-CP   GGHT14-CP             6/0 41.6 39.29
174   174 2023-05-08 08:45:52 GGHT14-CP   GGHT14-CP             6/0 42.1 39.29
238   238 2023-05-08 08:40:52 GGHT14-CP   GGHT14-CP             6/0 41.1 39.29
336   336 2023-05-08 08:35:51 GGHT14-CP   GGHT14-CP             6/0 41.6 39.29
376   376 2023-05-08 08:30:55 GGHT14-CP   GGHT14-CP             6/0 40.9 39.29
402   402 2023-05-08 08:25:53 GGHT14-CP   GGHT14-CP             6/0 40.2 39.29
479   479 2023-05-08 08:20:53 GGHT14-CP   GGHT14-CP             6/0 40.2 39.29
562   562 2023-05-08 08:15:50 GGHT14-CP   GGHT14-CP             6/0 41.0 39.29
578   578 2023-05-08 08:10:52 GGHT14-CP   GGHT14-CP             6/0 40.7 39.29
644   644 2023-05-08 08:05:54 GGHT14-CP   GGHT14-CP             6/0 41.4 39.29
707   707 2023-05-08 08:00:54 GGHT14-CP   GGHT14-CP             6/0 43.3 39.29
793   793 2023-05-08 07:50:50 GGHT14-CP   GGHT14-CP             6/0 41.0 39.29
808   808 2023-05-08 07:45:50 GGHT14-CP   GGHT14-CP             6/0 41.7 39.29
901   901 2023-05-08 07:40:52 GGHT14-CP   GGHT14-CP             6/0 41.6 39.29

I have try some method such as adding color in aes() of geom_line, adding scale_colour_manual(), and even add ggrepel() to use geom_label_repel() however all this method seems to not working.

Matt
  • 1
  • 1

1 Answers1

0

I'm a bit new to ggplot2 as well, but I think you have forgotten to put in the name attribute this works for my graph:

g <- g + geom_line(aes(y=glsM2, colour="Model 2"), show.legend = TRUE)
g <- g + scale_color_manual(name = "Models:", values = c("Model 2" = "green", Observed" = "blue", "GLS Model 3"="purple"))

Selva Prabhakaran's done a good tute: http://r-statistics.co/Complete-Ggplot2-Tutorial-Part1-With-R-Code.html

Candy
  • 1
  • 4
  • Hello, thank you for your info and documentation, i will look into it. About the name attribute in scale_color_manual(), i have added them but for me it seems doesn't work. – Matt May 16 '23 at 02:04
  • Update: after sometime look at the other post i have some info that could possibly worth to share. - If i remove the "col = i" outside of aes(), and keep the color inside, the label could be display. - If i try to process the ggplot() and geom() inside the loop, it will count as a layer, if i adding them one by one, this issue will not occur and label will be display normally. – Matt May 16 '23 at 07:39
  • Did you work it out? I think you are correct everything you want in the legend has to go inside the aes(). I haven't tried using a loop for different colours. I just noticed that you used "colors = card_label[i]" I think it should be singular (no 's'). Also, I spelt my colour with a 'u' as we do in Australia - I don't know if that is important. – Candy May 16 '23 at 08:30
  • not entirely, if i put color in aes() then put everything into the loop, it will only count as one layer so i still need to find some option to adding label outside of the loop, if i put color as a part of geom_line() function ex: geom_line(aes(),col = i), it will change the color of each line even if it's in a loop, however, no label will be display. About the spelling, it seem it could either be col, color or colour based on the help section – Matt May 16 '23 at 08:42
  • OK if something else comes to me I'll post it, otherwise I hope someone else has a better fix. – Candy May 16 '23 at 09:26
  • Hello, i Just found something that could solve my issue, it seems that the color in the aes() can be one of the data in the data frame, if i use color as one of the column, it will label it as different label Ex: since i want to plot multiple OBJECT_INSTANCE to one graph and label them, simply group them as one column then plot as follow "ggplot(vEPCnodesCP$`GGHT14-CP`,aes(SYS_DATETIME,CPU,color = OBJECT_INSTANCE)) + geom_line()" I found the solution based on this post https://stackoverflow.com/questions/32698616/ggplot2-adding-lines-in-a-loop-and-retaining-colour-mappings – Matt May 16 '23 at 09:34