英文:
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,但需要类似的解决方案。感谢任何帮助。
英文:
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:
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')
英文:
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')
答案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))
英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论