【问题标题】:Turning off ggplot clipping deletes line segment关闭ggplot裁剪删除线段
【发布时间】:2017-11-21 17:30:05
【问题描述】:

我正在尝试在 ggplot 的边缘绘制一些箭头。根据我的阅读,您必须关闭情节剪辑才能做到这一点。但是,当我这样做时,它会删除我在图表上的一条线段。

library(ggplot2)
library(ggrepel)
library(grid)

#----------------- Fake data practice --------------------- #

mydata <- data.frame(Labels = letters[1:14],
                     X_Values = seq(1, 14, 1),
                     Y_Values = rnorm(14, mean = 0, sd = 1),
                     Influence = seq(1, 14, 1))


mydata$Influencer <- factor(ifelse(mydata$Influence <= 3, 1, 0))

# --- Get min/max from data and use to set range at -1to1 or -2to2

chartMax <- ifelse(min(mydata$Y_Values) < -1 | max(mydata$Y_Values) > 1, 2, 1)
chartMin <- ifelse(chartMax == 1, -1, -2)

yTitle = "Some Title"
# --- Label setting, if greater than 0 nudge up, else nudge down

mydata$Nudger <- ifelse(mydata$Y_Values >= 0, .1, -.1)


p <- ggplot(mydata, aes(x = X_Values, y = Y_Values, group = Influencer)) +
        geom_point(aes(size = Influencer, color = Influencer), shape = 18) +
        geom_segment(x = 0, xend = 14, y = 0, yend = 0, color = "red", linetype = "dashed", size = 1.2, alpha = .5) +
        geom_text_repel(aes(x = X_Values, y = Y_Values, label = Labels),
                        box.padding = .4,
                        point.padding = .2,
                        nudge_y = .1) +
        scale_color_manual(values = c("grey", "blue")) + 
        scale_size_manual(values = c(4, 6)) +
        scale_y_continuous(name = "", limits = c(chartMin, chartMax)) + 
        scale_x_continuous(name = yTitle,
                           limits = c(1, 15),
                           breaks = c(2,13),
                           labels = c("Lower", "Higher")) +
        theme_classic() + theme(plot.margin = unit(c(1,3,1,2), "lines"),
                                legend.position="none",
                                axis.ticks.x=element_blank(),
                                axis.text.x = element_text(face = "bold"),
                                axis.title = element_text(face = "bold"),
                                axis.line.x = element_line(color = "blue"
                                                           ,size = 1
                                                           ,arrow = 
                                                             arrow(length = unit(0.5, "cm"),
                                                                   ends = "both"))) +
        annotation_custom(
          grob = linesGrob(arrow=arrow(type="open", ends="both", length=unit(0.5, "cm")), gp=gpar(col="blue", lwd=2)), 
          xmin = -1.4, xmax = -1.4, ymin = chartMin, ymax = chartMax
        ) 

p
# Here it works and you see the red dashed line

# Turn off panel clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

理想情况下,我希望在边缘的 y 轴旁边有一个蓝色箭头。我想我明白了,但我不能松开沿着图表内部延伸的红色虚线。

【问题讨论】:

标签: r ggplot2 annotations gtable


【解决方案1】:

我无法解释为什么会发生这种情况(似乎是一个错误,我建议提出问题here),但我可以确认该问题与 alpha 行有关。如果我们从 geom_segment 中删除 alpha = 0.5 参数,那么 clipping=off 不会删除该行:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2015-12-08
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多