似乎至少有两种不同的方法可以实现这一点:
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!)