Legend Attributes - janeshdev/ggplot2 GitHub Wiki

Legend Attributes

Return to opts() list

Base Plot

#create simple dataframe for plotting
xy <- data.frame(x=1:10, y=10:1, type = rep(LETTERS[1:2], each=5))

#create base plot
plot <- ggplot(data = xy)+
geom_point(aes(x = x, y = y, color=type))

#plot base plot
plot

http://i.imgur.com/mCJDb.png

legend.background (rect)

#color doesn't work, need colour
plot + theme(legend.background = element_rect(colour = 'purple', fill = 'pink', size = 3, linetype='dashed'))

http://i.imgur.com/HCzNH.png

legend.key (rect)

#color doesn't work, need colour
plot + theme(legend.key = element_rect(colour = 'purple', fill = 'pink', size = 0.5, linetype='dashed'))

http://i.imgur.com/VmcfX.png

legend.key.size (unit)

plot + theme(legend.key.size = unit(2, "cm"))

http://i.imgur.com/HctdR.png

legend.key.width (unit)

plot + theme(legend.key.width = unit(5, "cm"))

http://i.imgur.com/Ml0Hj.png

legend.text (text)

#color doesn't work, need colour
plot + theme(legend.text = element_text(colour = 'red', angle = 45, size = 10, hjust = 3, vjust = 3, face = 'bold'))

http://i.imgur.com/2iJVk.png

legend.title (text)

#color doesn't work, need colour
plot + theme(legend.title = element_text(colour = 'red', angle = 45, size = 10, hjust = 3, vjust = 7, face = 'italic'))

http://i.imgur.com/Wpiv8.png

legend.position (?)

#get rid of the legend
plot + theme(legend.position = 'none')
#move the legend, accepts left, right, top bottom
plot + theme(legend.position = 'left')
#define relative coordinates on plot c(x, y) between 0 and 1 
plot + theme(legend.position = c(0.5, 0.5))

alight=right

legend.justification (?)

#Justification defines which side of the legend that the legend.position coordinates refer to
plot + theme(legend.justification = 'right', legend.position=c(0.25,0.3))
#can use left, right, centre or numeric value (0 ≤ x ≤ 1)

http://i.imgur.com/h6doV.png