ggplot2:如何根据_multiple_ groups区分/ plot?

时间:2011-11-25 20:11:57

标签: r ggplot2

下图是根据两组(由变量“m”指定)应包含不同类型(实线和虚线)的线的图。正如你所看到的那样,它接近于正确,但这些线条随机地连接点而不是仅连接相应组中的点。

这看起来与哈德利的书(第50页)非常相似,但是虽然我使用了“组” - 变量,但它仍然不如预期。

## 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.

1 个答案:

答案 0 :(得分:2)

正如@lselzer所说,尝试删除第一个geom_line并将linetype参数移到aes

ggplot(mdf., aes(x=xz, y=Mean, shape=x)) +
    geom_point() + 
    geom_line(aes(group=m,linetype = m)) + # indicate group 2 by dashed lines
    scale_shape_manual(values=sym, breaks=x) +
    labs(x="x times z", y="Mean")