代码之家  ›  专栏  ›  技术社区  ›  John Doe

如何减少R中分段图的空白?

  •  0
  • John Doe  · 技术社区  · 7 年前

      person step start end
    1    sam    A     0   4
    2    sam    B     4   6
    3   greg    A     2   7
    4   greg    B     7  11
    

    library(ggplot2)
    library(dplyr)
    library(tidyr)
    
    df
    
    ggplot(df, aes(colour=step)) + 
      geom_segment(aes(x=start, xend=end, y=person, yend=person), size=3) +
      xlab("Duration")
    

    在最上面和最下面有很多我想去掉的空白。如何在不更改宽高比的情况下完成此操作?

    Segmented plot reduce empty space

    1 回复  |  直到 7 年前
        1
  •  0
  •   PavoDive    7 年前

    似乎至少有两种不同的方法可以实现这一点:

    scale_y_discrete ,正如@WaltS在评论中建议的那样:

    ggplot(df, aes(color = step))+
        geom_segment(aes(x=start, xend=end, y=person, yend=person), 
                     size = 3)+
        xlab("duration")+
        scale_y_discrete(expand = expand_scale(mult = 0.1))
    

    另一个使用 coord_cartesian

    ggplot(df, aes(color = step))+
      geom_segment(aes(x=start, xend=end, y=person, yend=person), 
                   size = 3)+
      xlab("duration")+
      coord_cartesian(expand = FALSE, #use no expansion
                      ylim = c(0.8,2.2), #set manually the limits in y
                      xlim = c(-1,12))  # set manually limits in x (or expand = F will make it tight!)