ggplot2无法调整日期垂线的图表大小

3

当我在图表中添加geom_hline()时,图表会被重新调整大小以适应它们。但是当我添加geom_vline()时,图表不会被重新调整大小。

为什么会这样?如何使图表重新调整大小?

最小工作示例

library(ggplot2)

data <- data.frame(
  time=c(
    "2016-12-09T05:07:11Z", "2016-12-10T09:42:45Z", "2016-12-09T10:04:57Z",
    "2016-12-09T02:19:04Z", "2016-12-11T17:43:02Z", "2016-12-11T05:40:48Z",
    "2016-12-11T08:47:13Z", "2016-12-12T15:41:13Z"),
  value=c(23.3, 8.1, 12.9, 12.7, 5.6, 3.9, 5.5, 27.8)
)
# Each contains 3 values: 1 within the domain/range of `data` and 2 on either side
vlines <- data.frame(time=c("2016-12-07T00:00:00Z", "2016-12-11T00:00:00Z", "2016-12-14T00:00:00Z"))
hlines <- data.frame(value=c(-20, 10, 50))

data$time <- strptime(as.character(data$time), "%Y-%m-%dT%H:%M:%S", tz="UTC")
vlines$time <- strptime(as.character(vlines$time), "%Y-%m-%dT%H:%M:%S", tz="UTC")
vlines$timeNum <- as.numeric(vlines$time)

p <- ggplot(data, aes(x=time, y=value)) + geom_line()

ggsave("mwe1.pdf", p)

p <- p +
  geom_hline(data=hlines, aes(yintercept=value), color="red") +
  geom_vline(data=vlines, aes(xintercept=timeNum), color="blue")

ggsave("mwe2.pdf", p)

mwe1.pdf

只有数据的图表

mwe2.pdf

针对<code>hlines</code>进行调整大小的图表,但不包括<code>vlines</code>

编辑:sessionInfo()

R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.6

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  base     

other attached packages:
[1] ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.3-2 scales_0.4.1     lazyeval_0.2.0  
 [5] plyr_1.8.4       tools_3.3.3      gtable_0.2.0     tibble_1.3.3    
 [9] Rcpp_0.12.12     grid_3.3.3       methods_3.3.3    rlang_0.1.1     
[13] munsell_0.4.3 

所以您想让X轴从“2016-12-07”开始,到“2016-12-14”结束? - pogibas
我想让2016-12-072016-12-14的垂直线可见。X轴应该在这两个点之前/之后稍微开始/结束(取决于如何设置“expand”参数,我认为它控制这个)。 - hhquark
2个回答

1
您可以使用 scale_x_date 调整 x 轴。使用 as.Date(range(vlines$time)) 添加限制。
这是我的代码(根据您的进行了调整):
######################
# Generate input data   

data <- data.frame(
    time = c("2016-12-09T05:07:11Z", "2016-12-10T09:42:45Z", "2016-12-09T10:04:57Z",
             "2016-12-09T02:19:04Z", "2016-12-11T17:43:02Z", "2016-12-11T05:40:48Z",
             "2016-12-11T08:47:13Z", "2016-12-12T15:41:13Z"),
    value = c(23.3, 8.1, 12.9, 12.7, 5.6, 3.9, 5.5, 27.8))
data$time <- strptime(as.character(data$time), "%Y-%m-%dT%H:%M:%S", tz = "UTC")
data$time <- as.Date(data$time, "%Y-%m-%dT%H:%M:%S")

vlines <- data.frame(time = c("2016-12-07T00:00:00Z", 
                              "2016-12-11T00:00:00Z", 
                              "2016-12-14T00:00:00Z"))
vlines$time <- strptime(as.character(vlines$time), "%Y-%m-%dT%H:%M:%S", tz = "UTC")
vlines$timeNum <- as.Date(vlines$time, "%Y-%m-%dT%H:%M:%S")

hlines <- data.frame(value = c(-20, 10, 50))

######################
# Plot your timeseries   

library(ggplot2)
ggplot(data, aes(time, value)) + 
    geom_line() +
    geom_hline(data = hlines, aes(yintercept = value), color = "red") +
    geom_vline(data = vlines, aes(xintercept = timeNum), color = "blue") +
    scale_x_date(limits = as.Date(range(vlines$time)))

结果:

enter image description here

PS:我不得不调整一些你代码中的时间/日期转换才能使其工作(你提供的代码对我来说不起作用)。

使用了sessionInfo()

R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1.9000 prompt_1.0.0       colorout_1.1-2    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.12      memuse_3.0-1      clisymbols_1.2.0  crayon_1.3.2     
 [5] grid_3.4.1        plyr_1.8.4        gtable_0.2.0      scales_0.5.0.9000
 [9] rlang_0.1.2       lazyeval_0.2.0    labeling_0.3      munsell_0.4.3    
[13] compiler_3.4.1    colorspace_1.3-2  tibble_1.3.4   

嗯,你的代码对我不起作用。我得到了 Error in Ops.Date((x - from[1]), diff(from)) : / not defined for "Date" objects。也许是版本冲突?我使用的是 ggplot2 2.2.1 版本。 - hhquark
@hhquark 我添加了我的会话信息。 - pogibas
谢谢。我也把我的加到了原帖中。看起来你在R和ggplot2方面更有优势。如果我更新的话,我会记住这一点的。 - hhquark

0

@PoGibas的答案对我来说不太适用,但稍微修改他的方法后就可以了。

library(ggplot2)

data <- data.frame(
  time=c(
    "2016-12-09T05:07:11Z", "2016-12-10T09:42:45Z", "2016-12-09T10:04:57Z",
    "2016-12-09T02:19:04Z", "2016-12-11T17:43:02Z", "2016-12-11T05:40:48Z",
    "2016-12-11T08:47:13Z", "2016-12-12T15:41:13Z"),
  value=c(23.3, 8.1, 12.9, 12.7, 5.6, 3.9, 5.5, 27.8)
)
# Each contains 3 values: 1 within the domain/range of `data` and 2 on either side
vlines <- data.frame(time=c("2016-12-07T00:00:00Z", "2016-12-11T00:00:00Z", "2016-12-14T00:00:00Z"))
hlines <- data.frame(value=c(-20, 10, 50))

data$time <- strptime(as.character(data$time), "%Y-%m-%dT%H:%M:%S", tz="UTC")
vlines$time <- strptime(as.character(vlines$time), "%Y-%m-%dT%H:%M:%S", tz="UTC")
vlines$timeNum <- as.numeric(vlines$time)

p <- ggplot(data, aes(x=time, y=value)) +
  geom_line() +
  geom_hline(data=hlines, aes(yintercept=value), color="red") +
  geom_vline(data=vlines, aes(xintercept=timeNum), color="blue") +
  scale_x_datetime(limits=as.POSIXct(range(vlines$time))) # add datetime limits

ggsave("mwe3.pdf", p)

MWE3

Working example

我现在将这个问题保留为未回答的,因为我仍然不明白为什么这是必要的。使用这种方法,如果我必须向绘图添加几个部分,我必须在进行操作时保持xmin / xmax以确保所有内容都可见。由于这在geom_hline()中不是必需的,所以我仍然认为我缺少一些重要的东西。

编辑:我接受@PoGibas的答案。看起来这就是目前ggplot2的情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接