繁体   English   中英

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

[英]How to reduce empty space in segmented plot in R?

我有以下数据框:

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

在顶部和底部,有很多空白要删除。 如何在不更改宽高比的情况下执行此操作?

分段图减少空白空间

似乎至少有两种不同的方法可以实现此目的:

使用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!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM