我有以下数据框:
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")
在最顶部和底部,我想删除很多空白区域。如何在不改变宽高比的情况下做到这一点?
答案 0 :(得分:0)
似乎至少有两种不同的方法来实现这一目标:
使用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!)