Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions R/gheatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ gheatmap <- function(p, data, offset=0, width=1, low="green", high="red", color=
dd$variable <- factor(dd$variable, levels=colnames_level)
}
V2 <- start + as.numeric(dd$variable) * width
mapping <- data.frame(from=dd$variable, to=V2)
mapping <- unique(mapping)
data_axis <- data.frame(from=dd$variable, to=V2)
data_axis <- unique(data_axis)

dd$x <- V2
dd$width <- width
Expand All @@ -127,25 +127,25 @@ gheatmap <- function(p, data, offset=0, width=1, low="green", high="red", color=
} else {
y <- max(p$data$y) + 1
}
mapping$y <- y
mapping[[".panel"]] <- factor("Tree")
data_axis$y <- y
data_axis[[".panel"]] <- factor("Tree")
# if custom column annotations are provided
if (!is.null(custom_column_labels)) {
# assess the type of input for the custom column annotation
# either a vector or a named vector with positions for specific names
if (is.null(names(custom_column_labels))) {
if (length(custom_column_labels) > nrow(mapping)) {
if (length(custom_column_labels) > nrow(data_axis)) {
warning(paste("Input column label vector has more elements than there are columns.",
"\n", "Using the first ", nrow(mapping)," elements as labels", sep=""))
mapping[["custom_labels"]] <- as.character(custom_column_labels[1:nrow(mapping)])
} else if (length(custom_column_labels) < nrow(mapping)) {
"\n", "Using the first ", nrow(data_axis)," elements as labels", sep=""))
data_axis[["custom_labels"]] <- as.character(custom_column_labels[1:nrow(data_axis)])
} else if (length(custom_column_labels) < nrow(data_axis)) {
warning(paste("Input column label vector has fewer elements than there are columns.",
"\n", "Using all available labels, n = ",
length(custom_column_labels), sep=""))
mapping[["custom_labels"]] <- as.character(c(custom_column_labels,
rep("", nrow(mapping) - length(custom_column_labels))))
data_axis[["custom_labels"]] <- as.character(c(custom_column_labels,
rep("", nrow(data_axis) - length(custom_column_labels))))
} else {
mapping[["custom_labels"]] <- custom_column_labels
data_axis[["custom_labels"]] <- custom_column_labels
}
} else {
if (!is.null(colnames_level)) {
Expand All @@ -154,18 +154,18 @@ gheatmap <- function(p, data, offset=0, width=1, low="green", high="red", color=
vector_order <- colnames_level

} else {
vector_order <- as.character(mapping$from)
vector_order <- as.character(data_axis$from)
}
for (elem in custom_column_labels) {
vector_order[which(vector_order == elem)] = names(which(custom_column_labels == elem))
}
mapping[["custom_labels"]] <- vector_order
data_axis[["custom_labels"]] <- vector_order
}
p2 <- p2 + geom_text(data=mapping, aes(x=to, y = y, label=custom_labels),
p2 <- p2 + geom_text(data=data_axis, aes(x=to, y = y, label=custom_labels),
size=font.size, family=family, inherit.aes = FALSE, angle=colnames_angle,
nudge_x=colnames_offset_x, nudge_y = colnames_offset_y, hjust=hjust)
} else {
p2 <- p2 + geom_text(data=mapping, aes(x=to, y = y, label=from), size=font.size, family=family,
p2 <- p2 + geom_text(data=data_axis, aes(x=to, y = y, label=from), size=font.size, family=family,
inherit.aes = FALSE, angle=colnames_angle,
nudge_x=colnames_offset_x, nudge_y = colnames_offset_y, hjust=hjust)
}
Expand All @@ -178,7 +178,7 @@ gheatmap <- function(p, data, offset=0, width=1, low="green", high="red", color=
p2 <- p2 + scale_y_continuous(expand = c(0,0))
}

attr(p2, "mapping") <- mapping
attr(p2, "data_axis") <- data_axis
return(p2)
}