このデータセットがあるとしましょう:
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)
line.type と line.color の相互作用によって、x 変数グループと y 変数グループをプロットしようとしています。さらに、line.type を使用して線種を指定し、line.color を使用して色を指定したいと考えています。これを書くと:
ggplot(data, aes(x = x, y = y, group = interaction(line.type, line.color), colour = line.color, linetype = line.type)) + geom_line()
動作しますが、次のように aes_string を使用しようとすると:
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()
エラーが発生します:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
私は何を間違っていますか?プロットする変数がたくさんあるので、aes_string を使用する必要があります。