I want to create a legend for my horizontal lines in geom_hline. The data I am using comes from 3 different dataframes. I am also using gghighlight, I believe that it masks any legend. How do I force a legend in that case?
The 3 dataframes are: 1) 'dataset' which stores the values for all the points 2) 'limits' which contains the max, min and target for some parameters 3) '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)
)
This is my code:
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') +
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()
The legend should show: 'red': min/max, 'blue':target, 'green': mean. Thanks!
Your code with some minnor changes can get the job done.
Put the color argument as an aesthetic within each geom and map to it a string with the label you want.
I did not used gghighlight
to highligth the non others points. For some reason using this function disable the legends.
In case you need to chage the colors, just add and scale_color_manual()
in the order of each geom.
CODE:
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 = "limits"), linetype='dashed') + #max line
geom_hline(data=limits, mapping=aes(yintercept = min, color = "limits"), linetype='dashed') + #min line
geom_hline(data=limits, mapping=aes(yintercept = target, color = "target"), linetype='dashed') + #target line
geom_hline(data=mean, mapping=aes(yintercept = mean, color = "mean"), linetype='dashed') + #mean line
geom_point(size=2) +
geom_point(data = filter(dataset, category != "Other"),
aes(x = datetime, y = average), color = "red", size = 3) +
geom_label(data = filter(dataset, category != "Other"),
aes(x = datetime, y = average, label = average), vjust = 1.5) +
facet_wrap(param~., scales='free') +
labs(x='Time' , y='Value') +
theme_bw() +
theme(legend.position = "bottom")