gpt4 book ai didi

r - ggplot 停止 geom_segment 在图例中显示 geom_boxplot

转载 作者:行者123 更新时间:2023-12-05 06:26:41 27 4
gpt4 key购买 nike

我正在尝试获得类似于 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")))

但是因为箱线图也使用了线型,如果我这样做,我最终会得到黑色方 block 。我也不想为 geom_segment 设置 show.legend = F,因为我想在图例中添加箭头。

作为奖励,我希望将图例中的箭头旋转为面朝上。但这并不重要。

我得到的图:

plot that I get

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

最佳答案

也许是这样的?

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

关于r - ggplot 停止 geom_segment 在图例中显示 geom_boxplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55805442/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com