本文介绍了如何从ggplot的图例中删除线条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已阅读了一些关于此的帖子,但没有找到适合我的问题的正确解决方案。 因为geom_vline中的虚线是垂直的,所以它也在图例中显示。最重要的是,短划线也可以得到一个真正没有必要的垂直合作伙伴。 是否可以只为颜色和类型获取水平线?这将使图例更清晰。 由于我的代码基于大型数据集,因此我在此处提供了一个简单的短变体。 #data(example) meetdagen1< - as.Date(c(2016-06-01,2016 -06-28,2016-07-17,2016-08-03,2016-08-30,2016-09-10)) maxtemp< - c(20 ,22,28,24,23,22) meantemp mintemp meetdagen2 maxtemp2 meantemp2< - maxtemp2 - 2 mintemp2< - meantemp2 - 2 #dataframes for ggplot df< - data.frame(meetdagen1 ,meantemp,mintemp,maxtemp) df2 #plot ggplot()+ xlab(时间(月))+ ylab(每日温度(°C))+ scale_x_date(date_labels =%b%d,date_breaks =1个月 )+ geom_line(data = df,aes(x = meetdagen1,y = maxtemp,color =max.temp),size = 0.75)+ geom_line(data = df,aes(x = meetdagen1,y = meantemp ,color =gem.temp),size = 0.75)+ geom_line(data = df,aes(x = meetdagen1,y = mintemp,color =min.temp),size = 0.75)+ geom_line(data = df2,aes(x = meetdagen2,y = maxtemp2,color =max.temp,lty =prediction),size = 0.75)+ geom_line(data = df2,aes (x = meetdagen2,y = meantemp2,color =gem.temp,lty =prediction),size = 0.75)+ geom_line(data = df2,aes(x = meetdagen2,y = mintemp2,color =min.temp,lty =prediction),size = 0.75)+ geom_vline(aes(xintercept = as.numeric(Sys.Date()),lty =today),size = 0.75 )+ scale_colour_manual(,values = c(max.temp =firebrick2,gem.temp =grey20,min.temp =royalblue3))+ scale_linetype_manual(, values = c(prediction = 3,today = 5))#+ #guides(color = guide_legend(override.aes = list(linetype = 0)))+ #guides(lintype = guide_legend(override.aes = list(color = NA))) 最后两行是我在类似问题中找到的解决方案。但他们隐藏所有线型或所有颜色,这是没有用的。 有什么想法? (我会插入图像,但我不知道我可以把它们放在哪个网站上......) 解决方案 code> show.legend = FALSE 来调用 geom_vline 它不包含垂直线,它应该给你你想要的。 I've read a few posts about this, but didn't find the right solution for my problem.Because the dashed line from geom_vline is vertical, it's also shown that way in the legend. On top of that, the short-dashed lines also get a vertical partner which is really not necessary.Is it possible to get only horizontal lines for both the colours and the types? It would make the legend much clearer.As my code is based on a large dataset, I made a simple short variant to show here.# data (example)meetdagen1 <- as.Date(c("2016-06-01", "2016-06-28", "2016-07-17", "2016-08-03", "2016-08-30", "2016-09-10"))maxtemp <- c(20, 22, 28, 24, 23, 22)meantemp <- maxtemp - 2mintemp <- meantemp - 2meetdagen2 <- c(meetdagen1, as.Date(c("2016-09-29", "2016-10-12", "2016-11-01")))maxtemp2 <- c(maxtemp, 20, 17, 19)meantemp2 <- maxtemp2 - 2mintemp2 <- meantemp2 - 2# dataframes for ggplotdf <- data.frame(meetdagen1, meantemp, mintemp, maxtemp)df2 <- data.frame(meetdagen2, meantemp2, mintemp2, maxtemp2)# plotggplot() + xlab("Time (months)") + ylab("Daily temperature (°C)") + scale_x_date(date_labels = "%b %d", date_breaks = "1 month") + geom_line(data = df, aes(x = meetdagen1, y = maxtemp, colour = "max.temp"), size = 0.75) + geom_line(data = df, aes(x = meetdagen1, y = meantemp, colour = "gem.temp"), size = 0.75) + geom_line(data = df, aes(x = meetdagen1, y = mintemp, colour = "min.temp"), size = 0.75) + geom_line(data = df2, aes(x = meetdagen2, y = maxtemp2, colour = "max.temp", lty = "prediction"), size = 0.75) + geom_line(data = df2, aes(x = meetdagen2, y = meantemp2, colour = "gem.temp", lty = "prediction"), size = 0.75) + geom_line(data = df2, aes(x = meetdagen2, y = mintemp2, colour = "min.temp", lty = "prediction"), size = 0.75) + geom_vline(aes(xintercept = as.numeric(Sys.Date()), lty = "today"), size = 0.75) + scale_colour_manual("", values = c(max.temp = "firebrick2", gem.temp = "grey20", min.temp = "royalblue3")) + scale_linetype_manual("", values = c(prediction = 3, today = 5)) #+ #guides(colour = guide_legend(override.aes = list(linetype = 0))) + #guides(lintype = guide_legend(override.aes = list(colour = NA)))The bottom two lines are a solution I found in a similar question. But they hide either all linetypes or all colours, which isn't useful.Any ideas?(I would insert images, but I have no idea on which website I can put them...) 解决方案 If you add show.legend = FALSE to the call to geom_vline it will not include the vertical line, which should give you what you want. 这篇关于如何从ggplot的图例中删除线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 04:13