2
我繪製了許多時間序列圖,並且需要具有一致的x值範圍(日期)以便比較圖。我試圖使用expand_limits
,但它不起作用。解決問題的最佳方法是什麼?我應該使用continuous_scale
嗎?在ggplot2中擴展日期的軸範圍
這會產生不同的範圍:
library(ggplot2)
#Example Data
ID <- c(rep(1, 3), rep(2, 3))
date1 <- as.Date(c("2015-02-01", "2015-03-01", "2015-04-01",
"2015-03-01", "2015-03-15", "2015-03-31"), "%Y-%m-%d")
v1 <- rep(1:3, 2)
df <- data.frame(ID, date1, v1)
df
p1 <- ggplot(df[df$ID == 1,], aes(x = date1, y = v1)) +
geom_point(size = 3, colour = "#0000FF")
p2 <- ggplot(df[df$ID == 2,], aes(x = date1, y = v1)) +
geom_point(size = 3, colour = "#0000FF")
plot(p1)
plot(p2)
這是我正在努力解決它:
p2 <- ggplot(df[df$ID == 2,], aes(x = date1, y = v1)) +
geom_point(size = 3, colour = "#0000FF") +
expand_limits(x = c("2015-02-01", "2015-04-01"))
plot(p2)
這是錯誤:Error: Invalid input: date_trans works with objects of class Date only
謝謝!