Let's say I have this dataset:
x <- rnorm(1000) y <- rnorm(1000, 2, 5) line.color <- sample(rep(1:4, 250)) line.type <- as.factor(sample(rep(1:5, 200))) data <- data.frame(x, y, line.color, line.type)
I am trying to build a group of x and y variables when line.type and line.color interact. In addition, I want to specify the type of line using line.type and color using line.color. If I write this:
ggplot(data, aes(x = x, y = y, group = interaction(line.type, line.color), colour = line.color, linetype = line.type)) + geom_line()
This works, but if I try to use aes_string like this:
interact <- c("line.color", "line.type") inter <- paste0("interaction(", paste0('"', interact, '"', collapse = ", "), ")") ggplot(data, aes_string(x = "x", y = "y", group = inter, colour = "line.color", linetype = "line.type")) + geom_line()
I get an error message:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I doing wrong? I need to use aes_string because I have many variables to build.