【问题标题】:ggplot2 not resizing plot for datetime vlineggplot2 未调整日期时间 vline 的绘图大小
【发布时间】:2017-09-20 20:45:15
【问题描述】:

当我将geom_hline() 添加到绘图时,绘图会调整大小以适应它们。但是当我添加geom_vline()'s 时,情节没有调整大小。

为什么会这样?如何让情节调整大小?

MWE

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

编辑: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 结束?
  • 我希望 2016-12-072016-12-14 的 vlines 可见。 x 轴应该在这两点之前/之后开始/结束(取决于我认为控制它的 "expand" 参数的设置方式)。

标签: r ggplot2


【解决方案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)))

结果:

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 我添加了我的会话信息
  • 谢谢。我也将我的添加到了 OP 中。看起来你在 R 和 ggplot2 中领先。如果我更新,我会记住这一点。
【解决方案2】:

@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

我暂时不回答这个问题,因为我仍然不明白为什么这是必要的。使用这种方法,如果我必须在绘图中添加多个部分,我必须在执行过程中保持 xmin/xmax 以确保所有内容都可见。由于 geom_hline() 没有必要这样做,我仍然认为我错过了一些重要的东西。

编辑:我接受@PoGibas 的回答。似乎这就是ggplot2 现在的样子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多