Graphics Reference
In-Depth Information
Figure 7-12. A shaded rectangle
Discussion
Each layer is drawn in the order that it's added to the ggplot object, so in the preceding example,
the rectangle is drawn on top of the line. It's not a problem in that case, but if you'd like to have
the line above the rectangle, add the rectangle first, and then the line.
Any geom can be used with annotate() , as long as you pass in the proper parameters. In this
case, geom_rect() requires min and max values for x and y .
Highlighting an Item
Problem
You want to change the color of an item to make it stand out.
Solution
To highlight one or more items, create a new column in the data and map it to the color. In this
example, we'll create a new column, hl , and set its value based on the value of group :
pg <- PlantGrowth # Make a copy of the PlantGrowth data
pg$hl <- "no" # Set all to "no"
pg$hl[pg$group == "trt2" ] <- "yes" # If group is "trt2", set to "yes"
Then we'll plot it with manually specified colors and with no legend ( Figure 7-13 ):
ggplot(pg, aes(x = group, y = weight, fill = hl)) + geom_boxplot() +
scale_fill_manual(values = c( "grey85" , "#FFDDCC" ), guide = FALSE
FALSE )
Search WWH ::




Custom Search