你知道如何在boxplot中更改日期格式吗?日期显示为yyyy-mm-dd,但.txt文件格式为dd-mm-yyyy。我想将格式更改为“%d%b%y”。
ra <- read.table("C:/users/david/Desktop/rc_adultos.txt", header=T, sep="\t")
ra$data <- as.Date(ra$data, format="%d-%m-%Y")
ra$rc <- as.numeric(ra$rc)
p5 <- ggplot(ra, aes(x=as.factor(data), y=rc)) +
geom_boxplot(fill="white") +
theme_classic() +
theme(axis.text.x = element_text(angle=45, hjust=1)) +
ylab("RC (mm)") +
xlab(NULL) +
stat_n_text(color = "black", size = 2.5,y.pos =0)
p5
data rc
07/07/16 0,561
07/07/16 0,561
07/07/16 0,4
07/07/16 0,401
07/07/16 1,265
07/07/16 2,169
19/08/16 0,294
19/08/16 0,358
19/08/16 0,575
19/08/16 0,688
19/08/16 0,306
19/08/16 0,334
02/09/16 4,441
02/09/16 0,376
02/09/16 0,268
02/09/16 0,361
02/09/16 0,375
02/09/16 0,428
25/04/17 2,436
25/04/17 2,107
25/04/17 1,81
25/04/17 2,753
25/04/17 3,291
25/04/17 2,411
25/04/17 2,407
有两种方法可以做到这一点,具体取决于你是否希望盒子均匀间隔(使用as.factor(format(.,"%d %b %y")
将日期转换为因子)或根据实际日期排列(使用aes(group=date)
+ scale_x_date(date_labels="%d %b %y")
如果它仍然无法正常工作,则可能与您的日期首先搞砸了...请参阅下面的示例,并附上您的示例数据。
数据(dd
)定义如下。
## note: (1) date starts as character, not format;
## (2) use %y, not %Y, for 2-digit year format
library(ggplot2); theme_set(theme_bw())
dd$date <- as.Date(dd$date, format="%d/%m/%y")
ggplot(dd,aes(date,rc))+geom_boxplot(aes(group=date))+
scale_x_date(date_label="%d %b %y")
ggsave("gg_date.png")
或许你想要:
dd$date2 <- as.factor(format(dd$date,"%d %b %y"))
ggplot(dd,aes(date2,rc))+geom_boxplot()
ggsave("gg_date2.png")
dd <- read.table(header=TRUE, stringsAsFactors=FALSE,
dec=",",
text="date rc
07/07/16 0,561
07/07/16 0,561
07/07/16 0,4
07/07/16 0,401
07/07/16 1,265
07/07/16 2,169
19/08/16 0,294
19/08/16 0,358
19/08/16 0,575
19/08/16 0,688
19/08/16 0,306
19/08/16 0,334
02/09/16 4,441
02/09/16 0,376
02/09/16 0,268
02/09/16 0,361
02/09/16 0,375
02/09/16 0,428
25/04/17 2,436
25/04/17 2,107
25/04/17 1,81
25/04/17 2,753
25/04/17 3,291
25/04/17 2,411
25/04/17 2,407")