Complicated Stacked-barplot in R using color column
NickName:shadowofzedark Ask DateTime:2015-06-11T08:14:00

Complicated Stacked-barplot in R using color column

I am trying to make a stacked barplot using R. The main sticking point is using the colors from the color column in the plot appropriately.

Requirements of the plot:

  • Each bar(x axis) should represent a time.
  • Each species should be its appropriate color (given by the color column) with its space on the barplot reflecting abundance(y axis).
  • Within each bar, the species in the same phyla should be grouped together.
  • Setting the width of the bars would be really cool, but not necessary.

Characteristics of the dataset:

  • Each species has an individual color and the colors of the species are gradiented by their phyla.
  • The abundances of species within a time sum to 100.
  • Not every species is in every time
  • There are 7 times, 8 phyla, 132 species

Other ideas on how to represent these data are welcome.

Representative data:

phyla           species                         abundance    color    time
Actinobacteria  Bifidobacterium_adolescentis    18.73529    #F7FBFF   D30
Firmicutes      Faecalibacterium_prausnitzii    14.118      #F7FCF5   D30
Firmicutes      Catenibacterium_mitsuokai       12.51944    #F3F9F2   D30
Bacteroidetes   Bacteroides_ovatus              7.52241     #FFF5EB   D30
Firmicutes      Faecalibacterium_prausnitzii    21.11866    #F7FCF5   D7
Firmicutes      Ruminococcus_sp_5_1_39BFAA      13.54397    #92B09C   D7
Actinobacteria  Bifidobacterium_adolescentis    10.21989    #F7FBFF   D7
Actinobacteria  Bifidobacterium_adolescentis    38.17028    #F7FBFF   D90
Firmicutes      Catenibacterium_mitsuokai       11.04982    #F3F9F2   D90
Firmicutes      Faecalibacterium_prausnitzii    9.82507     #F7FCF5   D90
Actinobacteria  Collinsella_aerofaciens         5.2334      #D4DEE9   D90

Thank you in advance; I am banging my head against the wall with this.

Code thanks to Robert.

#reshape the dataframes as matrices
#species are row names and times are columns (abundance data makes up matrix)
#put the matrix times in the correct order
#create stacked barplot that has the width of column reflecting shannon index
#save the stacked barplots in files named by the entry list
for(i in 1:n){
  phyl=aggregate(abundance ~ phyla+species+color+time, dfs[[i]], sum)
  phyl=phyl[with(phyl,order(phyla,species,time)),]
  wide <- reshape(phyl, idvar = c("phyla","species","color"),
                  timevar = "time", direction = "wide")
  wide[is.na(wide)]<-0
  wide

  res1=as.matrix(wide[,-c(1:3)],ncol=dim(wide[,-c(1:3)])[2])
   colnames(res1)=
    unlist(strsplit(colnames(res1), ".", fixed = TRUE)) [seq(2,length(colnames(res1))*2,by=2)]
  rownames(res1)=wide$species
  res1 <- res1[,c('E','FMT','PA','PF','D7','D30','D90')]

  bar.width <- as.matrix(div.dfs[[i]]['frac'])

   mypath <- file.path(output.path,paste(project.name, "_", lhs[i], ".tiff", sep = ""))
  tiff(file=mypath)
  mytitle = paste(project.name, lhs[i])
  barplot(res1,col=wide$color,beside = F, width = c(bar.width), main = mytitle, legend.text=F,args.legend=
            list(x = "top",bty="n",cex=.6,ncol=2))
  dev.off()

  rm(res1)
}

#makes the legend and exports as a eps file
setwd(output.path)
plot_colors <- database$color
text <- database$species
SetEPS()
postscript('legend.eps')
plot.new()
par(xpd=TRUE)
legend("center",legend = text, text.width = max(sapply(text, strwidth)),
       col=plot_colors, lwd=1, cex=.2, horiz = F, ncol=2, bty='n')
par(xpd=FALSE)
dev.off()

Copyright Notice:Content Author:「shadowofzedark」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30769481/complicated-stacked-barplot-in-r-using-color-column

More about “Complicated Stacked-barplot in R using color column” related questions

Complicated Stacked-barplot in R using color column

I am trying to make a stacked barplot using R. The main sticking point is using the colors from the color column in the plot appropriately. Requirements of the plot: Each bar(x axis) should repre...

Show Detail

R igraph: Color Vertices by column content

I am using igraph to plot a graph from SQL Server. I am providing as input a 3 column table: from to color Node1 NodeA red Node1 NodeB green Node1 NodeC blue Node2 NodeD red Node2 N...

Show Detail

How to paint r x r field with n different colors without using same color at same column, row

Recently, I encountered this problem and I can't solve it because of time over. 'Make a program that answers how many ways to paint r by r field with n different colors, without using the same col...

Show Detail

Change the color of a row based on other column using DT - Shiny - R

I have a data.table with 3 columns. trial &lt;- matrix(c(3,4,1,2,1,2,4,2,5), ncol=3) colnames(trial) &lt;- c('value', 'min', 'max') trial.table &lt;- data.table(trial) Using R (Shiny and

Show Detail

From hex color code or RGB to color name using R

I'm trying to caluclate the pecentage of each color given a picture. At this step I have this output: Color Count red green blue 861 ED1B24 16774 237 27 36 1 000000 11600 0 0...

Show Detail

R shiny : Color plot using ggplot

I am creating a plot in R shiny app based on usr input. My filtering of data is a bit complicated and I am unsure fow to pass it to &quot;fill&quot; in ggplot. Below is my code: data &lt;- reactive...

Show Detail

R XLConnect - filtering columns based on column color

In the XLconnect package (or any other package in R), is it possible to read an Excel sheet with colors in their headers and filtering them in R based on those colors? For example is column heade...

Show Detail

How can I plot this complicated integral in R?

I am currently struggling with a complicated function which I want to draw in R. Basically it is a combination of distribution and other dependent probability, so I have to modify a normal distribu...

Show Detail

How to color bar plot with bars dependent on column names in R

I have two data frames and a function maxthree which takes in a data frame and row name as an argument and plots the three highest value on the row (in a descending order) and the name of the colum...

Show Detail

Dealing with Large Legends in R Plots - Complicated Heatmap Example

I'm working on a really complicated heatmap figure in R. heatmap.2 from the gplots package is not enough for me, because I want multiple sidebar annotations, such as the heatmap.3 function permits:

Show Detail