Using multiple functionalities of the ggplot2
package in order to make a plot with geom_line
, strange output is produced when several of them are combined like in the following:
Necessary libraries:
library(ggplot2)
library(dplyr)
library(plotly)
Sample dataset:
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
b = rnorm(10, mean = 2, sd = 5),
c = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
The plot:
ggplot(df, aes(x = a)) +
geom_line(aes(y = b, group = 1, color = "line_one"), size = 2, alpha = 0.6) +
geom_line(aes(y = c, group = 1, color = "line_two"), size = 2, alpha = 0.6) +
scale_y_continuous(sec.axis = sec_axis(~. + 10)) +
labs(x = "My x axis",
y = "My y axis") +
theme(axis.text = element_text(angle = 90, hjust = 0.4, vjust = -0.5)) +
geom_hline(yintercept = df$b %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6) +
scale_color_manual(
name = "",
values = c("line_one" = "red", "line_two" = "blue")
) +
theme_light()
The output produced does not show the color of the lines in the legend box: