corrplot一列多个组单一图。

huangapple go评论58阅读模式
英文:

corrplot one column multiple groups single plot

问题

I would like a corrplot, where I only have the first column. I got that solution from here:
https://stackoverflow.com/questions/23613188/corrplot-with-only-one-variable-on-x-axis

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

但我想要在同一图中按组重复显示第一列:
corrplot一列多个组单一图。

我不一定要使用corrplot,但需要类似的解决方案。感谢任何帮助。

英文:

I would like a corrplot, where I only have the first column. I got that solution from here:
https://stackoverflow.com/questions/23613188/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:
corrplot一列多个组单一图。

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

答案1

得分: 2

这是一种不需要额外包的巧妙方法:

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

corrplot一列多个组单一图。

英文:

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

library(corrplot)
Dat&lt;-cor(iris[,1:4])[1:4,1, drop=FALSE]
PlotDat&lt;-cbind(Dat,Dat,Dat)
corrplot(PlotDat, cl.pos=&#39;n&#39;)

corrplot一列多个组单一图。

答案2

得分: 1

如果您想完全控制您的图表,您可以使用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))

corrplot一列多个组单一图。

英文:

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)]) %&gt;%
  setNames(1:3) %&gt;%
  rownames_to_column() %&gt;%
  pivot_longer(-rowname) %&gt;%
  mutate(rowname = factor(rowname, rev(unique(rowname)))) %&gt;%
  ggplot(aes(name, rowname)) +
  geom_tile(fill = &#39;white&#39;, color = &#39;black&#39;) +
  geom_point(aes(size = abs(value), color = value)) +
  coord_equal() +
  scale_x_discrete(NULL, labels = rep(&#39;Sepal.Length&#39;, 3), expand = c(0, 0),
                   position = &#39;top&#39;) +
  scale_y_discrete(NULL, expand = c(0, 0)) +
  scale_color_gradientn(colors = c(&#39;red3&#39;, &#39;red&#39;, &#39;white&#39;, &#39;#4080ce&#39;, &#39;#063062&#39;), 
                        limits = c(-1, 1), guide = &#39;none&#39;) +
  scale_size_continuous(limits = c(0, 1), range = c(0, 20), guide = &#39;none&#39;) +
  theme(axis.text.x = element_text(angle = 90),
        plot.margin = margin(20, 20, 20, 20))

corrplot一列多个组单一图。

huangapple
  • 本文由 发表于 2023年2月18日 01:02:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定