问题描述
我想使用
ggplot(test)+ geom_boxplot(aes(x = a,y = b))
有什么想法?
编辑:
预期结果:我只想第一次使用标签(格式为%H%M),第二个时间点没有标签。
这似乎按我的预期工作。 (您从未说过您的预期。)
plot2 = plot1 + scale_x_discrete(labels = format.Date(x,% Y /%m /%d%H))
plot2
您通过使用: aes(x = factor(a),y = b)创建了一个x因子变量,因此任何标签格式都需要考虑x-变量。我不使用ggplot2,因为它广泛的非标准评估和代码隐藏驱使我疯了,但是我确实想到了R对象的基本模式和类。
I want to use geom_boxplot and rescale my axis with scale_x_datetime at the same time. However geom_boxplot requires a factor as an input, while scale_x_datetime will only accept class POSIXct.Here is a reproducible example:
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 will be fine but the last line casts me the error:
Error: Invalid input: time_trans works with objects of class POSIXct only
however that is not an option for geom_boxplot which will execute with an undesired result if the x-value is not a factor:
ggplot(test) + geom_boxplot(aes(x=a, y=b))
Any ideas?
EDIT:
Desired result: I want to have a label for the first time only (in the format e.g. "%H%M") and no label for the second point in time.
This seems to work as I expected. (You never did say how you expected.)
plot2 = plot1 + scale_x_discrete(labels=format.Date(x, "%Y/%m/%d %H")) plot2
The point here is that you created an x-factor variable by using: aes(x=factor(a), y=b) so any label formatting needs to respect the mode of the x-variable. I don't use ggplot2 because its widespread non-standard evaluation and code hiding drives me nuts, but I do think in terms of the basic modes and classes of R objects.
这篇关于结合使用geom_boxplot和scale_x_datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!