R语言ggplot2中geom_line()无法添加图例的问题求助
解决ggplot2绘制geom_line时无法显示图例的问题
我是R编程新手,用ggplot2绘制geom_line图时无法添加图例,尝试过在geom_line的aes中添加color、使用scale_colour_manual,甚至引入ggrepel使用geom_label_repel,但这些方法均无效,希望得到解决办法。
原代码
########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)
数据结构示例(vEPCnodesCP[[1]][[i]])
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
问题根源与解决方案
问题点
- 代码中错误使用
colors(应为color)作为映射参数,且同时在geom_line外部手动指定col=i,这会覆盖aes内的颜色映射,导致ggplot无法生成图例 - 循环添加图层的方式不符合ggplot2基于长数据的绘图逻辑,分散的数据集无法让ggplot自动识别分组关系
修正步骤
合并数据集,添加分组列
把6个card的数据集合并成一个,并新增card列标识每个数据所属的card:# 合并6个card的数据,添加card标识列 combined_data <- do.call(rbind, lapply(1:6, function(i) { df <- vEPCnodesCP[[1]][[i]] df$card <- paste("card", i) df }))重构绘图代码
基于合并后的长数据,直接通过aes映射color到card列,无需循环添加图层:CPplot <- ggplot(combined_data, aes(x = SYS_DATETIME, y = CPU, color = card)) + geom_line() + labs(title = label, x = "date and time") + scale_x_datetime(date_breaks = "1 day") + theme(axis.text.x = element_text(angle = 60, hjust = 1)) + scale_colour_manual(values = c("card 1" = 1, "card 2" = 2, "card 3" = 3, "card 4" = 4, "card 5" = 5, "card 6" = 6)) CPplot
修正说明
- 合并后的数据符合ggplot2要求的长格式,让ggplot能自动识别分组并生成图例
- 正确使用
aes(color = card)建立颜色与分组的映射关系,不再手动指定col参数 scale_colour_manual中明确指定每个card对应的颜色值,确保颜色与原代码一致
内容的提问来源于stack exchange,提问作者Matt
相关产品推荐
相关产品推荐

