Graphics Reference
In-Depth Information
timeHMS_formatter(c( .33 , 50 , 51.25 , 59.32 , 60 , 60.1 , 130.23 ))
"0:20" "50:00" "51:15" "59:19" "1:00:00" "1:00:06" "2:10:14"
The scales package, which is installed with ggplot2, comes with some built-in formatting func-
tions:
comma() adds commas to numbers, in the thousand, million, billion, etc. places.
dollar() adds a dollar sign and rounds to the nearest cent.
percent() multiplies by 100, rounds to the nearest integer, and adds a percent sign.
scientific() gives numbers in scientific notation, like 3.30e+05 , for large and small num-
bers.
If you want to use these functions, you must first load the scales package, with lib-
rary(scales) .
Changing the Appearance of Tick Labels
Problem
You want to change the appearance of tick labels.
Solution
In Figure 8-17 (left), we've manually set the labels to be long—long enough that they overlap:
bp <- ggplot(PlantGrowth, aes(x = group, y = weight)) + geom_boxplot() +
scale_x_discrete(breaks = c( "ctrl" , "trt1" , "trt2" ),
labels = c( "Control" , "Treatment 1" , "Treatment 2" ))
bp
To rotate the text 90 degrees counterclockwise ( Figure 8-17 , middle), use:
bp + theme(axis.text.x = element_text(angle = 90 , hjust = 1 , vjust = .5 ))
Rotating the text 30 degrees ( Figure 8-17 , right) uses less vertical space and makes the labels
easier to read without tilting your head:
bp + theme(axis.text.x = element_text(angle = 30 , hjust = 1 , vjust = 1 ))
Search WWH ::




Custom Search