corrplot one column multiple groups single plot

97 Views Asked by At

I would like a corrplot, where I only have the first column. I got that solution from here: Corrplot with only one variable on x axis

library(corrplot) 
corrplot(cor(iris[,1:4])[1:4,1, drop=FALSE], cl.pos='n')

but I would like the first column to repeat WITHIN THE SAME PLOT by group: enter image description here

I am not married to using corrplot but need some similar solution. Thanks for any and all help.

2

There are 2 best solutions below

0
Dylan_Gomes On BEST ANSWER

Here is a hacky way to do it without an additional package:

library(corrplot)
Dat<-cor(iris[,1:4])[1:4,1, drop=FALSE]
PlotDat<-cbind(Dat,Dat,Dat)
corrplot(PlotDat, cl.pos='n')

enter image description here

0
Allan Cameron On

If you want complete control over your plot, you could do something like this using ggplot2

library(tidyverse)

as.data.frame(cor(iris[1:4])[,rep(1, 3)]) %>%
  setNames(1:3) %>%
  rownames_to_column() %>%
  pivot_longer(-rowname) %>%
  mutate(rowname = factor(rowname, rev(unique(rowname)))) %>%
  ggplot(aes(name, rowname)) +
  geom_tile(fill = 'white', color = 'black') +
  geom_point(aes(size = abs(value), color = value)) +
  coord_equal() +
  scale_x_discrete(NULL, labels = rep('Sepal.Length', 3), expand = c(0, 0),
                   position = 'top') +
  scale_y_discrete(NULL, expand = c(0, 0)) +
  scale_color_gradientn(colors = c('red3', 'red', 'white', '#4080ce', '#063062'), 
                        limits = c(-1, 1), guide = 'none') +
  scale_size_continuous(limits = c(0, 1), range = c(0, 20), guide = 'none') +
  theme(axis.text.x = element_text(angle = 90),
        plot.margin = margin(20, 20, 20, 20))

enter image description here