簡體   English   中英

使用 R 為 corrplot 中的每一行添加顏色

[英]Add a color for each row in a corrplot with R

我想為 corrplot 的每一“行”添加一種顏色,但我還沒有找到任何解決方案。

我的代碼如下所示:

#install.packages("corrplot")
library("corrplot")

df <- matrix(runif(60, 20, 30), 8)

Labels <- c("var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8")
LabelColRow <- c("red", "blue", "green", "yellow", "purple", "black", "grey")

rownames(df) <- Labels
colnames(df) <- Labels

corrplot(df, 
         is.corr = FALSE,        
         method = "circle",      
         mar = c(2, 1, 3, 1),
         addgrid.col = "NA",     
         cl.pos = "n",
         tl.cex = 0.75,         
         tl.col = "black",      
         tl.srt = 360,          
         tl.offset = 0.9,       
         tl.pos = "lt"
)

所以基本上我希望 var1 的“行”為紅色,var2 的行為藍色,等等。

編輯: 這里有一個例子

看起來corrplot提供按行着色的功能。 另一種方法是在ggplot構建圖表。 更多的工作,當然,但它允許靈活地為行着色。

作為解決方案的開始:

df %>% 
  as.data.frame() %>% 
  rowid_to_column() %>% 
  pivot_longer(-rowid) %>% 
  
  ggplot() +
  geom_point(aes(x = name, y = rowid, size = value, colour = factor(rowid)) ) +
  geom_text(aes(x = name, y = rowid, label = round(value,0))) +
  scale_size(range = c(0, 20)) +
  scale_colour_manual(values = c("blue", "green", "red", "orange", "grey", "purple", "darkblue", "black", "green")) +
  guides(colour = "none", size = "none") +
  labs(x = NULL, y = NULL)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM