本文介绍了添加图例以手动添加使用ggplot的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用 ggplot 添加3个手动添加行的相应图例。我的代码如下: library(ggplot2) df = data.frame(error = c(0.0832544999, 0.0226680026,0.0082536264,0.0049199958,0.0003917755,0.0003859976,0.0003888253,0.0003953918,0.0003958398),sDev = c(8.188111e-03,2.976161e-03,1.466221e-03,2.141425e-03,2.126976e-05,2.1339364e-05 ,2.169059e-05,2.629895e-05,2.745938e-05)) 最低 best.model gplot < - ggplot(df,aes(x = 1:length(error),y = error))+ scale_x_continuous(breaks = seq_along(df $ error))+ geom_point(size = 3) + geom_line()+ geom_errorbar(data = df,aes(x = 1:length(error),ymin = error - sDev,ymax = error + sDev), width = 0.1 )+ geom_hline(data = df,aes(yintercept = error [minimum] + sDev [minimum]),linetype =dashed)+ geom_vline(xintercept = minimum,linetype =dotted, color =red,size = 1)+ geom_vline(xintercept = best.model,linetype =dotted,color =blue,size = 1)+ theme_gray(base_size = 18)+ theme(axis.text = element_text( color =black))+ labs(x =#参数,fontface =bold)+ 实验室(y =CV错误)+ 实验室=交叉验证错误曲线) 我想知道如何添加3个虚线的图例黑色,红色和蓝色。 非常感谢!解决方案诀窍是使用适当的映射: gplot scale_x_continuous(breaks = seq_along(df $ error))+ geom_point(size = 3)+ geom_line()+ geom_errorbar (data = df,aes(x = 1:长度(错误) ,ymin = error -sDev,ymax = error + sDev), width = 0.1)+ geom_hline(data = df,aes(yintercept = error [minimum] + sDev [minimum],linetype = a,color =a))+ geom_vline(data = data.frame(type =b,col =b,minimum = minimum), aes(linetype = type, color = col,xintercept = minimum),size = 1,show_guide = TRUE)+ geom_vline(data = data.frame(type =b,col =b,best.model = best.model) , aes(linetype =c,color =c,xintercept = best.model),size = 1,show_guide = TRUE)+ scale_colour_manual(name =Legend,values = c (a=black,b=red,c=blue))+ scale_linetype_manual(name =Legend,values = c(a=dashed ,b=点,c=点))+ theme_gray(base_size = 18)+ 主题(axis.text = element_text(color =black), legend.key.height = grid :: unit(0.1,npc))+ labs(x =#参数,fontface =bold)+ labs(y =CV错误)+ 实验室(标题=交叉验证错误曲线) I'm trying to add the corresponding legend for 3 manually added lines using ggplot. My code is the following:library(ggplot2)df = data.frame(error = c(0.0832544999, 0.0226680026, 0.0082536264, 0.0049199958, 0.0003917755, 0.0003859976, 0.0003888253, 0.0003953918, 0.0003958398), sDev = c(8.188111e-03, 2.976161e-03, 1.466221e-03, 2.141425e-03, 2.126976e-05, 2.139364e-05, 2.169059e-05, 2.629895e-05, 2.745938e-05))minimum <- 6best.model <- 5gplot <- ggplot(df, aes(x=1:length(error), y=error)) + scale_x_continuous(breaks = seq_along(df$error)) + geom_point(size = 3) + geom_line() + geom_errorbar(data = df, aes(x = 1:length(error), ymin = error - sDev, ymax = error + sDev), width = 0.1) + geom_hline(data = df, aes(yintercept = error[minimum] + sDev[minimum]), linetype = "dashed") + geom_vline(xintercept = minimum, linetype = "dotted", color = "red", size = 1) + geom_vline(xintercept = best.model, linetype = "dotted", color = "blue", size = 1) + theme_gray(base_size = 18) + theme(axis.text = element_text(color = "black")) + labs(x = "# of parameters", fontface = "bold") + labs(y = "CV error") + labs(title = "Cross-validation error curve")I'd like to know how to add the legends for the 3 dotted lines in black, red, and blue.Thanks a lot in advance! 解决方案 The trick is to use appropriate mapping:gplot <- ggplot(df, aes(x=1:length(error), y=error)) + scale_x_continuous(breaks = seq_along(df$error)) + geom_point(size = 3) + geom_line() + geom_errorbar(data = df, aes(x = 1:length(error), ymin = error - sDev, ymax = error + sDev), width = 0.1) + geom_hline(data = df, aes(yintercept = error[minimum] + sDev[minimum], linetype="a", colour="a")) + geom_vline(data= data.frame(type="b", col="b", minimum=minimum), aes(linetype=type, colour=col, xintercept = minimum), size = 1, show_guide = TRUE) + geom_vline(data= data.frame(type="b", col="b", best.model=best.model), aes(linetype="c", colour="c", xintercept = best.model), size = 1, show_guide = TRUE) + scale_colour_manual(name="Legend", values = c("a" = "black", "b" = "red", "c" = "blue")) + scale_linetype_manual(name="Legend", values = c("a" = "dashed", "b" = "dotted", "c" = "dotted")) + theme_gray(base_size = 18) + theme(axis.text = element_text(color = "black"), legend.key.height = grid::unit(0.1, "npc")) + labs(x = "# of parameters", fontface = "bold") + labs(y = "CV error") + labs(title = "Cross-validation error curve") 这篇关于添加图例以手动添加使用ggplot的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:35