CellChat
CellChat copied to clipboard
rankNet: Wrong text color on axis.text when using parameter signaling or pairLR
- The same error reported in #133
When using parameter signaling or pairLR in rankNet, the color of text on the axis is different from the original one.
eg. runing command 1:
rankNet(object, mode = "comparison", measure = "weight",comparison = c(1,2), stacked = T, do.stat = TRUE)
result:
runing command 2:
rankNet(object, mode = "comparison", measure = "weight", signaling = c("NOTCH","VWF"),comparison = c(1,2), stacked = T, do.stat = TRUE)
result:
Solution:
- Fix the rankNet function, put the colors.text part after the subset part: @sqjin
colors.text part:
https://github.com/jinworks/CellChat/blob/346fb615aaefa3c1a2830ec2fe3a933c5f723c83/R/analysis.R#L1301-L1310
subset part:
https://github.com/jinworks/CellChat/blob/346fb615aaefa3c1a2830ec2fe3a933c5f723c83/R/analysis.R#L1319-L1328
Final codes:
modify_rankNet <- function(object, ......) {
......
for (i in 1:length(pair.name.all)) {
df.t <- df[df$name == pair.name.all[i], "contribution"]
if (sum(df.t) == 0) {
df <- df[-which(df$name == pair.name.all[i]), ]
}
}
if ((slot.name == "netP") && (!is.null(signaling))) {
df <- subset(df, name %in% signaling)
} else if ((slot.name == "netP") &&(!is.null(pairLR))) {
stop("You need to set `slot.name == 'net'` if showing specific L-R pairs ")
}
if ((slot.name == "net") && (!is.null(pairLR))) {
df <- subset(df, name %in% pairLR)
} else if ((slot.name == "net") && (!is.null(signaling))) {
stop("You need to set `slot.name == 'netP'` if showing specific signaling pathways ")
}
if (length(comparison) == 2) {
if (do.stat) {
colors.text <- ifelse((df$contribution.relative < 1-tol) & (df$pvalues < cutoff.pvalue), color.use[2], ifelse((df$contribution.relative > 1+tol) & df$pvalues < cutoff.pvalue, color.use[1], "black"))
} else {
colors.text <- ifelse(df$contribution.relative < 1-tol, color.use[2], ifelse(df$contribution.relative > 1+tol, color.use[1], "black"))
}
} else {
message("The text on the y-axis will not be colored for the number of compared datasets larger than 3!")
colors.text = NULL
}
if (stacked) {
gg <- ggplot(df, aes(x=name, y=contribution, fill = group)) + geom_bar(stat="identity",width = bar.w, position ="fill") # +
# xlab("") + ylab("Relative information flow") #+ theme(axis.text.x = element_blank(),axis.ticks.x = element_blank())
# scale_y_discrete(breaks=c("0","0.5","1")) +
if (measure == "weight") {
gg <- gg + xlab("") + ylab("Relative information flow")
} else if (measure == "count") {
gg <- gg + xlab("") + ylab("Relative number of interactions")
}
......
}
For users' temporary solution, copy the code of the rankNet function and fix it by assigning a modify_rankNet function
runing command 3:
modify_rankNet(object, mode = "comparison", measure = "weight", signaling = c("NOTCH","VWF"),comparison = c(1,2), stacked = T, do.stat = TRUE)
result: