簡體   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