Skip to content

Commit

Permalink
Moved recent changes to fix-structure-plot branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarbo committed Jan 29, 2025
1 parent 4f11485 commit 95228b2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 72 deletions.
6 changes: 2 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Encoding: UTF-8
Type: Package
Package: fastTopics
Version: 0.7-4
Date: 2025-01-29
Version: 0.7-03
Date: 2024-01-29
Title: Fast Algorithms for Fitting Topic Models and Non-Negative
Matrix Factorizations to Count Data
Authors@R: c(person("Peter","Carbonetto",role=c("aut","cre"),
Expand Down Expand Up @@ -47,9 +47,7 @@ Imports:
gtools,
quadprog,
irlba,
rlang,
dplyr,
tidyr,
Rtsne,
uwot,
ashr,
Expand Down
12 changes: 1 addition & 11 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,16 @@ importFrom(Rtsne,Rtsne)
importFrom(ashr,ash)
importFrom(cowplot,plot_grid)
importFrom(cowplot,theme_cowplot)
importFrom(dplyr,arrange)
importFrom(dplyr,coalesce)
importFrom(dplyr,group_by)
importFrom(dplyr,join_by)
importFrom(dplyr,lag)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(dplyr,ungroup)
importFrom(ggplot2,aes_q)
importFrom(ggplot2,aes_string)
importFrom(ggplot2,after_stat)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_boxplot)
importFrom(ggplot2,geom_col)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_tile)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_color_manual)
Expand Down Expand Up @@ -118,7 +110,6 @@ importFrom(plotly,layout)
importFrom(plotly,plot_ly)
importFrom(progress,progress_bar)
importFrom(quadprog,solve.QP)
importFrom(rlang,.data)
importFrom(stats,dpois)
importFrom(stats,formula)
importFrom(stats,glm)
Expand All @@ -134,7 +125,6 @@ importFrom(stats,rnorm)
importFrom(stats,rpois)
importFrom(stats,runif)
importFrom(stats,summary.glm)
importFrom(tidyr,crossing)
importFrom(utils,combn)
importFrom(utils,modifyList)
importFrom(utils,read.table)
Expand Down
2 changes: 1 addition & 1 deletion R/fit_poisson_nmf.R
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ fit_poisson_nmf <- function (X, k, fit0, numiter = 100,
cat(sprintf("Running at most %d %s updates, %s extrapolation ",
numiter,method.text,
ifelse(control$extrapolate,"with","without")))
cat("(fastTopics 0.7-4).\n")
cat("(fastTopics 0.7-3).\n")
}

# INITIALIZE ESTIMATES
Expand Down
64 changes: 19 additions & 45 deletions R/structure_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@
#' \code{structure_plot_ggplot_call} with your own function to
#' customize the appearance of the plot.
#'
#' @param res The resolution of the tiling along the y-axis
#' (in units of topic probabilities).
#'
#' @param \dots Additional arguments passed to \code{structure_plot}
#' (for the \code{plot} method) or \code{embed_method} (for
#' function \code{structure_plot}).
Expand Down Expand Up @@ -170,10 +167,9 @@
#'
structure_plot <-
function (fit, topics, grouping, loadings_order = "embed", n = 2000,
colors, gap = 1,
colors, gap = 1,
embed_method = structure_plot_default_embed_method,
ggplot_call = structure_plot_ggplot_call, res = 0.01,
...) {
ggplot_call = structure_plot_ggplot_call, ...) {

# Check and process input argument "fit".
if (is.matrix(fit)) {
Expand Down Expand Up @@ -270,7 +266,7 @@ structure_plot <-
return(ggplot_call(dat,colors))
} else {
out <- compile_grouped_structure_plot_data(fit$L,topics,grouping,gap)
return(ggplot_call(out$dat,colors,out$ticks,res = res))
return(ggplot_call(out$dat,colors,out$ticks))
}
}

Expand Down Expand Up @@ -332,21 +328,12 @@ plot.multinom_topic_model_fit <- function (x, ...)
#' \code{ticks = NULL}.
#'
#' @param font.size Font size used in plot.
#'
#' @importFrom rlang .data
#' @importFrom dplyr group_by
#' @importFrom dplyr ungroup
#' @importFrom dplyr arrange
#' @importFrom dplyr mutate
#' @importFrom dplyr coalesce
#' @importFrom dplyr lag
#' @importFrom dplyr left_join
#' @importFrom dplyr join_by
#' @importFrom tidyr crossing
#'
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 aes_string
#' @importFrom ggplot2 geom_tile
#' @importFrom ggplot2 geom_col
#' @importFrom ggplot2 scale_x_continuous
#' @importFrom ggplot2 scale_color_manual
#' @importFrom ggplot2 scale_fill_manual
#' @importFrom ggplot2 labs
#' @importFrom ggplot2 theme
Expand All @@ -357,32 +344,19 @@ plot.multinom_topic_model_fit <- function (x, ...)
#' @export
#'
structure_plot_ggplot_call <- function (dat, colors, ticks = NULL,
font.size = 9, res = 0.01) {
dat <- group_by(dat,.data$sample)
dat <- arrange(dat,.data$topic)
dat <- mutate(dat,"top" = cumsum(.data$prop))
dat <- mutate(dat,"bot" = coalesce(lag(.data$top),0))
dat <- ungroup(dat)
dat <- dat[,c("sample","topic","top","bot")]
upper <- max(dat$top)
res <- res/upper
rasterdat <- dat["sample"]
rasterdat <- crossing(rasterdat,
data.frame("y" = seq(0 + res/2,upper - res/2,
by = res)))
rasterdat <- left_join(rasterdat,dat,
by = join_by("sample",x$y > y$bot,x$y < y$top))
return(ggplot(rasterdat,aes_string(x = "sample",y = "y",fill = "topic")) +
geom_tile() +
scale_fill_manual(values = colors) +
scale_x_continuous(limits = c(0,max(dat$sample) + 1),breaks = ticks,
labels = names(ticks)) +
labs(x = "",y = "topic proportion") +
theme_cowplot(font.size) +
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 45,hjust = 1)))
}
font.size = 9)
ggplot(dat,aes_string(x = "sample",y = "prop",color = "topic",
fill = "topic")) +
geom_col() +
scale_x_continuous(limits = c(0,max(dat$sample) + 1),breaks = ticks,
labels = names(ticks)) +
scale_color_manual(values = colors) +
scale_fill_manual(values = colors) +
labs(x = "",y = "topic proportion") +
theme_cowplot(font.size) +
theme(axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 45,hjust = 1))

# This is used by structure_plot to create a data frame suitable for
# plotting with 'ggplot'. Input argument L is the topic proportions
Expand Down
12 changes: 1 addition & 11 deletions man/structure_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 95228b2

Please sign in to comment.