R-图例中的geom_vline和geom_smooth组合 [英] R - combined geom_vline and geom_smooth in legend

查看:169
本文介绍了R-图例中的geom_vline和geom_smooth组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot2图表中添加geom_smooth()geom_vline()时,我在图例中遇到一些奇怪的行为.这是一个可重现的示例:

I'm encountering some strange behaviour in my legend when adding a geom_smooth() and a geom_vline() in my ggplot2 chart. Here's a reproducible example:

library(ggplot2)
n <- 60

set.seed(123)
df <- data.frame(Area = sample(c("A", "B", "C", "D"), size = n, replace = TRUE),
             x = runif(n),
             y = runif(n),
             Type = sample(c("I", "II"), size = n, replace = TRUE),
             Result = sample(c("K", "L", "M"), size = n, replace = TRUE))

df.breaks <- data.frame(Area = c("B", "C"), x = c(0.8, 0.3))

ggplot(df, aes(x = x, y = y)) + 
  geom_point(aes(colour = Result, shape = Type), size = 3) + 
  geom_smooth(aes(linetype = "Smooth"), colour = "green", se = FALSE) + 
  geom_hline(yintercept = 0.3) + 
  facet_wrap(~Area) + 
  geom_vline(data = df.breaks, aes(xintercept = x, linetype = "Break"), colour = "purple") + 
  scale_colour_manual(values = c("K" = "red", "L" = "orange", "M" = "blue")) + 
  scale_linetype_manual(name = "Lines", values = c("Break" = "dashed", "Smooth" = "solid"))

您会注意到,线条"图例在每个项目中都有垂直线和水平线,在第一种情况下有几条虚线,而在第二种情况下有几条实线.我正在尝试调整代码以产生图例,该图例具有(1)水平的绿线和位于其旁边的键,称为平滑",以及(2)垂直的紫色淡色线,其旁边具有键,称为"Break".我将不胜感激,因为无论我尝试了什么(包括linetype内部/外部aes()等,或者使用scale_linetype_identity(),甚至是guides中的override.aes选项),我都找不到正确的组合!

As you will notice the "Lines" legend has both the vertical and the horizontall lines in each item, and in the first case there are a couple of dashed lines while in the second case a couple of solid lines. I'm trying to adjust my code to produce a legend with (1) a horizontal green line and a key next to it called "Smooth" and (2) a vertical purple dahsed line with a key next to it called "Break". I would appreciate some help as, no matter what I tried (including linetype inside/outside aes() etc, or using scale_linetype_identity(), or even the override.aes option in guides) I couldn't find the right combination!

我搜索了类似的示例,即使我发现其他帖子在colourfillshape等上有垂直线重叠,我也找不到在linetype上有垂直线的帖子.像我这样的传奇.任何帮助将不胜感激!谢谢!

I searched for similar examples and even though I found other posts with a superimposed vertical line on colour, fill, or shape etc, I coulnd't find one with a vertical line on a linetype legend such as mine. Any help will be deeply appreciated! Thanks!

推荐答案

对此问题的延迟答复,但我认为最好有一个可用的答案,以防其他人(或我未来的自我!)对此感兴趣.

A belated reply to this question but I thought it would be better to have an answer available in case someone else (or my future self!) is interested in this.

在评论部分中@aosmith的建议并鼓励他提供答案后,我最终将情节的代码替换为:

Following @aosmith's suggestion in the comments' section and his encouragement to provide the answer, I eventually replaced the code for the plot with:

ggplot(df, aes(x = x, y = y)) + 
     geom_point(aes(colour = Result, shape = Type), size = 3) + 
     geom_hline(yintercept = 0.3) + 
     facet_wrap(~Area) + 
     geom_vline(data = df.breaks, 
                aes(xintercept = x, linetype = "Break"), colour = "purple") + 
     scale_colour_manual(values = c("K" = "red", "L" = "orange", "M" = "blue")) + 
     scale_linetype_manual(name = "Lines",
                           values = c("Break" = "dashed", "Smooth" = "solid")) + 
     geom_smooth(aes(fill = "Smooth"), method = "loess", colour = "green", se = FALSE) + 
     scale_fill_discrete(name = "")

上面的最后两行替换了原始代码中的以下行:

The last two lines above replace the following line from the original code:

geom_smooth(aes(linetype = "Smooth"), colour = "green", se = FALSE) +.

生成的图表仍然需要做一些工作,尤其是在图例间距方面,但是它可以解决原始问题.

The resulting chart still needs some work, esepcially with the legend spacing, but it solves the original problem.

这篇关于R-图例中的geom_vline和geom_smooth组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆