如何突出显示特定的间隔,并在ggplot中用R中相同图形上的一行标记它

时间:2015-08-05 21:22:52

标签: r graph plot

我正在尝试在同一图表上添加特定时间间隔的行。我成功地为该间隔添加了一个点但是无法绘制一条线。 我的数据集超过一个月,同一点有2000多条记录。

我面临以下问题。

  1. 我无法在特定的时间间隔内绘制图表上的彩色垂直线(目前“红色”点位于下图中)
  2. 我无法在图表上标记水平线。
  3. x轴上的TIME搞砸了。
  4. 我使用以下内容:

    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"))

    我在上述代码之后得到的图表是:

    enter image description here

    我的数据集如下所示:

    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

1 个答案:

答案 0 :(得分:1)

使用您的部分数据集,我首先缩短TIME变量以仅保留年/月/日。

sampledata$TIME <- substr(x = sampledata$TIME, start = 1, stop = 10)

借用大部分ggplot2代码,下面插入一条垂直线(您可以根据需要重新定位)并标记水平平均线(同样,您可以根据需要找到它,包括使用vjust参数)。我将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))

enter image description here