public
Last active

Line Breaks Between Words in Axis Labels in ggplot in R

  • Download Gist
lineBreaks.R
R
1 2 3 4 5 6 7 8 9 10
library(OIdata)
data(birds)
library(ggplot2)
 
levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect))
ggplot(birds,
aes(x = effect,
y = speed)) +
geom_boxplot() +
coord_flip()
sessionInfo.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
R version 3.0.1 (2013-05-16)
Platform: x86_64-pc-linux-gnu (64-bit)
 
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
 
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
 
other attached packages:
[1] gridExtra_0.9.1 ggplot2_0.9.3.1 OIdata_1.0 maps_2.3-6
[5] RCurl_1.95-4.1 bitops_1.0-6
 
loaded via a namespace (and not attached):
[1] colorspace_1.2-3 dichromat_2.0-0 digest_0.6.3 gtable_0.1.2
[5] labeling_0.2 MASS_7.3-29 munsell_0.4.2 plyr_1.8
[9] proto_0.3-10 RColorBrewer_1.0-5 reshape2_1.2.2 scales_0.2.3
[13] stringr_0.6.2 tools_3.0.1
tableAsImage-simple.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14
library(OIdata)
data(birds)
library(gridExtra)
 
# line breaks between words for levels of birds$effect:
levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect))
 
xyTable <- table(birds$sky, birds$effect)
 
plot.new()
grid.table(xyTable,
# change font sizes:
gpar.coltext = gpar(cex = 1.2),
gpar.rowtext = gpar(cex = 1.2))
tableAsImage.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
library(OIdata)
data(birds)
library(ggplot2)
library(gridExtra)
 
# line breaks between words for levels of birds$effect:
levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect))
 
xyTable <- table(birds$sky, birds$effect)
 
qplot(1:10, 1:10, geom = "blank") +
theme_bw() +
theme(line = element_blank(),
text = element_blank()) +
annotation_custom(grob = tableGrob(xyTable,
# change font sizes:
gpar.coltext = gpar(cex = 1.2),
gpar.rowtext = gpar(cex = 1.2)),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.