当我将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)
修改: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
答案 0 :(得分:1)
您可以使用dependencies {
...
compile 'com.applovin:applovin-sdk:7.3.2' <-- add this line
...
}
调整x轴。使用scale_x_date
添加限制。
这是我的代码(根据你的调整):
as.Date(range(vlines$time))
结果:
PS:我必须在您的代码中调整一些时间/日期转换才能工作(您提供的代码对我不起作用)。
使用######################
# 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)))
:
sessionInfo()
答案 1 :(得分: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)
我现在暂时没有回答这个问题,因为我仍然不明白为什么这是必要的。使用这种方法,如果我必须在绘图中添加几个部分,我必须保持xmin / xmax,以确保一切都可见。由于geom_hline()
没有必要这样做,我仍然认为我错过了一些重要的东西。
编辑:我接受了@PoGibas的回答。似乎这就是ggplot2
现在的方式。