本文介绍了将vline添加到现有绘图并使其显示在ggplot2图例中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一些数据用于绘制直方图。我也有两组具有一定意义的阈值。 我可以用适当的样式绘制直方图和曲线。但是,我无法让我的vlines显示在图例中。我相信像这样的东西应该可以工作,但是图例项目不会显示。 df< - data.frame (val = rnorm(300,75,10)) cuts1 cuts2 ggplot(data = df,aes(x = val))+ geom_histogram()+ geom_vline(xintercept = cuts1, linetype = 1, color =red, labels =Thresholds A, show_guide = TRUE)+ geom_vline(xintercept = cuts2, linetype = 2, color = green, labels =Thresholds B, show_guide = TRUE) pre $ cuts1 cuts2 ggplot(data = df,aes(x = val))+ geom_histogram()+ geom_vline(data = cuts1,aes(xintercept = vals,shape =阈值), linetype = 1, color =red, labels =Thresholds A, show_guide = TRUE)+ geom_vline(data = cuts2 ,aes(xintercept = vals,shape = Thresholds), linetype = 2, color =green, labels =Thresholds B, show_guide = TRUE) 所以,最后,我正在寻找的是直接手动添加两个设置图线,然后让它们在图例中正确显示。解决方案诀窍是将阈值数据所有在 same 数据框中,然后映射美学,而不是设置它们: cuts< ; --bbind(cuts1,cuts2) ggplot(data = df,aes(x = val))+ geom_histogram()+ geom_vline(data = cuts, aes(xintercept = vals, linetype = Thresholds, color = Thresholds), show_guide = TRUE) I have some data that I am using to plot a histogram. I also have two sets of thresholds that have some significance.I am able to plot the histogram and the vlines with the appropriate styles. However, I cannot get my vlines to show up in the legend. I believe that something like this should work, however the legend items don't ever display.df <- data.frame(val=rnorm(300, 75, 10))cuts1 <- c(43, 70, 90)cuts2 <- c(46, 79, 86)ggplot(data=df, aes(x=val)) + geom_histogram() + geom_vline(xintercept=cuts1, linetype=1, color="red", labels="Thresholds A", show_guide=TRUE) + geom_vline(xintercept=cuts2, linetype=2, color="green", labels="Thresholds B", show_guide=TRUE)Alternatively, if I construct a data.frame for my cuts and do an aesthetic mapping, I can get my vlines to show up in the legend. Unfortunately, the legend gives me two instances of the different line types superimposed on each other:cuts1 <- data.frame(Thresholds="Thresholds A", vals=c(43, 70, 90))cuts2 <- data.frame(Thresholds="Thresholds B", vals=cuts2 <- c(46, 79, 86))ggplot(data=df, aes(x=val)) + geom_histogram() + geom_vline(data=cuts1, aes(xintercept=vals, shape=Thresholds), linetype=1, color="red", labels="Thresholds A", show_guide=TRUE) + geom_vline(data=cuts2, aes(xintercept=vals, shape=Thresholds), linetype=2, color="green", labels="Thresholds B", show_guide=TRUE)So, in the end, what I'm looking for, is the most straightforward way to manually add two sets of lines to a plot, and then have them appear correctly in the legend. 解决方案 The trick is to put the threshold data all in the same data frame, and then map aesthetics, rather than set them:cuts <- rbind(cuts1,cuts2)ggplot(data=df, aes(x=val)) + geom_histogram() + geom_vline(data=cuts, aes(xintercept=vals, linetype=Thresholds, colour = Thresholds), show_guide = TRUE) 这篇关于将vline添加到现有绘图并使其显示在ggplot2图例中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 20:46