0

I have a shiny app created that plots metagenome data using ggplot2, phyloseq, and plotly with dplyr and tidyr. It creates pretty good stacked barplots and heatmaps. The only issue is that it reorders sample names on the x-axis, for example. 1–10 are arranged as 1, 10, 2, 5, 6... How do I correct that bug?

I have tried forcats and reorder but nothing is happening. I believe they are required to be converted to factors, but I'm not sure and i dont know how to do it.

source code is:

plot_barplot <- function(
genera,
samples,
cutoff,
modus
){
if (length(samples) == 0){
messagetext <- list(
x = 1,
y = 1,
text = "No sample selected \nplease select a sample"
font = list(size = 28)
)
 plot <- plot_ly(as.data.frame(NULL))
 plot <- plot %>% layout(annotations = messagetext,
                         yaxis = list(visible = FALSE),
                         xaxis = list(visible = FALSE))
 } else {
 plotdata <- dplyr::select(genera$Abundance, all_of(samples))
  if (ncol(plotdata) > 1) {
  plotdata$Total <- rowSums(plotdata)
  circ = F
 } else if (ncol(plotdata) == 1) {
  plotdata$copy <- plotdata[1]
  plotdata$Total <- rowSums(plotdata) / 2
  circ = T
 }
 plotdata <- plotdata[order(plotdata$Total,
                           decreasing = T),]
 plotdata <- rbind(plotdata[1:cutoff,],
                  colSums(plotdata[(cutoff + 1):nrow(plotdata),]))
 row.names(plotdata)[nrow(plotdata)] <- "#others"
 if (circ == T | modus == "total"){
  plotdata <- dplyr::select(plotdata, Total)
  plotdata$Name <- row.names(plotdata)
  plot <- plotly::plot_ly(plotdata,
                          labels = ~Name,
                          values = ~Total,
                          type = "pie")
 } else {
  plotdata <- dplyr::select(plotdata, -Total)
  if (modus == "hide_others"){
    plotdata <- dplyr::slice_head(plotdata, 
    n = (nrow(plotdata)    -1))  
  }
  datapoints <- colnames(plotdata)
  plotdata$Name <- row.names(plotdata)
  plotdata <- tidyr::pivot_longer(plotdata,
                                  datapoints,
                                  names_to = "Sample",
                                  values_to = "Abundance")
  if (modus %in% c("absolute", "hide_others")){
    plot <- ggplot2::ggplot(plotdata,
                            ggplot2::aes(fill = Name,
                                         y = Abundance,
                                         x = sample))
    plot <- plot + ggplot2::geom_bar(position = "stack",
                                     stat = "identity")
    plot <- plot + ggplot2::theme_classic()
    plot <- plotly::ggplotly(plot)
  } else {
    plot <- ggplot2::ggplot(plotdata,
                            ggplot2::aes(fill = Name,
                                         y = Abundance,
                                         x = Sample))
    plot <- plot + ggplot2::geom_bar(position = "fill",
                                     stat = "identity")
    plot <- plot + ggplot2::theme_classic()
    plot <- plotly::ggplotly(plot)
    }
   }
  }


    return(plot)
   }
Axeman
  • 32,068
  • 8
  • 81
  • 94
Ahmedtrio
  • 1
  • 1

0 Answers0