Sunday, May 22, 2011

Legends in ggplot2

A simple plot takes a few lines of coding:


g1 <- ggplot(d, aes(birth.year))


g2 <- g1 + geom_line(aes(y=alive0, linetype="Famale")) + 
geom_line(aes(y=alive1, linetype="Male")) + scale_linetype_discrete(name = "")


g3 <- g2 + geom_point(aes(y=alive0, shape="Famale")) + 
geom_point(aes(y=alive1, shape="Male")) + scale_x_continuous("") + scale_y_continuous("Proportion Alive in 1982") + opts(legend.position=c(.24, .95), legend.justification = c(1, 1)) + scale_shape_discrete(name="")


g4 <- g3 + labs(fill="")


3 comments:

a cup in the garden said...

hi,
it would be nice if you could post also tha data you've used for the plot.
thanks,
marco

izahn said...

The more typical way to do this in ggplot2 is

dm <- melt(d, measure.vars = grep("alive", names(d)))
dm$sex <- with(dm, ifelse(variable == "alive0", "Female", "Male"))

ggplot(dm, aes(x=birth.year, y = value, linetype = sex, shape = sex)) +
geom_line() +
geom_point() +
scale_x_continuous("") +
scale_y_continuous("Proportion Alive in 1982") +
opts(legend.position=c(.24, .95), legend.justification = c(1, 1)) +
scale_shape_discrete(name="") +
scale_linetype_discrete(name="")

This is both more compact and more readable.

Mac said...

I easily produced a scatter plot using ggplot2, with a third variable (formed as a factor) used in the 'colour' option to distinguish two groups.
The plot correctly identifies points by colour according to the two groups.
The problem comes with the legend. The factor has only 2 levels, 60 and 80. Yet the legend shows 6 levels in increments of 5 between 60 and 80.
Why?
How can I correct this error?
Thanks, Glen McPherson

Counter