#这是我的样本数据集 - 我认为这是堆栈溢出的正确格式,但如果不是,我可以尝试别的东西
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
----------
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
答案 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))