我对#R编程相当陌生,目前我遇到了一个问题,我无法使用ggplot2为geom_line图形添加图例,代码如下
########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)
**vEPCnodesCP1i**将是 Dataframe ,如下所示
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
我尝试了一些方法,如在geom_line的aes()中添加颜色,添加scale_colour_manual(),甚至添加ggrepel()来使用geom_label_repel(),但所有这些方法似乎都不起作用。
1条答案
按热度按时间2q5ifsrm1#
我对ggplot2也有点陌生,但我想你忘了在name属性中添加这对我的图形有效:
塞尔瓦·普拉巴卡兰做得很好:http://r-statistics.co/Complete-Ggplot2-Tutorial-Part1-With-R-Code.html