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.