[英]how to highlight a specific interval and label it in ggplot with a line on the same graph in R
I am trying to add a line for a specific time interval on the same graph. 我正在尝试在同一张图表上为特定时间间隔添加一条线。 I was successful in adding a point for that interval but not able to plot a line. 我成功地为该间隔添加了一个点,但无法绘制一条线。 My data set spans over a month and has 2000+ records for the same Point. 我的数据集跨越一个月,并且针对同一Point具有2000多个记录。
I am facing the following problems. 我面临以下问题。
I am using the following : 我正在使用以下内容:
ggplot(sampledata, aes(x=TIME, y=TEMP)) + geom_line(colour="blue") + geom_point(data=sampledata[sampledata$ISFAILED==1, ], aes(x=TIME, y=TEMP), colour="red", size =5) + geom_hline(yintercept=mean(sampledata$TEMP)) + annotate("text", mean(sampledata$TEMP), mean(sampledata$TEMP), label = "Average Temp of the Point") + scale_y_continuous(limits=c(20,60)) + scale_x_datetime(breaks="3 hour") + ggtitle("Analysis of the Failed Point") + theme(plot.title = element_text(lineheight=.8, face="bold"))
The graph which I am getting after the above code is : 在上面的代码之后我得到的图形是:
my data set looks like this: 我的数据集如下所示:
POINTs TIME ISFAILED TEMP Point A 2014-08-01-01.29.21.904990 0 48 Point A 2014-08-01-03.26.13.074337 0 51 Point A 2014-08-01-07.53.10.520026 0 50 Point A 2014-08-01-04.32.55.003535 0 51 Point A 2014-08-01-02.52.45.467612 0 50 Point A 2014-08-01-06.46.18.597976 0 50 Point A 2014-08-01-07.19.53.073411 0 49 Point A 2014-08-01-05.40.10.461919 0 47 Point A 2014-08-01-00.56.06.125871 0 50 Point A 2014-08-01-09.00.30.929944 0 51 Point A 2014-08-01-11.13.55.381132 0 51 Point A 2014-08-01-17.36.20.011432 0 53 Point A 2014-08-01-15.39.52.437406 0 48 Point A 2014-08-01-16.13.07.448502 0 54 Point A 2014-08-01-16.29.48.596822 0 54 Point A 2014-08-01-07.03.11.885053 0 51 Point A 2014-08-01-13.44.01.552165 0 50 Point A 2014-08-01-06.29.38.790211 0 49 Point A 2014-08-01-10.39.58.155461 0 51 Point A 2014-08-01-08.09.55.884192 0 49 Point A 2014-08-01-09.33.52.323988 0 51 Point A 2014-08-01-10.57.19.051928 0 50 Point A 2014-08-01-12.53.29.557342 0 49 Point A 2014-08-01-17.03.36.697180 0 54 Point A 2014-08-01-02.02.46.494588 0 50 Point A 2014-08-01-02.36.05.328555 0 48 Point A 2014-08-01-04.49.30.035927 0 50 Point A 2014-08-01-11.30.39.280761 0 51 Point A 2014-08-01-11.46.46.207718 0 48 Point A 2014-08-02-11.05.02.353600 0 56 Point A 2014-08-02-09.25.17.728003 0 58 Point A 2014-08-02-03.35.40.512034 0 54 Point A 2014-08-02-06.22.47.452380 0 49 Point A 2014-08-01-19.33.18.048757 0 55 Point A 2014-08-02-01.39.03.352322 0 58 Point A 2014-08-02-04.59.25.138487 0 55 Point A 2014-08-02-07.28.47.747091 1 29 Point A 2014-08-02-02.45.45.520469 0 55 Point A 2014-08-01-23.59.15.588449 0 57 Point A 2014-08-02-05.48.58.214984 0 48 Point A 2014-08-02-19.57.24.112321 0 30 Point A 2014-08-02-09.08.40.053227 0 58 Point A 2014-08-02-04.25.34.167623 0 55
Using your partial data set, I first shortened the TIME variable to keep only the year/month/date. 使用您的部分数据集,我首先缩短了TIME变量,以仅保留年/月/日。
sampledata$TIME <- substr(x = sampledata$TIME, start = 1, stop = 10)
Borrowing much of your ggplot2
code, the following inserts a vertical line (which you can relocate as you wish) and labels the horizontal mean line (and, again, you can locate it as as you wish, including using the vjust
argument). 借用了许多ggplot2
代码,以下代码插入一条垂直线(您可以根据需要重新定位)并标记水平中线(并且再次可以根据需要进行定位,包括使用vjust
参数)。 I angled the TIME axis text so that each day is more visible, but you can also shorten the variable more, reduce the font size, show every other day, etc. for further refinement on the entire data set. 我调整了TIME轴文本的角度,以使每一天都更加可见,但是您还可以进一步缩短变量,减小字体大小,隔天显示等,以便进一步完善整个数据集。
ggplot(sampledata, aes(x=TIME, y=TEMP)) +
geom_line(colour="blue") +
geom_point(data=sampledata[sampledata$ISFAILED==1, ], aes(x=TIME, y=TEMP), colour="red", size =5) +
geom_hline(yintercept=mean(sampledata$TEMP)) +
geom_vline(xintercept = 1.5, color = "red") +
annotate("text", x = 1.5, y = mean(sampledata$TEMP), label = "Average Temp of the Point") +
theme(axis.text.x = element_text(angle = 30))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.