在ggplot2的坐标轴上添加有颜色的箭头(部分超出绘图区域)

7
我想添加一个有颜色的箭头(轴的全部长度)以显示时间向一个方向移动(可以假定,但对于这个图表没有数值,因此我希望箭头显示方向)。我可以使用geom_segment来绘制它,但是在图形区域之外的部分会被省略。
我看过这篇文章:R & ggplot2:如何在坐标轴标签下获取箭头? 但是这种解决方法是对轴标题的黑客攻击。这篇文章:https://dev59.com/Ymkv5IYBdhLWcg3wiRWo#10542622 显示文本区域外的线条,但不是彩色箭头。 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"))) 

给予

enter image description here

但是我想要的是

enter image description here

4个回答

9
问题似乎只是剪切区域(如这里所回答的)。请尝试:
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)

获取

图片描述


不错的方法。非常感谢。 - Tyler Rinker

9
您可以定义自己的轴Grob,
library(ggplot2)

element_grob.element_custom <- function(element, ...)  {
  grid::segmentsGrob(0,1,1,1, arrow = arrow())
}
 ## silly wrapper to fool ggplot2
axis_custom <- function(...){
  structure(
    list(...), # this ... information is not used, btw
    class = c("element_custom","element_blank", "element") # inheritance test workaround
  ) 

}

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_line() +
  theme(axis.line = axis_custom(),
        axis.line.y=element_blank())

enter image description here


谢谢您提供的解决方案。您能告诉我如何设置这个箭头的颜色/宽度/填充吗? - Yuriy Petrovskiy
我知道现在已经很晚了,但这是答案:https://stackoverflow.com/questions/22775567/change-line-colour-and-thickness-in-grid-curve-r。只需将gpar参数添加到grid::segmentsGrob()函数中即可。请记得在没有加载grid的情况下将gpar()更改为grid::gpar()。 - Jinglestar
要编辑箭头,只需在函数arrow()中添加参数: 例如,arrow(length=unit(0.1,'inches'), angle=40) - Jinglestar

3
2016年9月以来,element_line(arrow = arrow(...))就提供了这个功能。所需的全部只是 theme(axis.line.x = element_line(arrow = ...)。有关箭头调整,请参阅element_line()的文档,也可以查看这个更高级的问题

enter image description here

library(dplyr)
library(ggplot2)

theme_set(theme_bw())

data = tibble(time = 0:5, y = 0:5)
ggplot(data, aes(x = time, y = y)) +
  geom_area(alpha = 0.1) +
  coord_cartesian(ylim = c(0, 5)) +
  labs(x = "time", y = "y") +
  scale_x_continuous(breaks = NULL, expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme(axis.line.x = element_line(arrow = arrow()),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank())

2
您还可以添加coord_cartesian(clip = "off")
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"))) +
  coord_cartesian(clip = "off")

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接