我正在使用 R 中的 ggplot2 绘制折线图。我想以正确的日期格式命名高于某个阈值的点。
我绘制图表的代码是:
ggplot(DateSubset1, aes(TimeStamp)) +
geom_line(aes(y = CPU, colour = "Orange")) +
geom_line(aes(y = MEM), colour = "Black")+
scale_x_datetime(date_break = "1 days")+
geom_point(aes (x= TimeStamp, y=CPU), size = 1,colour = "Purple",
subset(DateSubset1, CPU>25 ))+
geom_point(aes (x= TimeStamp, y=MEM), size = 1,colour = "Blue",
subset(DateSubset1, MEM>10 ))+
scale_y_continuous(breaks = c(5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80))
我的图表如下所示:
我想用正确的日期格式标记这些点(高于某个阈值),就像我的数据集一样。我努力了
geom_text(aes(y=CPU, label= ifelse(CPU>25, TimeStamp, '')))
使用这个我的图表看起来像:
geom_text(aes(y= CPU,label= ifelse(CPU>25, format(TimeStamp), format =
"%y%m%d %h%m%s",'')))
和
geom_text(aes(y= CPU, label=ifelse(CPU>25, as.Date(TimeStamp), '')))
和
geom_text(aes(y= CPU, label=ifelse(CPU>25, as.Date.POSIXct(TimeStamp), '')))
数据集字符串:
data.frame':
1420 obs. of 3 variables:
$ TimeStamp: POSIXct, format: "2017-06-28 07:03:02" "2017-06-28 07:06:01"
"2017-06-28 07:09:01" ...
$ CPU : num 0.9 0.8 12.2 3.7 2.3 1.7 1.4 1.1 1 0.9 ...
$ MEM : num 1.7 1.8 1.5 1.8 1.8 1.8 1.9 1.9 1.9 2.1 ...
示例数据如下所示:
TimeStamp CPU MEM
2017-06-28 07:03:02 0.9 1.7
2017-06-28 07:06:01 0.8 1.8
2017-06-28 07:09:01 12.2 1.5
2017-06-28 07:12:01 3.7 1.8
2017-06-28 07:15:01 2.3 1.8