legend.justification

278 views
Skip to first unread message

Jacob Wegelin

unread,
May 29, 2011, 9:13:52 PM5/29/11
to ggp...@googlegroups.com

The example below creates a plot with a two-part legend.

I would like to place the legend at the top of the right margin, not halfway between the top and the bottom of that margin.

The "legend.justification" syntax does not return an error, but it does not seem to have any effect either.

Does documentation for "legend.justification" exist? It is briefly mentioned on page 112 of the ggplot2 book.

How would I locate the legend in the top right corner?

junk<-data.frame(
xaxis=seq(-3,3, length=30)
, gender="F"
)
junk2<-junk
junk2$gender<-"M"
junk<-rbind(junk , junk2)
rm(junk2)
junk$mu <- c(F=0.9, M=-1.3, A=1)[as.character(junk$gender)]
junk<-ddply(junk, .(gender), function( thispiece ){
thispiece$density<-dnorm( x=thispiece$xaxis, mean=thispiece[1,"mu"], sd=1)
thispiece
}
)
junk$observed <- c(F=-1.5, M=2, A=0.3)[as.character(junk$gender)]

require(ggplot2)
print(
ggplot(junk, aes(x=xaxis, y=density, group=gender, linetype=gender) )
+ geom_path()
+ geom_point( aes(x=observed, y=0.02, shape=gender), size=3)
+ scale_linetype_manual( name="Density", values=c(F=1, M=2))
+ scale_shape_manual( name="Observed", values=c(F=19, M=1))
+ scale_x_continuous(name="Possible values")
+ opts(
legend.direction = "vertical"
, legend.position = "right"
, legend.justification=c(0,0)
)
)

# Other values for legend.justification which do not seem to have any
# effect:
# , legend.justification="top"
# , legend.justification=c(1,1)
# , legend.justification=c(1,0)


Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
830 E. Main St., Seventh Floor
P. O. Box 980032
Richmond VA 23298-0032

Brandon Hurr

unread,
May 29, 2011, 9:44:25 PM5/29/11
to Jacob Wegelin, ggp...@googlegroups.com
plot + opts(legend.position = c(0.5, 0.5))


You need to adjust the legend.position and not the justification. It's kind of fiddly too. I tried using Inf (Infiinite) but that wasn't recognized. Perhaps there is a better way, but I do not know of it. 

junk<-data.frame(
       xaxis=seq(-3,3, length=30)
       , gender="F"
)
junk2<-junk
junk2$gender<-"M"
junk<-rbind(junk , junk2)
rm(junk2)
junk$mu <- c(F=0.9, M=-1.3, A=1)[as.character(junk$gender)]
junk<-ddply(junk, .(gender), function( thispiece ){
       thispiece$density<-dnorm( x=thispiece$xaxis, mean=thispiece[1,"mu"], sd=1)
       thispiece
       }
)
junk$observed <- c(F=-1.5, M=2, A=0.3)[as.character(junk$gender)]

require(ggplot2)
print(
       ggplot(junk, aes(x=xaxis, y=density, group=gender, linetype=gender) )
               + geom_path()
               + geom_point( aes(x=observed, y=0.02, shape=gender), size=3)
               + scale_linetype_manual( name="Density", values=c(F=1, M=2))
               + scale_shape_manual( name="Observed", values=c(F=19, M=1))
               + scale_x_continuous(name="Possible values")
               + opts(
                       legend.direction = "vertical"
                       , legend.position = c(0.8,0.7)
                       , legend.justification=c(0,0)
       )
)





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

Reply all
Reply to author
Forward
0 new messages