I've built a heatmap but I can't seem to set up groups next to the rows and columns. Please I want something like this heatmap.
#Design of the data as an example
Data <- data.frame(replicate(6,sample(0:1,6,rep=TRUE)))
row.names(Data) <- c('Gene1','Gene2','Gene3','Gene4','Gene5','Gene6')
colnames(Data) <- c("Strain1", "Strain2", "Strain3", "Strain4", "Strain5", "Strain6")
pheatmap(Data, color=colorRampPalette(c("#FFE4B5","#708090"))(2),
legend_breaks = c(1, 0),
legend_labels = c("Presence", "Absence"),
border_color = "black", display_numbers = FALSE,
number_color = "black",
fontsize_number = 8)
#Now I want to create Groups (Row) ##To designate that they are
part of the same antibiotic resistance family##
#Groupe1 "Gene3 and Gene6" Color1
#Groupe2 "Gene1 and Gene5" Color2
#Groupe3 "Gene2 and Gene4" Color3
#How can I do this?
#Same question if I want to create Groupes for column ##To
designate that they are part of the same bacterial genus#
#Band1 "Strain1" and "Strain3" Color4
#Band2 "Strain4" and "Strain5" Color5
#Band3 "Strain2" and "Strain6" Color6
You need to create little data frames for your annotations and pass them to annotation_row and annotation_col. The actual colours are passed as a named list of two named vectors to annotation_colors:
row_df <- data.frame(Group = paste("Groupe", rep(1:3, each = 2)),
row.names = paste0("Gene", 1:6))
col_df <- data.frame(Genus = c("GenusA", "GenusC", "GenusA",
"GenusB", "GenusB", "GenusC"),
row.names = paste0("Strain", 1:6))
pheatmap(Data,
color = colorRampPalette(c("#FFE4B5","#708090"))(2),
legend_breaks = c(1, 0),
legend_labels = c("Presence", "Absence"),
border_color = "black", display_numbers = FALSE,
number_color = "black",
annotation_row = row_df,
annotation_col = col_df,
fontsize_number = 8,
annotation_colors = list(Group = c("Groupe 1" = "red3", "Groupe 2" = "blue4",
"Groupe 3" = "green4"),
Genus = c(GenusA = "gold", GenusB = "purple",
GenusC = "cyan")))
Related
I want to make a legend for the color annotation blocks at right-side of the complexheatmap graph
library(ComplexHeatmap)
x11()
#list = c("R","H","K","D",'E','S','T','N','Q','C','G','P','A','V','I','L','M','F','Y','W','O','U','B','Z','X','J')
A = rep("group1", 26)
A[4:5] = "group2"
A[6:9] = "group3"
A[10:12] = "group4"
A[13:20] = "group5"
A[21:26] = "group6"
rnames <- c("+","+","+", "-", "-","δ","δ","δ","δ", "*","*","*","h","h","h","h","h","h","h","h","u","u","u","u","u","u")
ha = rowAnnotation(foo = anno_block(gp = gpar(fill = 2:7)), annotation_legend_param = list(foo = list(labels = c("+ve", "-ve", "polar", "special","hydrophobic","unique"))))
names(rnames) = unique(A)
ht = Heatmap(data2,
name = "Occurance",
cluster_rows = FALSE,
cluster_columns = FALSE,
column_title = "Amino Acid Occurance in Overlapped Region in LCD in Known RBP genes",
row_title = "AA",
column_names_gp = gpar(fontsize = 10),
row_title_gp = gpar(fontsize = 20),
column_names_rot = 90,
row_split = A,
right_annotation = ha,
row_names_side = "left",
row_names_gp = gpar(fontsize = 10),
heatmap_width = unit(12, "cm"),
heatmap_height = unit(12, "cm")
)
ht
and the result of graph is shown at below
As you can see there are 6 different color annotation block shown at right side, I want to make the legend for these 6 block named in order as "+ve", "-ve", "polar", "special","hydrophobic" and "unique". However, the above codes only result in an error of 'Error: Amount of legend params is larger than the number of simple annotations.'
Can anyone tell me and teach me how to do it?
I am creating forest plots using the meta package in R. I have added a line of text using text.addline1 (see bottom of plot). How do I make that text bold? It says see "gpar" in the cran documentation but I can't work it out.
library(meta)
## make data
df1 <- data.frame(matrix(, nrow=14, ncol=6))
colnames(df1) <- c("estimate", "std.error", "N", "study", "Exposure", "or" )
df1$estimate <- c("1.0588634", "1.0210044", "1.0484577", "0.9872621", "1.0122652",
"0.9934573", "0.9429622", "1.0320382", "0.9654555", "1.0499671", "1.0400071",
"1.0208341", "0.9954885", "0.9879208")
df1$std.error <- c(0.054092871, 0.037674050, 0.093067276, 0.054198110, 0.008714173, 0.010266589,
0.056993648, 0.058017078, 0.058050610, 0.089878607, 0.056951120, 0.011041428,
0.013533755, 0.090526260)
df1$N <- 500
df1$study <- "Study"
df1$Exposure <- "Smoker"
df1$Exposure[7:14] <- "Non smoker"
df1$or <- c(0.057196058, 0.020786829, 0.047320225, -0.012819723, 0.012190549, -0.006564245,
-0.058729114, 0.031535708, -0.035155279, 0.048758876, 0.039227559, 0.020620087,
-0.004521733, -0.012152786)
meta_obj <- metagen(or, std.error, sm = "OR", data = df1,
studlab = study, comb.fixed = TRUE, comb.random = FALSE, byvar = Exposure)
# FOREST ------------------------------------------------------------------
forest(meta_obj, smlab = "Text here", spacing = 1.5,
xlab = "Odds ratio",
xlim = c(0.5, 2),
leftcols = c("studlab", "N"),
col.diamond = "red", col.by = "black",
overall.hetstat = FALSE,
print.I2 = TRUE, print.tau2 = FALSE,
subgroup = T, overall = F,
text.addline1 = "HOW DO I MAKE THIS BOLD?",
ref = 1)
See argument 'ff.addline':
forest(meta_obj, smlab = "Text here", spacing = 1.5,
xlab = "Odds ratio",
xlim = c(0.5, 2),
leftcols = c("studlab", "N"),
col.diamond = "red", col.by = "black",
overall.hetstat = FALSE,
print.I2 = TRUE, print.tau2 = FALSE,
subgroup = T, overall = F,
ref = 1,
text.addline1 = "USE ARGUMENT 'ff.addline = \"bold\"'",
ff.addline = "bold",
fs.addline = 12,
colgap.left = "2.8cm")
I used mschart to generate a chart in R. However:
(1) I can't find how to change the legend text. Now the legend text is "a" and "b". I hope to change to other string, such as "legend text 1" and "legend text 2".
(2) If I change "a" and "b" to chinese characters, such "中国" and "北京" I got an error. The new word file can't be opened. If I changed back to English characters, it works again!
library(officer)
library(magrittr)
library(mschart)
doc <- read_docx()
newdata <- data.frame(
Name = c("a","a","a","a","b","b","b","b"),
wave_id = c(
"2017Q1", "2017Q2", "2017Q3", "2017Q4", "2017Q1", "2017Q2", "2017Q3", "2017Q4"
),
pct = c(0.68,0.71,0.70,0.72,0.57,0.57,0.57,0.58)
)
my_barchart <- ms_barchart(data = newdata, x = "wave_id", y = "pct", group = "Name")
my_barchart <- chart_settings(
x = my_barchart, dir="horizontal", grouping = "stacked", overlap = 100
)
my_barchart <- chart_labels(
my_barchart, title = NULL, xlab = NULL, ylab = NULL
)
my_barchart <- chart_data_labels(
my_barchart, position = "ctr", show_val = TRUE,
separator = ",", show_cat_name = FALSE
)
fp_text_settings <- list(a = fp_text(font.size = 7), b = fp_text(font.size = 7))
my_barchart <- chart_labels_text(my_barchart, fp_text_settings)
mytheme <- mschart_theme(
axis_title_x = fp_text(color = "red", font.size = 24, bold = TRUE),
axis_title_y = fp_text(color = "green", font.size = 12, italic = TRUE),
grid_major_line_x = fp_border(width = 0, color = "orange"),
axis_ticks_y = fp_border(width = 1, color = "orange"))
my_barchart <- set_theme(my_barchart, mytheme)
doc %>% body_add_chart(my_barchart)
print(doc, target = "new.docx")
Hi I'm using the ComplexHeatmap package and followed their vignette however for some reason I cannot seem to force the legend to become horizontal. So for example here is an example,
set.seed(123)
library(ComplexHeatmap)
mat = matrix(rnorm(80, 2), 8, 10)
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
rownames(mat) = paste0("R", 1:12)
colnames(mat) = paste0("C", 1:10)
ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))),
col = list(type1 = c("a" = "red", "b" = "blue")),
annotation_legend_param = list(type1 = list(
title_gp = gpar(fontsize = 16),
legend_direction = "horizontal", labels_gp = gpar(fontsize = 8)))
)
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column)
draw(ht1, heatmap_legend_side = "right")
so despite add in legend_direction = "horizontal" I still keep getting this here,
If you need to plot the heatmap legend horizontally and at the bottom of the heatmap, you can use this solution:
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column,
heatmap_legend_param = list(
legend_direction = "horizontal",
legend_width = unit(5, "cm")
)
)
draw(ht1, heatmap_legend_side = "bottom")
Otherwise, if you need to draw your (discrete) annotation legend horizontally and at the top of the heatmap, you can use nrow=1 in annotation_legend_param:
ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))),
col = list(type1 = c("a" = "red", "b" = "blue")),
annotation_legend_param = list(
type1 = list(
title_gp = gpar(fontsize = 16),
labels_gp = gpar(fontsize = 8),
nrow=1)))
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column)
draw(ht1, annotation_legend_side = "top")
I have the following data frame:
library(pheatmap)
library(RColorBrewer)
dat <- structure(c(36.28516213, -9.184523335, 5.716034994, 32.82180488,
-18.5467127, 22.79930301, -24.79075287, -49.23768715), .Dim = c(2L,
4L), .Dimnames = list(c("motif_a", "motif_b"), c("cow", "dog",
"snake", "cat")))
dat
#> cow dog snake cat
#> motif_a 36.285162 5.716035 -18.54671 -24.79075
#> motif_b -9.184523 32.821805 22.79930 -49.23769
And with the following heatmap code:
p <- pheatmap(
dat,
color = colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(1000),
clustering_method = "ward.D2",
border_color = "grey60",
clustering_distance_columns = "correlation",
clustering_distance_rows = "correlation",
fontsize_row = 6,
cluster_rows = TRUE,
cluster_cols = TRUE,
scale = "row"
)
I can get this plot.
As noted in the plot how can I get corresponding value for
every point in the heatmap to the right-hand color scale.
For example, cow motif_b has value -9.184523, but the right-hand
color scale gives a value between 0 to -0.5. How can I get these values?
The final result is a data frame with the values transformed.
How can I do that?
You asked pheatmap to plot the Pearson correlation, which is between -1 and 1, when you specified clustering_distance_xxx = "correlation". Run ?pheatmap for more details.
If you remove those parts from the code, then you'll get what you expected
library(pheatmap)
library(RColorBrewer)
dat <- structure(c(36.28516213, -9.184523335, 5.716034994, 32.82180488,
-18.5467127, 22.79930301, -24.79075287, -49.23768715), .Dim = c(2L,
4L), .Dimnames = list(c("motif_a", "motif_b"), c("cow", "dog",
"snake", "cat")))
myCol <- colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(8)
p <- pheatmap(
dat,
color = myCol,
clustering_method = "ward.D2",
border_color = "grey60",
clustering_distance_columns = "correlation",
clustering_distance_rows = "correlation",
fontsize_row = 6,
cluster_rows = TRUE,
cluster_cols = TRUE,
scale = "row"
)
p1 <- pheatmap(
dat,
color = myCol,
clustering_method = "ward.D2",
border_color = "grey60"
)
Created on 2018-03-27 by the reprex package (v0.2.0).