无法在ggplot2中为geom_hline添加标签

时间:2018-04-10 22:58:31

标签: r ggplot2 label

#这是我的样本数据集 - 我认为这是堆栈溢出的正确格式,但如果不是,我可以尝试别的东西

    Date        Analyte                Result
    3/1/2016    Total Phosphorus        1.25
    5/1/2016    Total Phosphorus        1.1
    7/1/2017    Total Phosphorus        0.33
    11/1/2016   Total Organic Nitrogen  1.3
    1/1/2017    Total Organic Nitrogen  1.1
    3/1/2017    Total Organic Nitrogen  0.7
    ----------

这是我用来创建散点图并添加行的代码,但我无法使标签生效。我想标记geom_hlines。我的数据集名为tptn

tptn$Date <- as.Date(tptn$Date, "%m/%d/%Y")
        p<-ggplot(tptn, aes(x=Date, y=Result, group=Analyte, color=Analyte))  + 
            ggtitle("Average Total Phosphorus and Total Organic Nitrogen in Pond 7") +
            theme(plot.title = element_text(hjust = 0.5)) + xlab("Month") + 
            ylab("mg/L") + geom_point(size = 4) +
            theme(axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0)) + 
            scale_x_date(breaks = date_breaks("2 month"),labels = date_format("%b %Y")) 
            + geom_hline(yintercept = 0.1, linetype = 2, colour='turquoise4', size = 1) 
            + 
            geom_hline(yintercept = 0.8, linetype = 3, colour = 'red', size = 1) 
            p<-p+expand_limits(y=0) 
            p + scale_y_continuous(expand = c(0, 0.1))
    #tried this
        p + annotate(geom="text", x=3, y=2, label="Total Phosphorus",
                  color="red")
    #tried this
        + geom_text(data=tptn(x=0,y=0.1), aes(x, y), label=Total Phosphorus, vjust=-1)

这些独立工作,但我无法在两行显示标签

p + annotate(geom="text", x=as.Date("2016-09-01"), 
             y=0.18, label="Total Phosphorus Threshold", fontface="bold", colour='turquoise4') 
p + annotate(geom="text", x=as.Date("2017-9-01"), 
             y=0.9, label="Total Nitrogen Threshold", fontface="bold", colour='red')
#The figure is of my full data set

My full data set

1 个答案:

答案 0 :(得分:0)

我认为你只是将文字放在x轴上。简单地将其拉入工作中。

library(tidyverse)
library(lubridate)

dat <- data.frame(Date = c('3/1/2016', '5/1/2016', '7/1/2017', '11/1/2016', '1/1/2017', '3/1/2017'),
           Analyte = c('Total Phosphorus', 'Total Phosphorus', 'Total Phosphorus', 'Total Organic Nitrogen', 'Total Organic Nitrogen',  'Total Organic Nitrogen'),
           Result = c(1.25, 1.1, 0.33, 1.3, 1.1, 0.7))  

dat %>% 
  mutate(Date = mdy(Date)) %>% 
  ggplot(aes(Date, Result, color = Analyte)) + geom_point() +
  geom_hline(yintercept = 0.1, linetype = 2, colour='turquoise4', size = 1) +
  geom_hline(yintercept = 0.8, linetype = 3, colour = 'red', size = 1) +
  annotate(geom="text", x = as.Date("2016-09-01"), 
           y = 0.18, label = "Total Phosphorus Threshold", fontface="bold", colour='turquoise4') +
  annotate(geom="text", x = as.Date("2017-04-01"), 
           y = 0.85, label = "Total Nitrogen Threshold", fontface="bold", colour='red') +
  ggtitle("Average Total Phosphorus and Total Organic Nitrogen in Pond 7") +
  theme(plot.title = element_text(hjust = 0.5)) + xlab("Month") + 
  ylab("mg/L") +
  theme(axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0))