Legends are a key component of data visualization. ggplot format controls are defined below.
Data
The diamonds data that ships with ggplot.
The Default Legend
The following example presents the default legend to be cusotmized.
1 2 |
def <- ggplot(diamonds, aes(cut, price)) + geom_boxplot(aes(fill = factor(cut))) + labs(title = "Diamonds Data", x = "Cut", y = "Price (USD)") |
Click to enlarge
Removing the Legend
Legends are created for different aesthetics, such as fill, color/colour, linetype, shape, etc. Each aesthetic has a scale function than can be called to remove the legend:
1 2 3 4 5 6 7 8 |
# Remove legend given prior aesthetic (fill) def + guides(fill = FALSE) # Use the scale for the prior aesthetic def + scale_fill_discrete(guide = FALSE) # Use theme to remove all legends competely def + theme(legend.position = "none") |
Removing the Legend Title
The logic is the same, but the syntax changes slightly to remove the legend key or title:
1 2 3 4 5 |
# Remove title for prior aesthetic (fill) def + guides(fill = guide_legend(title = NULL)) # Use theme to remove all legend titles def + theme(legend.title = element_blank()) |
Change the Legend Title and Text
The legend key is updated using the name argument and the labels are changed using the labels argument to the appropriate scale function.
1 2 3 |
def + scale_fill_discrete(name="Quality of the Cut", breaks=c("Fair", "Good", "Very Good", "Premium", "Ideal"), labels=c("Fair Stones", "Good Stones", "Very Good Stones", "Premium Stones", "Ideal Stones")) |
Of course, it also possible to modify the text in the original data.frame so the default chart legend is created with the preferred labels.
Formatting the Legend Key and Label Text
Sometimes legends are hard to read and require adjustment for improved emphasis or clarity. For this purpose use the theme() function.
1 2 3 4 5 6 7 |
# Title format def + scale_fill_discrete(name = "Quality of the Cut") + theme(legend.title = element_text(colour="orange", size = 12, face = "bold")) # Label format def + scale_fill_discrete(name = "Quality of the Cut") + theme(legend.text = element_text(colour="orange", size = 12, face = "italic")) |
Changing the Legend Position
The legend can be positioned outside of the plot box using the theme() function as follows. Position options include “top”, “bottom”, “left” and “right”. Legends can also be placed inside the plot box using x/y coordinates, where (0,0) is the lower left corner and (1,1) is the upper right corner. This chart doesn’t lend itself to an internal, but the process is still shown.
1 2 3 4 5 |
# Change legend position using text options def + guides(fill=guide_legend(title=NULL)) + theme(legend.position = "bottom") # Change legend position using x/y coordinates def + guides(fill = guide_legend(title = NULL)) + theme(legend.position = c(.80, .80)) |
Changing Legend Item Order
1 2 |
# Change legend item order def + scale_fill_discrete(breaks=c("Ideal","Premium","Very Good", "Good", "Fair")) |
Managing Multiple Legends in a Layered Chart
ggplot supports the layering of multiple data objects and graph types. Multilayered charts also present the challenge of managing multiple legends. As before, legend control is tied to use of the appropriate scale function given previously declared aesthetics. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Multiple Legends # load data data(diamonds) # calc quantiles with specified probabilities new.prob <- c(0.90, 0.95, 0.99) cut.probs <- ddply(diamonds, .(cut), numcolwise(quantile, probs = new.prob))[, c("cut", "price")] cut.probs <- transform(cut.probs, quantile = c("P90", "P95", "P99")) # layered graph ggplot(diamonds, aes(cut, price)) + geom_boxplot(aes(fill = factor(cut))) + geom_point(data = cut.probs, aes(cut, price, color = factor(quantile)), size = 5) + scale_fill_discrete(name = "Quality of the Cut") + scale_color_discrete(name = "My Quantiles") |