创建第二个ggplot热图,其行顺序与第一个热图相同

时间:2019-07-19 17:55:23

标签: r ggplot2 heatmap

我正在尝试在ggplot中创建第二个热图,该图将维护第一个热图的行,以便可以比较结果。

我有一个File_A,其中包含1000多行,类似于下面的

Animal Bloodtype Count
Horse  Opos      10
Horse  Apos      5
Horse  Bpos      4
Horse  ABpos     5
Horse  Oneg      6
Horse  Aneg      7
Horse  Bneg      9
Horse  ABneg     10
Horse  Unknown   10
Cat    Opos      12
Cat    Apos      15
Cat    Bpos      14
Cat    ABpos     15
Cat    Oneg      16
Cat    Aneg      17
Cat    Bneg      19
Cat    ABneg     14
Cat    Unknown   14
Dog    Opos      9
Dog    Apos      23
Dog    Bpos      12
Dog    ABpos     42
Dog    Oneg      45
Dog    Aneg      23
Dog    Bneg      45
Dog    ABneg     32
Dog    Unknown   32
Mouse  Opos      3
Mouse  Apos      4
Mouse  Bpos      5
Mouse  ABpos     3
Mouse  Oneg      6
Mouse  Aneg      8
Mouse  Bneg      8
Mouse  ABneg     20
Mouse  Unknown   20
Pig    Opos      19
Pig    Apos      13
Pig    Bpos      22
Pig    ABpos     32
Pig    Oneg      25
Pig    Aneg      13
Pig    Bneg      35
Pig    ABneg     22
Pig    Unknown   22

还有一个modified_File_A,它也包含超过1000行,如下所示:

Animal Bloodtype Count
Horse  O      310
Horse  A      35
Horse  B      234
Horse  AB     325
Horse  O      346
Horse  A      427
Horse  B      439
Horse  AB     410
Horse  Unknown   210
Cat    O      312
Cat    A      215
Cat    B      314
Cat    AB     415
Cat    O      316
Cat    A      617
Cat    B      419
Cat    AB     314
Cat    Unknown   214
Dog    O      239
Dog    A      223
Dog    B      312
Dog    AB     342
Dog    O      245
Dog    A      423
Dog    B      345
Dog    AB     532
Dog    Unknown   132
Mouse  O      13
Mouse  A      24
Mouse  B      65
Mouse  AB     33
Mouse  O      56
Mouse  A      48
Mouse  B      28
Mouse  AB     320
Mouse  Unknown   202
Pig    O      193
Pig    A      135
Pig    B      224
Pig    AB     321
Pig    O      252
Pig    A      323
Pig    B      352
Pig    AB     222
Pig    Unknown   222

这是我用来创建第一个热图的代码

csv_file<-read.csv("~/Documents/FileA.csv")

csv_file.s <- ddply(csv_file, .(Bloodtype), transform, rescale = scale(Count))

csv_file.s$Category <- csv_file.s$Bloodtype

levels(csv_file.s$Category) <- 
  list("Opos" = c("Opos"),
       "Apos" = c("Apos"),
       "Bpos" = c("Bpos"),
       "ABpos" = c("ABpos"),
       "Oneg" = c("Oneg"),
       "Aneg" = c("Aneg"),
       "Bneg" = c("Bneg"),
       "Oneg" = c("Oneg"),
       "Unknown" = c("Unknown"))

csv_file.s$rescaleoffset <- csv_file.s$rescale + 100*(as.numeric(as.factor(csv_file.s$Category))-1)
scalerange <- range(csv_file.s$rescale)
gradientends <- scalerange + rep(c(0,100,200), each=8)
colorends <- c("white", "Aquamarine4", "white", "yellow4", "white", "turquoise4","white","orange4", "white", "slategray4","white","seagreen4","white","purple4","white","red4","white","blue4")

ggplot(csv_file.s, aes(x = Bloodtype, y = Animal, fill = Bloodtype, alpha = Count)) +
  geom_tile(fill = "white", alpha = 1) +
  geom_tile() +
  scale_alpha_continuous(breaks = seq(0, max(csv_file.s$Count), length.out =
10),limits = c(0, NA))

将y轴排列为: 猪 鼠 马 狗 猫

我正在尝试使用相同的代码来创建第二个热图,但是它不保持第一个热图的y轴顺序。

csv_file<-read.csv("~/Documents/Modified_FileA.csv")

csv_file.s <- ddply(csv_file, .(Bloodtype), transform, rescale = scale(Count))

csv_file.s$Category <- csv_file.s$Bloodtype

levels(csv_file.s$Category) <- 
list("O" = c("O"),
     "A" = c("A"),
     "B" = c("B"),
     "AB" = c("AB"),
     "Unknown" = c("Unknown"))

csv_file.s$rescaleoffset <- csv_file.s$rescale + 100*(as.numeric(as.factor(csv_file.s$Category))-1)
scalerange <- range(csv_file.s$rescale)
gradientends <- scalerange + rep(c(0,100,200), each=4)
colorends <- c("white", "Aquamarine4", "white", "yellow4", "white", "turquoise4","white","orange4","white","blue4")

ggplot(csv_file.s, aes(x = Bloodtype, y = Animal, fill = Bloodtype, alpha = Count)) +
  geom_tile(fill = "white", alpha = 1) +
  geom_tile() +
  scale_alpha_continuous(breaks = seq(0, max(csv_file.s$Count), length.out =
10),limits = c(0, NA))

我不确定是否可以通过某种方式将第一个热图的y轴顺序分配给变量,以便将其作为级别引入第二个热图?

1 个答案:

答案 0 :(得分:0)

我认为您的代码中可能只包含一些错字。 ggplot使用df作为变量,而不是代码中的csv_file.s。我尝试了您的代码,并在更新变量后为您提供的两个数据集设置了y轴。在测试可重现的代码时,最好重新启动R,并确保包括一切,包括库调用。 祝你好运