简体   繁体   中英

Add annotation to axis text for ggcorrplot

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 eg, sample1 ). Associated to each label, I have another label (in the example below it would be eg, 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.

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")))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM