-
Notifications
You must be signed in to change notification settings - Fork 46
Description
Suggestion for a new function which would essentially be an extension of vis_compare but has colors to specify what the 'not same' change is. Admittedly this is limited use case as too many variables and it would be super messy. The main thing purpose I was thinking of is tracking changes in genetics technical replicates where the possible values are 0, 1, 2, NA and you want to keep track of what these values change to between replicates.
Halfway through writing this I thought I may as well have a crack, this works although the 'new bit' could be a better, I'm primarily a base kid so that is what I did it in rather than mutate. Also I didn't use your colors and went with viridis. Finally the only thing I didnt do out of lazyness was NA's being used in from/to scenarios should be "NA" so that ggplot doesn't remove them and they get a color.
You could also not implement this if you think it is weird and that would be super fine.
vis_compare_new <- function(df1,
df2, type="same"){
# throw error if df1 not data.frame
visdat:::test_if_dataframe(df1)
# throw error if df2 not data.frame
visdat:::test_if_dataframe(df2)
if (!identical(dim(df1), dim(df2))) {
stop("vis_compare requires identical dimensions of df1 and df2")
}
v_identical <- Vectorize(identical)
df_diff <- purrr::map2_df(df1,df2, v_identical)
head(df_diff)
d <- df_diff %>% as.data.frame() %>% purrr::map_df(visdat:::compare_print) %>%
visdat:::vis_gather_() %>% dplyr::mutate(value_df1 = visdat:::vis_extract_value_(df1),
value_df2 = visdat:::vis_extract_value_(df2))
#The new bit
if (type!="same"){
cols<-c('value_df1','value_df2' )
d$fctr <- apply( d[ , cols ] , 1 , paste , collapse = "-" )
d$fctr[d$valueType=="same"]<-"same"
d$value_df1<-as.character(d$value_df1)
d$value_df2<-as.character(d$value_df2)
d[,cols][d$valueType=="same",]<-"same"
}
fillType<-dplyr::case_when(
type == "same"~"valueType",
type == "from"~"value_df1",
type == "to"~ "value_df2",
type == "both"~"fctr")
ggplot2::ggplot(data = d, ggplot2::aes_string(x = "variable", y = "rows")) +
ggplot2::geom_raster(ggplot2::aes_string(fill = fillType)) +
ggplot2::theme_minimal() +
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, vjust = 1, hjust = 1)) +
ggplot2::labs(x = "", y = "Observations", fill = "Cell Type") +
# ggplot2::scale_fill_manual(limits = c("same", "different"), breaks = c("same", "different"), values = c("#fc8d59",
# "#91bfdb"), na.value = "grey") +
ggplot2::scale_y_reverse() +
ggplot2::theme(axis.text.x = ggplot2::element_text(hjust = 0.25)) +
ggplot2::scale_x_discrete(position = "top", limits = names(df_diff))+viridis::scale_fill_viridis(discrete = TRUE)
}
vis_compare_new(df1, df2, type = "both")