Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Depends:
Imports:
ggplot2 (>= 1.0.0),
dplyr (>= 0.4.3),
lazyeval (>= 0.1.10),
tibble
rlang
URL: https://github.com/tinyheero/cofeatureR
BugReports: https://github.com/tinyheero/cofeatureR/issues
License: GPL-3
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(plot_cofeature_mat)
importFrom(rlang,.data)
importFrom(stats,setNames)
86 changes: 33 additions & 53 deletions R/plot_cofeature_mat.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,63 +144,49 @@ plot_cofeature_mat <- function(

if (type.display.mode == "single") {
message("Using type.display.mode single")
in.df <- dplyr::distinct_(in.df)
in.df <- dplyr::distinct(in.df)

mutate.call <- lazyeval::interp(~ factor(type, levels = rev(type.order)),
type = as.name("type"),
type.order = as.name("type.order"))
in.df <- dplyr::mutate(in.df,
type = factor(.data$type, levels = rev(type.order)))

in.df <- dplyr::mutate_(in.df,
.dots = setNames(list(mutate.call), "type"))

in.df <- dplyr::group_by_(in.df, .dots = c("sampleID", "feature"))
in.df <- dplyr::arrange_(in.df, .dots = c("type"))
in.df <- dplyr::group_by(in.df, .data$sampleID, .data$feature)
in.df <- dplyr::arrange(in.df, .data$type)
in.df <- dplyr::top_n(in.df, n = 1)
in.df <- dplyr::ungroup(in.df)
}

message("Setting feature order")
mutate.call <- lazyeval::interp(~ as.numeric(
factor(feature,
levels = feature.order)),
feature = as.name("feature"))
in.df <- dplyr::mutate_(in.df,
.dots = setNames(list(mutate.call), "feature"))
in.df <- dplyr::mutate(in.df,
feature = as.numeric(factor(.data$feature,
levels = feature.order)))

# Set sample order
message("Setting sample order")
mutate.call <- lazyeval::interp(~ factor(sampleID,
levels = sample.id.order),
sampleID = as.name("sampleID"))
in.df <- dplyr::mutate_(in.df,
.dots = setNames(list(mutate.call), "sampleID"))
in.df <- dplyr::mutate(in.df,
sampleID = factor(.data$sampleID,
levels = sample.id.order))

# Calculate shift
in.df <- dplyr::group_by_(in.df, .dots = c("feature", "sampleID"))
mutate.call <- lazyeval::interp(~ (1:n())/n() -
1/(2 * n()) - 1/2)
in.df <- dplyr::mutate_(in.df,
.dots = setNames(list(mutate.call), "shift"))
in.df <- dplyr::group_by(in.df, .data$feature, .data$sampleID)
in.df <- dplyr::mutate(in.df,
shift = (1:dplyr::n()) / dplyr::n() -
1 / (2 * dplyr::n()) - 1/2)

# Calculate height
mutate.call <- lazyeval::interp(~ 1/n())
in.df <- dplyr::mutate_(in.df,
.dots = setNames(list(mutate.call), "height"))
in.df <- dplyr::mutate(in.df,
height = 1 / dplyr::n())

# Calculate feature_shift
mutate.call <- lazyeval::interp(~ feature + shift,
feature = as.name("feature"),
shift = as.name("shift"))
in.df <- dplyr::mutate_(in.df,
.dots = setNames(list(mutate.call), "feature_shift"))

p1 <- ggplot2::ggplot(in.df,
ggplot2::aes_string(x = "sampleID",
y = "feature_shift",
height = "height",
fill = "type")) +
in.df <- dplyr::mutate(in.df,
feature_shift = .data$feature + .data$shift)

p1 <- ggplot2::ggplot(in.df,
ggplot2::aes(x = .data$sampleID,
y = .data$feature_shift,
height = .data$height,
fill = .data$type)) +
ggplot2::scale_x_discrete(drop = drop.x) +
ggplot2::scale_y_discrete(limits = 1:length(feature.order),
ggplot2::scale_y_discrete(limits = 1:length(feature.order),
labels = feature.order) +
ggplot2::ylab("Feature") +
ggplot2::xlab("Sample ID")
Expand All @@ -213,7 +199,7 @@ plot_cofeature_mat <- function(
if (missing(missing.fill.col)) {
if (tile.flag) {
p1 <- p1 +
ggplot2::geom_tile(color = tile.col, size = 1)
ggplot2::geom_tile(color = tile.col, linewidth = 1)
}
} else {
p1 <- add_tiles(p1, in.df, tile.col, missing.fill.col, tile.border.size)
Expand All @@ -234,7 +220,7 @@ plot_cofeature_mat <- function(
if (dot.flag) {
if (!missing(dot.size)) {
p1 <- p1 +
ggplot2::geom_point(ggplot2::aes_string(size = dot.size))
ggplot2::geom_point(ggplot2::aes(size = .data[[dot.size]]))
} else {
p1 <- p1 +
ggplot2::geom_point()
Expand All @@ -243,27 +229,21 @@ plot_cofeature_mat <- function(
p1
}

#' Add tiles to the ggplot2
#' Add tiles to the ggplot2
#'
#' @param p1 Existing ggplot2
#' @inheritParams plot_cofeature_mat
add_tiles <- function(p1, in.df, tile.col, missing.fill.col, tile.border.size) {

# Plot two geom_tile. 1 for data present and 1 for data missing
filter.crit.1 <- lazyeval::interp(~ !is.na(type),
.values = list(type = as.name("type")))
filter.crit.2 <- lazyeval::interp(~ is.na(type),
.values = list(type = as.name("type")))

# No borders for missing data.
p1 <-
p1 <-
p1 +
ggplot2::geom_tile(
data = dplyr::filter_(in.df, filter.crit.1),
color = tile.col, size = tile.border.size
data = dplyr::filter(in.df, !is.na(.data$type)),
color = tile.col, linewidth = tile.border.size
) +
ggplot2::geom_tile(
data = dplyr::filter_(in.df, filter.crit.2),
data = dplyr::filter(in.df, is.na(.data$type)),
fill = missing.fill.col, color = tile.col
)

Expand Down