ggplot stop geom_segment显示图例中的geom_boxplot

时间:2019-04-23 06:26:17

标签: r ggplot2 legend

我正在尝试获得类似于Glen_b对this question的回答。基本上,我希望框线图顶部的箭头表示使用ggplot超出范围的离群值。

我可以使剧情的主要部分看起来像我想要的那样,但是我遇到了遇到一个明智的传说的问题。

我举了一个例子:

library(plyr)
library(dplyr)
library(ggplot2)

#create test data frame with extreme outlier
mpg_test <- mpg

mpg_test[1,"hwy"] = 250
pmax = 50
pmin = min(mpg_test$hwy)

outliersabovepmax <- filter(mpg_test, hwy > pmax) %>% mutate(hwy= pmax) 

#basic plot without cropping/adding arrows
p <- ggplot(mpg_test, aes(x = class, y=hwy, colour = class)) + geom_boxplot()
p

我可以使情节的主体看起来像我想要的一样:

#plot that I want
p2 <- p + 
  geom_segment(data = outliersabovepmax, 
               aes(xend = class, y = hwy-1, yend = hwy+1, 
                   linetype = "Outlier above"), 
               arrow = arrow(), show.legend = T) +
  coord_cartesian(ylim = c(pmin, pmax))

但是我希望图例不显示颜色图例上的箭头。

我通常希望能够做以下事情:

p2 + 
  guides(colour = guide_legend(override.aes = list(linetype = "blank")))

但是因为箱线图也使用线型,所以如果这样做,我只会得到黑色正方形。我也不想为show.legend = F设置geom_segment,因为我想在图例中使用箭头。

作为奖励,我希望旋转图例中的箭头以使其朝上。但这不是那么重要。

我得到的情节:

plot that I get

我想要的图(用油漆制作): plot with legend that I'd like

1 个答案:

答案 0 :(得分:0)

也许是这样吗?

p + 
  # hide legend in actual segment layer
  geom_segment(data = outliersabovepmax, 
               aes(xend = class, y = hwy-1, yend = hwy+1), 
               arrow = arrow(), show.legend = F)+

  # have invisible segment layer that shows legend
  geom_segment(data = outliersabovepmax, 
               aes(xend = class, y = hwy-1, yend = hwy+1, linetype = "Outlier above"), 
               arrow = arrow(), alpha = 0, show.legend = T) +

  # override alpha for linetype legend
  guides(linetype = guide_legend(override.aes = list(alpha = 1))) +

  coord_cartesian(ylim = c(pmin, pmax))

plot

如果要更改geom_segment的图例键的布局,则可以深入研究GeomSegment的基础代码:

library(grid)
GeomSegment2 <- ggproto("GeomSegment2", GeomSegment,
                        draw_key = function (data, params, size)                         {
                          data$linetype[is.na(data$linetype)] <- 0
                          segmentsGrob(
                            # vertical instead of horizontal line
                            0.5, 0.1, 0.5, 0.9, #0.1, 0.5, 0.9, 0.5, 
                            gp = gpar(col = alpha(data$colour, data$alpha), 
                                      lwd = data$size * .pt, 
                                      lty = data$linetype, 
                                      lineend = "butt"), 
                            arrow = params$arrow)
                        })
geom_segment2 <- function (mapping = NULL, data = NULL, stat = "identity", 
                           position = "identity", ..., arrow = NULL, arrow.fill = NULL, 
                           lineend = "butt", linejoin = "round", na.rm = FALSE, 
                           show.legend = NA, inherit.aes = TRUE) {
  layer(data = data, mapping = mapping, stat = stat, geom = GeomSegment2, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list(arrow = arrow, arrow.fill = arrow.fill, 
                      lineend = lineend, linejoin = linejoin, na.rm = na.rm, 
                      ...))
}

用法:

p + 
  # hide legend in actual segment layer
  geom_segment(data = outliersabovepmax, 
               aes(xend = class, y = hwy-1, yend = hwy+1), 
               arrow = arrow(), show.legend = F)+

  # have invisible segment layer that shows legend
  geom_segment2(data = outliersabovepmax, 
               aes(xend = class, y = hwy-1, yend = hwy+1, linetype = "Outlier above"), 
               arrow = arrow(), alpha = 0, show.legend = T) +

  # override alpha for linetype legend
  guides(linetype = guide_legend(override.aes = list(alpha = 1))) +

  coord_cartesian(ylim = c(pmin, pmax))

2nd plot