简体   繁体   English

错误“已选择未定义的列”-R中的弦图(圆形包装)

[英]Error 'undefined columns selected' - Chord diagram (circlize package) in R

I need some help with an error message returning when using the chordDiagram() function from the circlize package. 使用circlize包中的chordDiagram()函数时,我需要一些有关返回错误消息的帮助。

I am working with fisheries landings. 我正在与渔业登陆有关。 Fishing vessels start their trip in one port (homeport PORT_DE ), and land their catch (scallops in this case) in another port (landing port PORT_LA ). 渔船从一个港口( PORT_DE港口PORT_DE )开始PORT_DE ,并在另一港口(着陆港口PORT_LA )着陆其渔获物(在这种情况下为扇贝)。 I am trying to draw a chord diagram using circlize package to visualise the flow of landings between ports. 我正在尝试使用circlize软件包绘制和弦图,以可视化端口之间的着陆流程。 I have 161 unique ports and the port names are stored as character strings. 我有161个唯一的端口和端口名称会存储为character的字符串。

Before calling the chordDiagram() function to draw the chord diagram, I store the relevant columns in a dummy object ( m ). 在调用chordDiagram()函数绘制和弦图之前,我将相关列存储在虚拟对象( m )中。

# Store relevant column
m <- data.frame(PORT_DE = VMS_by_trips$PORT_DE_Label, 
            PORT_LA = VMS_by_trips$PORT_LA_Label, 
            SCALLOP_W = VMS_by_trips$Trip_SCALLOP_W)

head(m)
# PORT_DE  PORT_LA SCALLOP_W
# 1  Arbroath Arbroath  2.147143
# 2  Eyemouth Aberdeen  8.791970
# 3    Buckie Aberdeen  2.025833
# 4  Montrose Aberdeen  8.268540
# 5  Aberdeen Aberdeen  1.358286
# 6 Peterhead Aberdeen  0.797500

I then create an adjacency matrix using dcast() and rename rows. 然后,我使用dcast()创建一个邻接矩阵并重命名行。

require(reshape2)
m <- as.matrix(dcast(m, PORT_DE ~ PORT_LA, value.var = "SCALLOP_W", fun.aggregate = sum))
dim(m) #adjecency matrix represents port pairs
#[1] 153 138

row.names(m) <- m[,1]
m <- m[,2:dim(m)[2]]
class(m) <- "numeric"

Finally, I call the plot function chordDiagram() . 最后,我调用绘图函数chordDiagram()

library(circlize) 
chordDiagram(m) 

Unfortunately, this results in an error message. 不幸的是,这导致出现错误消息。

Error in `[.data.frame`(df, c(1, 2, 5)) : undefined columns selected

If I replace the row and column names with numbers, the function runs, and the correct plot is returned. 如果用数字替换行名和列名,函数将运行,并返回正确的图。

row.names(m) <- 1:153
colnames(m) <- 1:137

Any ideas how to run the function with the actual port names? 有任何想法如何使用实际的端口名运行该功能吗?

I have already tried to remove special characters, replace " " spaces with "_" underscores, keep a smaller number of characters, keep only a few port pairs. 我已经尝试删除特殊字符,用下划线"_"代替" " ,保留较少的字符,仅保留几个端口对。 Unfortunately the same error keeps appearing. 不幸的是,相同的错误不断出现。 Any help appreciated. 任何帮助表示赞赏。

Please note that since posting this question, I have managed to create the visualisation needed. 请注意,自发布此问题以来,我设法创建了所需的可视化文件。 Here is a link to another related question, which also includes the code to adjust various settings of a chord diagram. 这是另一个相关问题的链接,其中还包括用于调整和弦图各种设置的代码。

Adjust highlight.sector() width and placement - Chord diagram (circlize package) in R 调整highlight.sector()的宽度和位置-R中的和弦图(圆形包装)

With thanks to @ZuguangGu, the reason for the error message was the NAs in my column names. 感谢@ZuguangGu,出现错误消息的原因是我的列名中包含NAs If you remove them first, then the chord diagram plots just fine. 如果先删除它们,则和弦图的绘制就很好。 Following the same notation, please see below. 按照相同的符号,请参阅下文。

#create adjacency matrix
m <- data.frame(PORT_DE = VMS_by_trips$PORT_DE_Label, 
                PORT_LA = VMS_by_trips$PORT_LA_Label, 
                SCALLOP_W = VMS_by_trips$Trip_SCALLOP_W)


#Check for NA values in your dataset
which(is.na(m[, 1]))
which(is.na(m[, 2]))

#Remove the rows which have NA values, there will not be errors any more.
df = m
df = df[!(is.na(df[[1]]) | is.na(df[[2]])), ]

require(reshape2)
m <- dcast(df, PORT_DE ~ PORT_LA, value.var = "SCALLOP_W", fun.aggregate = sum)
row.names(m) <- m[,1]
m <- as.matrix(m[, -1])

# remove self-links
m2 = m
cn = intersect(rownames(m2), colnames(m2)) 
for(i in seq_along(cn)) {
  m2[cn[i], cn[i]] = 0
}

# Export 3 versions of the chord diagram in a PDF

library(circlize) 

pdf("test.pdf")

# Use all data
chordDiagram(m)
title("using all data")

#remove self-links
chordDiagram(m2)
title("remove self-links")

#here reduce = 0.01 means to remove ports which have capacity less than 0.01 of capacity of all ports.
chordDiagram(m2, reduce = 0.01)
title("remove self-links and small sectors")

dev.off()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM