I am having trouble labelling my horizontal lines on a secondary axis in a facet wrap. I have 3 dataframes, 'dataset' which stores the values for all the points, 'limits' which contains the max, min and target for some parameters and 'mean' which contains the mean for every parameter. Below is a minimum reproducible sample of the datasets:
dataset <- data.frame(
param = c('A','A','A','A','A', 'T','T','T','T','T', 'N','N','N','N','N', 'R','R','R','R','R'),
category = c('Other','this','Other','Other','Other','this','Other','Other','Other','Other','Other','Other','this','Other','Other','Other','Other','Other','Other','this'),
average = c(1.55,1.46,1.42,1.57,1.58, 1.57,1.46,1.42,1.57,1.59, 1.67,1.56,1.62,1.67,1.69, 1.47,1.36,1.32,1.47,1.49),
datetime = c('2019-06-10 07:27:24','2019-06-10 08:20:24','2019-06-10 09:27:24','2019-06-10 07:45:24','2019-06-10 08:13:24',
'2019-06-10 09:27:24','2019-06-10 10:20:24','2019-06-10 11:27:24','2019-06-10 09:45:24','2019-06-10 10:13:24',
'2019-06-10 13:27:24','2019-06-10 14:20:24','2019-06-10 15:27:24','2019-06-10 13:45:24','2019-06-10 14:13:24',
'2019-06-10 18:27:24','2019-06-10 19:20:24','2019-06-10 20:27:24','2019-06-10 18:45:24','2019-06-10 19:13:24')
)
dataset$datetime <- as.POSIXct(dataset$datetime, format = "%Y-%m-%d %H:%M:%S")
limits <- data.frame(
param = c('A', 'T'),
target = c(1.55, 1.55),
min = c(1.39, 1.39),
max = c(1.71, 1.71)
)
mean <- data.frame(
param = c('A', 'T', 'N', 'R'),
mean = c(1.549, 1.548, 1.65, 1.45)
)
I would like to create a secondary axis which will have a tick mark with a label at the corresponding value in each parametric graph, namely 'Max'=maxvalue for the max line, 'Min'=minvalue for the min line, 'Target'=targetvalue for the target line and 'Mean'=meanvalue for the mean line.
This is what I have tried for creating the Mean label. However, it ends up plotting every single mean for every parameter in each plot which is wrong (eg, if I have 4 plots, there will be 4 means and the code plots 4 means in each plot instead of just 1 mean in each plot). I would like each mean to correspond to each parameter. Also, would appreciate any help to label the max, min and target lines (not all plots have them).
I'm also receiving a warning message and I'm not sure where the error is: Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf 3: In min(x) : no non-missing arguments to min; returning Inf 4: In max(x) : no non-missing arguments to max; returning -Inf
Thank you!
library(ggplot2)
library(gghighlight)
ggplot(data=dataset, mapping=aes(x=datetime, y=average)) +
geom_line(group=1, alpha=0.3, color='black') +
geom_hline(data=limits, mapping=aes(yintercept = max), color='red', linetype='dashed') + #max line
geom_hline(data=limits, mapping=aes(yintercept = min), color='red', linetype='dashed') + #min line
geom_hline(data=limits, mapping=aes(yintercept = target), color='blue', linetype='dashed') + #target line
geom_hline(data=mean, mapping=aes(yintercept = mean), color='green', linetype='dashed') + #mean line
geom_point(size=2, color='red') +
facet_wrap(param~., scales='free') +
scale_y_continuous(sec.axis=sec_axis(~.,labels=rep('Mean',length(mean$mean)), breaks=mean$mean))+
gghighlight(category!='Other', label_key = average, n=1, use_group_by = FALSE,
unhighlighted_params = list(color='black', size=1, alpha=0.7)) +
labs(x='Time' , y='Value') +
theme_bw()
My plot:
How I want the secondary axis in each plot to look like