mixing tiled, point, and line in a legend

43 views
Skip to first unread message

bekozi

unread,
May 16, 2012, 12:58:57 PM5/16/12
to ggp...@googlegroups.com
Hi! My issue is related to the creation of manual scales mixing tiled, line, and point layers. I have successfully created manual legends for point and line data in the past, but am getting stuck when adding a gradient into the mix.

The code below generates an example plot. I want the legend to include the gradient scale definition, a line description, and a point description. I did not include my futile attempts to get it working... I am not sure it is possible, so wanted to check before whiling away. There may be a “melt” approach in here, but I don’t see it.

Thanks in advance!

require(ggplot2)

## tiled data
tile = expand.grid(x=seq(1,100),y=seq(1,100))
tile$z = tile$x*tile$y

## random "observations" (the points)
obs = tile[sample(seq(1,nrow(tile)),25),c("x","y")]

## construct the plot -----------------------------

g = ggplot()
## tile layer
g = g + geom_tile(data=tile,aes(x=x,y=y,fill=z))
## add contours
g = g + stat_contour(data=tile,aes(x=x,y=y,z=z))
## point layer
g = g + geom_point(data=obs,aes(x=x,y=y))
print(g)

Dennis Murphy

unread,
May 16, 2012, 5:01:27 PM5/16/12
to bekozi, ggp...@googlegroups.com
Hi:

In order to create a legend for a non-positional aesthetic, you have
to map an aesthetic to a variable. You haven't done that for the
points and contour lines, so no legend is created for them. It's not
at all clear to me why you would want a merged (?) legend if fill
color is the only mapped aesthetic in the plot. What do you mean by
'line description' and 'point description'?

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

bekozi

unread,
May 16, 2012, 6:54:44 PM5/16/12
to ggp...@googlegroups.com, bekozi
Thanks for pointing out the aesthetic mapping. Attached is the result I was looking for...almost. I would love less white space between "Surface Contour" and "Observation", but that's just splitting hairs. I had originally hoped to build the legend by stacking scale_colour_manual, scale_linetype_manual, and scale_point_manual, but it seems to get complicated quickly (at least for me).

Sorry, my terminology is probably bad. By "description", I had meant a key,value pair in the legend for the point and line geometries, so yes, my original code would have been confusing.

Appreciate the help! New code is below...

-Ben


require(ggplot2)

## tiled data
tile = expand.grid(x=seq(1,100),y=seq(1,100))
tile$z = tile$x*tile$y
tile$brk = "Surface Contour"

## random "observations" (the points)
obs = tile[sample(seq(1,nrow(tile)),25),c("x","y")]
obs$brk = "Observation"

## construct the plot -----------------------------------------------------------

g = ggplot()
## tile layer
g = g + geom_tile(data=tile,aes(x=x,y=y,fill=z))
## add contours
g = g + stat_contour(data=tile,aes(x=x,y=y,z=z,linetype=brk))
## point layer
g = g + geom_point(data=obs,aes(x=x,y=y,shape=brk))

g$options$labels$fill = "Surface Value"
g$options$labels$linetype = ""
g$options$labels$shape = ""

print(g)
plot.png
Reply all
Reply to author
Forward
0 new messages