r - 将彩色箭头添加到ggplot2的轴(部分在绘图区域之外)

标签 r ggplot2

我想添加一个彩色箭头(轴的全长)以显示时间在一个方向上移动(可以假定,但是对于此绘图,没有数值,所以我希望箭头显示方向)。我可以使用geom_segment对其进行绘制,但是在绘制区域之外的部分丢失了。

我看过这篇文章:R & ggplot2: How to get arrows under the axis label?,但是这个解决方案是对轴标题的修改。这篇文章:https://stackoverflow.com/a/10542622/1000343显示了文本区域之外的行,但没有彩色箭头。

MWE

library(ggplot2); library(grid); library(scales)

dat <- data.frame(Time=0:5, y=0:5)

ggplot(dat, aes(x=Time, y=y)) +
    geom_area(alpha=.1) + theme_bw() +
    scale_y_continuous(expand = c(0, 0)) +
    scale_x_continuous(expand = c(0, 0)) +
    theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank()
    )    

我尝试过的:
ggplot(dat, aes(x=Time, y=y)) +
    geom_area(alpha=.1) + theme_bw() +
    scale_y_continuous(expand = c(0, 0)) +
    scale_x_continuous(expand = c(0, 0)) +
    theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank()
    ) +
    geom_segment(aes(x=0, xend = 5 , y=0, yend = 0), size=1.5,
        arrow = arrow(length = unit(0.6,"cm"))) 

给予

但我想要

最佳答案

问题似乎只是剪切区域(如here回答)。尝试:

p1<-ggplot(dat, aes(x=Time, y=y)) +
    geom_area(alpha=.1) + theme_bw() +
    scale_y_continuous(expand = c(0, 0)) +
    scale_x_continuous(expand = c(0, 0)) +
    theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank()
    ) +
    geom_segment(aes(x=0, xend = 5 , y=0, yend = 0), size=1.5,
        arrow = arrow(length = unit(0.6,"cm"))) 

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

要得到

关于r - 将彩色箭头添加到ggplot2的轴(部分在绘图区域之外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27140058/

相关文章:

r - 如何在 "modelerData"节点中正确制作 "modelerDataModel"和 "Extension Transform (R syntax)"添加多个列

r - 使用 R/ggplot/ggmap 填充等高线图

R 子集/过滤器不返回任何行

R ggplot : Apply label only to last N data points in plot

r - 在 ggplot2 #2 的各个方面注释文本

r - 查找纵向数据中出现超过 3 次的观测值

r - 如何在不调用ggplot()的情况下将ggproto对象一起添加并保存以供以后使用?

r - 颜色渐变线为轴

r - 使用 stat_summary 自动调整 ylim

r - 如何让 ggplot 正确排序构面?