我想使用geom_boxplot
並同時使用scale_x_datetime
重新調整我的座標軸。但geom_boxplot
需要factor
作爲輸入,而scale_x_datetime
將只接受POSIXct
類。 這裏是一個重複的例子:結合使用geom_boxplot和scale_x_datetime
library(ggplot2)
library(scales)
dates <- c("02/27/92", "02/27/92")
times <- c("20:03:20", "22:29:56")
x <- paste(dates, times)
x <- strptime(x, "%m/%d/%y %H:%M:%S")
test = cbind(data.frame(rep(x, 20)), rnorm(40))
names(test)=c("a", "b")
plot1 = ggplot(test) + geom_boxplot(aes(x=factor(a), y=b))
plot1
plot2 = plot1 + scale_x_datetime(breaks = date_breaks("1 hour"))
plot2
plot1將被罰款,但最後一行蒙上我的錯誤:
Error: Invalid input: time_trans works with objects of class POSIXct only
然而,這並不是geom_boxplot
一個選項,這將有令人失望的結果,如果執行x值不是因素:
ggplot(test) + geom_boxplot(aes(x=a, y=b))
任何想法?
編輯:
期望的結果:我想爲只在第一次(在格式例如"%H%M"
)和在第二時間點沒有標籤的標籤。
我懷疑'scale_x_datetime'期望x變量是連續的或日期的,而且一個因子不能滿足這個期望。嘗試更改標籤的格式,而不是應用不合適的比例。 –
@BondedDust true,因爲錯誤表示需要一個'POSIXct'變量,但是'geom_boxplot'已經迫使我使用'factor'。 – mts
對,所以去找一個ggplot2格式化函數而不是'scale_x_datetime'。看看這個包的索引,我會看看'?guides'。 –