R:填写数据框以创建对称恒等式图
R: Filling out dataframe to create symmetric identity plot
我有一些计算被写入文件并读入安排如下的数据帧:
sequence_1 sequence_2 identity
CP010953 CP010953 100
CP010953 CP012689 73.9
CP010953 CP000025 73.86
CP010953 CP012149 73.77
CP010953 HE978252 73.72999999999999
CP010953 CP009043 83.35000000000001
数据来自计算(在 Python 中),该计算计算两个字符串之间的字符匹配数除以其中一个字符串的长度(两个字符串的长度相同)。当时这似乎是个好主意,但是当我进行计算时,我使用 itertools.combinations_with_replacement 命令来使计算更快。因此,如果我比较 3 个字符串 (a、b、c),它只会比较 a&b、a&c、b&c,而不比较 b&a、c&a 和 c&b,因为它们分别具有与 a&b、a&c 和 b&c 相同的值。问题是,当我将数据读入 R 并绘制热图时,我得到的结果是:
那是一堆空白(您可能会看到我需要的值都在那里 -- 例如:AL111168 和 CP000538(都位于左下角)在 y 轴上有值, 但不是 x 轴)!
有没有办法用 R 中的适当值来填补这些空白?我可以在循环中执行此操作,但这不是很 R-esque。我确定之前有人问过这个问题,但我认为我使用的搜索词不正确。
这是我的一些代码:
args = commandArgs(trailingOnly=TRUE)
file_name <- args[1]
gene_name <- args[2]
image_name = paste(gene_name, '.png', sep='')
myDF <- read.csv(file_name, header=T, sep='\t')
my_palette <- colorRampPalette(c('red', 'yellow', 'green'))
png(filename=image_name, width=3750,height=2750,res=300)
par(mar=c(9.5,4.3,4,2))
print(corpus <- qplot(x=sequence_1, y=sequence_2, data=myDF, fill=identity, geom='tile') +
geom_text(aes(label=identity), color='black', size=3) +
scale_fill_gradient(limits=c(0, 100), low='gold', high='green4') +
labs(title='Campylobacter Pair-wise Sequence Identity Comparison', x=NULL, y=NULL) +
guides(fill = guide_legend(title = 'Sequence\nSimilarity %', title.theme = element_text(size=15, angle = 0))) + theme(legend.text=element_text(size=12)) +
theme(axis.text.x=element_text(angle=45, size=14, hjust=1, colour='black'), axis.text.y=element_text(size=14, hjust=1, colour='black')) )
dev.off()
提前致谢。
我想出了办法
mDF <- myDF
colnames(mDF)[1] <- 'sequence_2'
colnames(mDF)[2] <- 'sequence_1'
newDF <- rbind(mDF, myDF)
然后绘制newDF。
我有一些计算被写入文件并读入安排如下的数据帧:
sequence_1 sequence_2 identity
CP010953 CP010953 100
CP010953 CP012689 73.9
CP010953 CP000025 73.86
CP010953 CP012149 73.77
CP010953 HE978252 73.72999999999999
CP010953 CP009043 83.35000000000001
数据来自计算(在 Python 中),该计算计算两个字符串之间的字符匹配数除以其中一个字符串的长度(两个字符串的长度相同)。当时这似乎是个好主意,但是当我进行计算时,我使用 itertools.combinations_with_replacement 命令来使计算更快。因此,如果我比较 3 个字符串 (a、b、c),它只会比较 a&b、a&c、b&c,而不比较 b&a、c&a 和 c&b,因为它们分别具有与 a&b、a&c 和 b&c 相同的值。问题是,当我将数据读入 R 并绘制热图时,我得到的结果是:
那是一堆空白(您可能会看到我需要的值都在那里 -- 例如:AL111168 和 CP000538(都位于左下角)在 y 轴上有值, 但不是 x 轴)!
有没有办法用 R 中的适当值来填补这些空白?我可以在循环中执行此操作,但这不是很 R-esque。我确定之前有人问过这个问题,但我认为我使用的搜索词不正确。
这是我的一些代码:
args = commandArgs(trailingOnly=TRUE)
file_name <- args[1]
gene_name <- args[2]
image_name = paste(gene_name, '.png', sep='')
myDF <- read.csv(file_name, header=T, sep='\t')
my_palette <- colorRampPalette(c('red', 'yellow', 'green'))
png(filename=image_name, width=3750,height=2750,res=300)
par(mar=c(9.5,4.3,4,2))
print(corpus <- qplot(x=sequence_1, y=sequence_2, data=myDF, fill=identity, geom='tile') +
geom_text(aes(label=identity), color='black', size=3) +
scale_fill_gradient(limits=c(0, 100), low='gold', high='green4') +
labs(title='Campylobacter Pair-wise Sequence Identity Comparison', x=NULL, y=NULL) +
guides(fill = guide_legend(title = 'Sequence\nSimilarity %', title.theme = element_text(size=15, angle = 0))) + theme(legend.text=element_text(size=12)) +
theme(axis.text.x=element_text(angle=45, size=14, hjust=1, colour='black'), axis.text.y=element_text(size=14, hjust=1, colour='black')) )
dev.off()
提前致谢。
mDF <- myDF
colnames(mDF)[1] <- 'sequence_2'
colnames(mDF)[2] <- 'sequence_1'
newDF <- rbind(mDF, myDF)
然后绘制newDF。