I am using ggcorrplot
to plot the correlation matrix of large datasets. The problem is that it's difficult to see the labels of the axis text (in the example below it would be e.g., sample1
). Associated to each label, I have another label (in the example below it would be e.g., normal
for sample1
). Is it possible to annotate the axis text labels with a parenthesis or a box around each level of the samples' labels?
cor.vals <- cor(matrix(rnorm(100), nrow = 10))
colnames(cor.vals) <- paste0("sample", 1:10)
rownames(cor.vals) <- colnames(cor.vals)
ggcorrplot::ggcorrplot(cor.vals)
labels.samples <- c(rep("normal", 5), rep("drug", 5))
Similarly for the x-axis. My data are much larger with many more categories, so adding them manually is not really an option.
CodePudding user response:
The exact look you are going for is not achievable directly within ggplot
. You can do it by adding shapes manually using grid
, but I note that you feel this is not really an option.
One logically equivalent and similar look could be achieved with facets, though that requires fiddling with the output of ggcorrplot
:
p <- ggcorrplot::ggcorrplot(cor.vals)
p$data$facets_y <- factor(rep(labels.samples, length = nrow(p$data)), c("normal", "drug"))
p$data$facets_x <- rep(labels.samples, each = length(labels.samples))
p$coordinates <- coord_cartesian()
p facet_grid(facets_x~facets_y, scales = "free", switch = "both")
theme(panel.spacing = unit(-0.02, "npc"),
axis.ticks.y.left = element_blank(),
strip.background = element_rect(color = c("black")))