R 更改堆叠图形图例中的几何图形顺序

R 更改堆叠图形图例中的几何图形顺序,r,ggplot2,legend,layer,R,Ggplot2,Legend,Layer,我正在尝试更改图例使用ggplot为图形显示点和线的顺序 目前,我的图形本身先显示点,然后在点的顶部显示线,这就是我想要的。以下是一个可复制的示例: ggplot(iris, aes(Sepal.Width, Petal.Width, linetype = Species)) + geom_point(aes(colour = Species, shape = Species)) + geom_smooth() 正如您从图例中看到的,它已按绘制顺序覆盖了这些值,但我想逆转这一点。我知

我正在尝试更改图例使用ggplot为图形显示点和线的顺序

目前,我的图形本身先显示点,然后在点的顶部显示线,这就是我想要的。以下是一个可复制的示例:

ggplot(iris, aes(Sepal.Width, Petal.Width, linetype = Species)) +
  geom_point(aes(colour = Species, shape = Species)) +
  geom_smooth()

正如您从图例中看到的,它已按绘制顺序覆盖了这些值,但我想逆转这一点。我知道我可以将点覆盖在绘图顶部,如下所示:

ggplot(iris, aes(Sepal.Width, Petal.Width, linetype = Species)) +
  geom_smooth() +
  geom_point(aes(colour = Species, shape = Species)) 

我已经从中搜索并找到了函数
guides(fill=guide\u legend(reverse=TRUE))
,但这没有任何效果


从我在ggplot中读到的任何帮助,我都非常感激,我认为这不可能设置覆盖ggplot图例的顺序。在
guides
函数中,可以使用
order
参数设置单独指南的顺序,该参数解释如下:

小于99的正整数,指定此指南在多个指南中的顺序。它控制多个指南的显示顺序,而不是指南本身的内容。如果为0(默认值),则顺序由秘密算法确定

在我看来,通过以下组合最容易实现您的目标:

  • 使用
    legend=FALSE
    抑制某些图例
  • 多次打印某些图层
  • 在下面的示例中,
    geom_smooth
    被绘制两次。一次创建图例背景,然后第二次覆盖数据。第二次打印时,不会显示图例:

    ggplot(iris, aes(Sepal.Width, Petal.Width, linetype = Species)) +
      geom_smooth() +
      geom_point(aes(colour = Species, shape = Species)) +
      geom_smooth(show.legend = FALSE)
    

    另一种方法是使线条颜色随每个数据集而变化。这需要更少的玩转,更容易理解,所以我可能会这样说:

    ggplot(iris, aes(Sepal.Width, Petal.Width, colour = Species, shape = Species, linetype = Species)) +
      geom_point(aes())  +
      geom_smooth(aes())
    

    这里有两种方法,允许对图例中的格罗布进行操作,但这两种方法都不简单

    第一个使用ggplot的gtable。布局数据框中的“z”列给出了GROB的绘制顺序。但是为了操纵图例的grob,必须访问图例本身的布局数据框。大概是这样的:

    library(ggplot2)
    library(gtable)
    library(grid)
    
    df = read.table(text = 
    "School      Year    Value 
     A           1998    5
     B           1999    10
     C           2000    15
     A           2000    7
     B           2001    15
     C           2002    20", sep = "", header = TRUE)
    
    p <- ggplot(iris, aes(Sepal.Width, Petal.Width, linetype = Species)) +
      geom_point(aes(colour = Species, shape = Species), size = 2) +
      geom_smooth()
    
    # Get the ggplot grob
    gt <- ggplotGrob(p)
    
    # Get the legend
    leg <- gt$grobs[gt$layout$name == "guide-box"][[1]]$grobs[[1]]
    
    # Check the layout
    leg$layout
    
    第二种方法使用网格的编辑功能来更改顺序

    # Get the ggplot grob
    gt <- ggplotGrob(p)
    
    # Get the legend grob
    leg = getGrob(grid.force(gt), gPath("guides"), grep = TRUE)
    
    # Get a list of grobs (in the legend).
    # This also gives the order.
    grid.ls(grid.force(leg))
    
    # Nearly the same names for the grobs, 
    #   and the same order as before 
    #   (don't count the indented grobs).
    
    # Change the order in which grobs are drawn
    leg = reorderGrob(leg,  c(5:4, 8:7, 11:10), back = FALSE)
    
    # Remove the original legend and replace with the new legend
    gt = removeGrob(grid.force(gt), "guides", grep = TRUE)
    gt = addGrob(gt, leg, "guide-box", grep = TRUE)
    
    # Draw the plot
    grid.newpage()
    grid.draw(gt)
    
    #获取ggplot grob
    
    gt Hi Justin,请编辑您的问题,并提供一个最小的、完整的、可验证的示例,特别是重现问题的示例数据集:。使帮助你更容易:)我最后只是重写了这个问题,这样你就可以更好地了解如何为将来构建它。请检查编辑并批准:)嗨,米奇,非常感谢你花时间教我如何正确地组织问题,并提供一个可验证的例子。我一定会从你的例子中学习,并在将来自己做这件事。我绝对同意编辑:)
    # Get the ggplot grob
    gt <- ggplotGrob(p)
    
    # Get the legend grob
    leg = getGrob(grid.force(gt), gPath("guides"), grep = TRUE)
    
    # Get a list of grobs (in the legend).
    # This also gives the order.
    grid.ls(grid.force(leg))
    
    # Nearly the same names for the grobs, 
    #   and the same order as before 
    #   (don't count the indented grobs).
    
    # Change the order in which grobs are drawn
    leg = reorderGrob(leg,  c(5:4, 8:7, 11:10), back = FALSE)
    
    # Remove the original legend and replace with the new legend
    gt = removeGrob(grid.force(gt), "guides", grep = TRUE)
    gt = addGrob(gt, leg, "guide-box", grep = TRUE)
    
    # Draw the plot
    grid.newpage()
    grid.draw(gt)