【问题标题】:Add colored arrow to axis of ggplot2 (partially outside plot region)将彩色箭头添加到 ggplot2 的轴(部分在绘图区域之外)
【发布时间】:2015-01-24 05:51:44
【问题描述】:

我想添加一个彩色箭头(轴的全长)以显示沿某个方向移动 a 的时间(可以假设,但是对于此图没有数值,因此我希望箭头显示方向)。我可以使用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"))) 

给予

但我想要

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    问题似乎只是剪辑区域(如回答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)
    

    得到

    【讨论】:

    • 不错的方法。非常感谢。
    【解决方案2】:

    您可以定义自己的轴 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())
    

    【讨论】:

    • 感谢您提供此解决方案。你能告诉我如何为这个箭头设置颜色/宽度/填充吗?
    • 我知道已经晚了,但答案是:stackoverflow.com/questions/22775567/…。只需将 gpar 参数添加到 grid::segmentsGrob() 函数。只要记住在 grid::gpar() 中更改 gpar() 以防您没有加载网格
    • 要编辑箭头,您只需在函数 arrow() 中添加参数:例如,arrow(length=unit(0.1,'inches'), angle=40)
    【解决方案3】:

    element_line(arrow = arrow(...))September 2016 开始提供此功能。所以只需要theme(axis.line.x = element_line(arrow = ...)。有关箭头的调整,请参阅 element_line()this superseding question 的文档。

    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())
    

    【讨论】:

      【解决方案4】:

      你也可以加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")
      

      【讨论】:

        猜你喜欢
        • 2020-02-17
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多