在 R 中的 ggplot 中以正确的格式将点标记为日期
Label the points as date in the proper format in ggplot in R
我正在使用 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
好的,试试这个代码:
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))
我正在使用 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
好的,试试这个代码:
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))