我正在使用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
答案 0 :(得分:0)
好的,试试这段代码:
zz = '
CPU MEM
0.9 1.7
0.8 1.8
12.2 1.5
'
df <- read.table(text = zz, header = TRUE)
df
TmS = c("2017-06-28 07:03:02", "2017-06-28 07:06:01", "2017-06-28 07:09:01")
df = cbind(TmS, df)
df$TmS = as.character(df$TmS)
label = as.character(ifelse(df$CPU>10, df$TmS, ''))
df$TmS = as.POSIXct(df$TmS)
ggplot(df, aes(TmS)) +
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= TmS, y=CPU), size = 1,colour = "Purple",
subset(df, CPU>10))+
geom_point(aes (x= TmS, y=MEM), size = 1,colour = "Blue",
subset(df, MEM>1.5))+
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=label))