I would like to put numerical values ages
into the matrix cells, while now, I have them on x-ais
library("ggplot2")
library("reshape2")
mydata <- mtcars[, c(1,3,4,5,6,7)]
cormat <- round(cor(mydata),2)
melted_cormat <- melt(cormat)
ids = c(1, 2, 3, 4, 5,6)
ages = c(11, 22, 33, 44, 55,66)
ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) +
scale_x_discrete(labels = ids ) +
scale_y_discrete(labels = ids)
Current output:
My ideas
With geom_text
but unsuccessful after the last scale
+ geom_text(aes(label = ages), size = 3)
Error in various attempts
Error: Aesthetics must be either length 1 or the same as the data (4): label, x, y, fill
Testing rawr's proposal in comment
+ geom_text(label = interaction(rep(ages, length(ages)), sep = ', '), size = 3)
Output with real-world data where you see that it repeats the first cell ID's Age for all columns; maybe, it could be enough to include the first age in the diagonal because otherwise we need two ages per cell which makes the matrix look crowded
Trying to adjust the wiki answer to the real case
I cannot adjust the wiki answer and its ifelse
to my real case where the following geom-text
works
geom_text(label = interaction(rep(ages, length(ages)), sep = ', ')) +
OS: Debian 8.5
R: 3.1.1
See Question&Answers more detail:
os