在数据框中查找ID并绘制另一个数据框中的值

时间:2018-10-31 08:18:51

标签: r ggplot2

对于df1中的每一对,我想找出df2和df3中的匹配行,并进行散点图绘制(基于两对中的facet_grid)。 在我的示例中,它将针对8813绘制hsa-let-7a-3p,针对2519绘制hsa-let-7a-5p。 我虽然使用which函数来获取正确的行,但是没有用。

df1 <- read.table(text = "
miRNA            Gene
hsa-let-7a-3p     8813
hsa-let-7a-5p     2729
hsa-miR-199b-3p  7114", header = TRUE) 

df2

structure(c(5.27881924454587, 5.42100992826421, 5.03661514415768, 
4.61991946787712, 4.77653649009038, 4.20681831482766, 4.72326708238235, 
5.71639332668001, 4.64150222478444, 5.30683756666667, 3.12549237216981, 
3.90209754560968, 5.30553723124249, 3.84903066479419, 4.48855173958744, 
3.43691433497801), .Dim = c(4L, 4L), .Dimnames = list(c("8813", 
"2519", "2729", "4800"), c("0086", "0175", "0217", "0394")))

df3

structure(c(5.56747199573984, 17.0087987317192, 4.25668419353693, 
13.0353755634515, 12.5433058620952, 12.0651538467382, 5.12412779449881, 
15.056925223215, 2.32974750359771, 10.7172886032171, 9.51977921513182, 
8.81158700492176, 5.09668325530934, 16.1403627714742, 4.83409156331527, 
11.5486987433545, 11.3966153591115, 9.53901970845365, -4.25127156533006, 
16.6238786279913, 5.08580281916836, 12.2523973319305, 10.5197937363935, 
10.675242984158), .Dim = c(6L, 4L), .Dimnames = list(c("hsa-let-7a-3p", 
"hsa-let-7a-5p", "hsa-let-7b-3p", "hsa-let-7b-5p", "hsa-let-7c-5p", 
"hsa-let-7d-3p"), c("0086", "0175", "0217", "0394")))

1 个答案:

答案 0 :(得分:2)

合并所有三个数据帧之后,我们需要将宽数据转换为长数据:

library(tidyverse)

# add rownames as column
df2 <- rownames_to_column(as.data.frame(df2), "Gene")
df3 <- rownames_to_column(as.data.frame(df3), "miRNA")

# then merge and gather
plotDat <- merge(
  merge(df1, df2, by = "Gene") %>%
    gather(key = "myID", value = "x", -c(Gene, miRNA)),
  merge(df1, df3, by = "miRNA") %>% 
    gather(key = "myID", value = "y", -c(Gene, miRNA))
  )

# plot with facets
ggplot(plotDat, aes(x, y)) +
  geom_point() +
  facet_grid(~ miRNA + Gene)

enter image description here