Created
January 13, 2017 14:55
-
-
Save trinker/b24d6abf475aaea265b490bf6b756533 to your computer and use it in GitHub Desktop.
ggplot2 labels on top
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(reshape2) | |
library(ggplot2) | |
data(mtcars) | |
dat <- with(mtcars, data.frame(mpg, cyl, disp, hp, wt, gear)) | |
cor.matrix <- round(cor(dat, use = "pairwise.complete.obs", method = "spearman"), digits = 2) | |
diag(cor.matrix)<-NA | |
cor.dat <- melt(cor.matrix) | |
cor.dat <- data.frame(cor.dat) | |
cor.dat <- cor.dat[complete.cases(cor.dat),] | |
ggplot(cor.dat, aes(Var2, Var1, fill = value)) + | |
geom_tile(colour="gray90", size=1.5, stat="identity") + | |
geom_text(data=cor.dat, aes(Var2, Var1, label = value), color="black", size=rel(4.5)) + | |
scale_fill_gradient(low = "white", high = "dodgerblue", space = "Lab", na.value = "gray90", guide = "colourbar") + | |
scale_x_discrete(expand = c(0, 0), position = "top") + | |
scale_y_discrete(expand = c(0, 0)) + | |
xlab("") + | |
ylab("") + | |
theme(panel.grid.major = element_blank(), | |
panel.grid.minor = element_blank(), | |
panel.border = element_rect(fill=NA,color="gray90", size=0.5, linetype="solid"), | |
axis.line = element_blank(), | |
axis.ticks = element_blank(), | |
panel.background = element_rect(fill="gray90"), | |
plot.background = element_rect(fill="gray90"), | |
legend.position = "none", | |
axis.text = element_text(color="black", size=14) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/26838005/putting-x-axis-at-top-of-ggplot2-chart/41598508#41598508
Trick is