Reputation: 177
I am trying to create a second heatmap in ggplot that would maintain the rows from the first heatmap so that I can compare the results.
I have a File_A that contains more than 1000 lines, similar to below
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
And a modified_File_A that also contains more than 1000 lines, similar to below:
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
This is the code I used to create the FIRST heatmap
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))
Which arranges the y-axis as: Pig Mouse Horse Dog Cat
I am trying to use the same code to create a second heatmap but it doesnt maintain the y-axis order from the first heatmap..
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))
I'm not sure if there is a way I can assign the y-axis order from first heatmap to a variable so I can introduce it to second heatmap as a level?
Upvotes: 0
Views: 197
Reputation: 584
I think you might just have some typos in your code. The ggplot is using df as a variable, not the csv_file.s you have in your code. I tried your code and the y-axis was the same for both datasets you provided once I updated the variables. It's a good idea to restart R when testing reproducible code and make sure to include everything, including your library calls. Good luck
Upvotes: 1