Below is a plot which should contain lines of different types (solid and dashed) according to two groups (specified by the variable “m”). As you can see, it’s close to being correct, but the lines kind of randomly connect the points instead of only connecting the points in the corresponding group.
This looks quite similar to what’s in Hadley’s book (page 50), but although I used the “group”-variable, it’s still not as expected.
## new minimal example
require(ggplot2)
require(reshape2)
require(plyr)
set.seed(1)
## parameters
m <- c("g1", "g2")
x <- c(10, 20, 50, 100)
z <- c(5, 20, 50)
N <- 1000
## lengths
lm <- length(m)
lx <- length(x)
lz <- length(z)
## build result array containing the measurements
arr <- array(rep(NA, lm*lx*lz*N), dim=c(lm, lx, lz, N),
dimnames=list(
m=m,
x=x,
z=z,
N=1:N))
## fill with dummy data
for(i in 1:lx){
for(j in 1:lz){
arr[1,i,j,] <- 0+i+j+runif(N, min=-4, max=4)
}
}
arr[2,,,] <- arr[1,,,] + 2
names(dimnames(arr)) # "m" "x" "z" "N"
## compute a (dummy) summary statistic
means <- apply(arr, MARGIN=1:3, FUN=mean)
## create molten data
mdf <- reshape2:::melt.array(means, formula = . ~ m + x + z, value.name="Mean")
mdf. <- mutate(mdf, xz=x*z) # add x*z
mdf.$x <- as.factor(mdf.$x) # change to factor (for grouping with different shapes)
## trial
sym <- c(1, 2, 4, 20) # plot symbols
ggplot(mdf., aes(x=xz, y=Mean, shape=x)) +
geom_line(aes(group=x)) + geom_point() + # indicate group 1 by solid lines
geom_line(aes(group=m), linetype=2) + # indicate group 2 by dashed lines
scale_shape_manual(values=sym, breaks=x) +
labs(x="x times z", y="Mean")
## => Each of the two groups specified by m should be depicted by a special line type
## (solid for "g1", dashed for "g2"), but the lines are not correctly drawn...
## The goal is to connect the dots of the second group by a dashed line and to
## highlight the nodes by the same plot symbols (sym) as for the first group.
As @lselzer says, try removing the first
geom_lineand moving thelinetypeargument into theaes: