I am trying to plot a bunch of rectangles with the fill being color gradients determined by a proportion.
To start with, I have a Data Frame like so:
sampleID 1 2 3 4 5 ... 100
sample1 1 1 1 1 1 ... 1
sample2 1 1 1 1 1 ... 1
sample3 2 2 2 2 2 ... 2
sample4 2 2 1 1 2 ... 2
...
where the integers correspond to group assignments for 100 runs of an analysis I did. I want samples with mixed group assignments to have a color gradient (e.g., sample4 is 25% blue and 75% red).
Here's my code:
library("RColorBrewer")
library("plotrix")
# Make sampleID column rownames and remove from df
col2rownames <- function(df){
rownames(df) <- df$sampleID
df$sampleID <- NULL
return(df)
}
df <- col2rownames(df)
# Get a list of frequency tables corresponding to each row in df.
df.freq <- apply(df, 1, table)
# Convert list of table() objects to list of data.frames
df.freq <- lapply(df.freq, function(x) { as.data.frame(x, stringsAsFactors = F) } )
# Make color vector
colors <- c(
"1" = "#808080", # BXCH
"2" = "purple4", # BXON
"3" = "yellow3", # BXFL
"4" = "orange1", # BXEA
"5" = "mediumaquamarine", # BXFL second cluster
"6" = "magenta3", # GUFL
"7" = "blue", # GUMS
"8" = "red", # BXMX
"9" = "green2", # BXTT
"10" = "#00ffff" # BXDS
)
# Subset only colors present in each df.freq data.frame
collist <- list()
collist <- lapply(df.freq, function(x) {
colors[x[, 1]]
})
# Convert to list of vectors
collist <- lapply(collist, as.vector)
# Get number of data frames in list
mylen <- length(df.freq)
# Plot an empty box
plot(1:mylen, type="n", axes=F)
# Initialize counters
counter_min <- 0
counter_max <- 1
# Initialize newcollist
newcollist <- list()
# Plot rectangles with color gradient
for (i in 1:length(collist)){
colsubset <- c(collist[[i]])
newcollist[[i]] <- colsubset
gradient.rect(xleft = 0, ybottom = counter_min, xright = 5, ytop = counter_max, col = colsubset, gradient = "x")
counter_min <- counter_max
counter_max <- counter_min + 1
}
And here's my current output:
However, the rectangles with >1 color are shown as 50/50, which is not the correct proportions. For example, the purple and light blue near the top are actually supposed to be 88% and 12%, respectively.
I am stuck here. Does anyone know a way to plot the color fills by proportions?
Thanks very much for your time.