I am trying to create a chart with two data dependent line formats: the “x opp”-data (high values) with a solid line, and the “x ned”-data (low values) with a longdashed line.
Using this code I get the opposite line format, and to taunt me R even labels the legend opposite
library(ggplot2)
df <- data.frame(
grp_val=c("x opp", "x opp", "x opp", "x ned", "x ned", "x ned"),
x_val=c(1, 2, 3, 1, 2, 3),
y_val=c(14, 16, 15, 5, 7, 6)
)
ggplot(df, aes(x_val, y_val)) +
geom_line(aes(group=grp_val, linetype = ifelse(grepl("opp", grp_val), "solid", "longdash")))
I’m guessing the aes is unhappy with an ifelse inside it. But why not throw an error, instead of plotting the opposite? Please give me a hint, cause I’m old and dumb…