facet_grid plot: Why does geom_line() not draw lines?

1,825 views
Skip to first unread message

Marius Hofert

unread,
Aug 23, 2011, 2:12:07 AM8/23/11
to ggplot2
Dear all,

I would like to build a "matrix of plots" (via facet_grid) and below
is what I achieved so far. It already looks great but I can't figure
out two things:
(1) How can I connect the points (in each plot) for each group by a
line? I tried geom_line() but it does not do anything (except changing
the legend). My goal is the have something like (line)type = "b" (=
both).
(2) The "parameters" p1 (legend) and p2 (x axis) appear in
"lexicographical order" but I would like to have them in the obvious
order of increasing numbers, so p1.20, p1.50, p1.100, p1.200 and
similarly for p2. Does this work with scale_colour_manual()?

Cheers,

Marius



require(ggplot2)
require(reshape2)

## parameters of some model
p1 <- c(20, 50, 100, 200)
p2 <- c(5, 10, 20, 50, 100)
p3 <- c("group.1","group.2","group.3","group.4","group.5")
p4 <- c(0.2, 0.5, 0.8)

## replications
N <- 1000

## lengths
l1 <- length(p1)
l2 <- length(p2)
l3 <- length(p3)
l4 <- length(p4)

## build result array containing the measurements
arr <- array(rep(NA, l1*l2*l3*l4*N), dim=c(l1, l2, l3, l4, N),
dimnames=list(
p1=paste("p1.", as.character(p1), sep=""),
p2=paste("p2.", as.character(p2), sep=""),
p3=p3,
p4=paste("p4.", as.character(p4), sep=""),
N=paste("N.", as.character(1:N), sep="")))

## fill the result array (dummy here) with values
for(i in 1:l1){
for(j in 1:l2){
for(k in 1:l3){
for(l in 1:l4){
arr[i,j,k,l,] <- i+j+k+l+runif(N)
}
}
}
}

## create molten data
mdf <- melt(arr, formula = . ~ + p1 + p2 + p3 + p4 + N) # create
molten data frame
colnames(mdf) # => fine
stopifnot(dim(mdf)==c(l1*l2*l3*l4*N, 6))

## build means (dummy function here)
mdf.mean <- dcast(mdf, p1 + p2 + p3 + p4 ~ "Mean",
fun.aggregate=mean, value_var="value")

## plot
ggplot(mdf.mean, aes(x=p2, y=Mean, colour=p1)) + geom_point() +
geom_line() + facet_grid(p3 ~ p4, scales = "free_y")
## => why does geom_line() not connect the points?

Dennis Murphy

unread,
Aug 23, 2011, 3:08:01 AM8/23/11
to Marius Hofert, ggplot2
Hi:

The usual culprit in this case is that your x-variable is a factor, so
you need to use the group aesthetic to work around it.

Sure enough, p1 and p2 are both factors
> str(mdf.mean)
'data.frame': 300 obs. of 5 variables:
$ p1 : Factor w/ 4 levels "p1.100","p1.20",..: 1 1 1 1 1 1 1 1 1 1 ...
$ p2 : Factor w/ 5 levels "p2.10","p2.100",..: 1 1 1 1 1 1 1 1 1 1 ...
$ p3 : Factor w/ 5 levels "group.1","group.2",..: 1 1 1 2 2 2 3 3 3 4 ...
$ p4 : Factor w/ 3 levels "p4.0.2","p4.0.5",..: 1 2 3 1 2 3 1 2 3 1 ...
$ Mean: num 7.49 8.51 9.5 8.5 9.49 ...

Assuming you wanted separate lines by levels of p1,

ggplot(mdf.mean, aes(x=p2, y=Mean, colour=p1)) + geom_point() +

geom_line(aes(group = p1)) + facet_grid(p3 ~ p4, scales = "free_y")

You should also check out the 'group = 1' trick. A good example of its
use is found on p. 51 of Hadley's book (section 4.5.3).

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Marius Hofert

unread,
Aug 23, 2011, 3:20:42 AM8/23/11
to Dennis Murphy, ggplot2
Dear Dennis,

thank you very much, that clarified a lot. I try to put it in the "toolbox" of things I learned about ggplot2 recently. I also checked out "group=1" :-)

ggplot(mdf.mean, aes(x=p2, y=Mean, colour=p1)) + geom_point() +

geom_line(aes(group=1)) + facet_grid(p3 ~ p4, scales = "free_y")

The only thing I have left to figure out is how to freely specify the order in which the groups appear.

Cheers,

Marius

Reply all
Reply to author
Forward
0 new messages