使用 corplot 制作遗传相关图

问题描述 投票:0回答:1

我正在尝试使用 corplot 制作遗传相关图,但不断收到此错误:

Error in corrplot(genetic_corr_matrix, method = "circle") : 
  The matrix is not in [-1, 1]!

这是我的数据框:

                          AD_Bellenguez AD_Kunkle Education Nonresponse_participation Weighted_participation
AD_Kunkle                 "0.9523"      NA        NA        NA                        NA                    
Education                 "0.0483"      "-0.2048" NA        NA                        NA                    
Nonresponse_participation "-0.0088"     "0.1893"  "-0.4016" NA                        NA                    
Participation             "-0.0595"     "-0.3027" "0.6584"  "-0.3159"                 "0.9599"              
Weighted_participation    "0.1164"      "-0.1751" "1.3116"  "-0.7404"                 NA    

我尝试过的代码:

library(corrplot)
corrplot(genetic_corr_matrix, method = "circle")

谢谢您的帮助!

r matrix correlation genetic
1个回答
0
投票

我制作了脚本,可能会有帮助

# Create a dataframe
df <- data.frame(
  row.names = c("AD_Kunkle", "Education", "Nonresponse_participation", "Participation", "Weighted_participation"),
  AD_Bellenguez = c(0.9523, 0.0483, -0.0088, -0.0595, 0.1164),
  AD_Kunkle = c(NA, -0.2048, 0.1893, -0.3027, -0.1751),
  Education = c(NA, NA, -0.4016, 0.6584, 1.3116),
  Nonresponse_participation = c(NA, NA, NA, -0.3159, -0.7404),
  Weighted_participation = c(NA, NA, NA, 0.9599, NA)
)

# Reset row names to a regular column
df <- tibble::rownames_to_column(df, var = "var1")

# Reshape to long format
df_long <- gather(df, key = "var2", value = "value", -var1)
View(df_long)
# Create a copy of the dataframe and swap columns var1 and var2
df_long_swapped <- df_long [,c("var2","var1","value")]
colnames(df_long_swapped)<-c("var1","var2","value")

# Combine rows using bind_rows
df_combined <- bind_rows(df_long, df_long_swapped)
View(df_combined)

# Make the "value" column numeric
df_combined_unique <- df_combined  %>%
  mutate(value = as.numeric(value),
         value = ifelse(var1 == var2, ifelse(is.na(value), 100, value), value))
View(df_combined_unique)
#remove row with NA 
df_combined_unique <- na.omit(df_combined_unique)

View(df_combined_unique)
nrow(df_combined_unique)
df_matrix <- acast(df_combined_unique, var1 ~ var2, value.var = "value")

# Generate the correlation matrix
genetic_corr_matrix <- cor(df_matrix)

# Create the correlation plot
library(corrplot)
corrplot(genetic_corr_matrix, method = "circle")
© www.soinside.com 2019 - 2024. All rights reserved.