ggplot2 - Label the points as date in the proper format in ggplot in R -
i plotting line chart using ggplot2 in r. want name points above threshold in proper date format.
my code plotting graph is:
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))
my graph looks this:
i want label these points (which above threshold) proper date format dataset has. have tried
geom_text(aes(y=cpu, label= ifelse(cpu>25, timestamp, '')))
using graph looks like:
geom_text(aes(y= cpu,label= ifelse(cpu>25, format(timestamp), format = "%y%m%d %h%m%s",'')))
and
geom_text(aes(y= cpu, label=ifelse(cpu>25, as.date(timestamp), '')))
and
geom_text(aes(y= cpu, label=ifelse(cpu>25, as.date.posixct(timestamp), '')))
string of dataset:
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 ...
the sample data looks like:
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
ok, try code:
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))
Comments
Post a Comment