gpt4 book ai didi

r - ggplot2 不调整日期时间 vline 的绘图大小

转载 作者:行者123 更新时间:2023-12-04 12:29:57 25 4
gpt4 key购买 nike

当我将 geom_hline() 添加到绘图时,绘图会调整大小以适应它们。但是,当我添加 geom_vline() 时,绘图不会调整大小。

为什么会这样?我怎样才能让绘图调整大小?

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

Plot with data only

mwe2.pdf

Plot resized for <code>hlines</code>, but not <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

最佳答案

您可以使用 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

关于r - ggplot2 不调整日期时间 vline 的绘图大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46331477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com