Home > Net >  Plot single block of color in R
Plot single block of color in R

Time:05-03

I am trying to figure out how to plot a single block of color in R. I am trying to visualize a region of a genome with color. I am starting with a matrix that has 1 row and 6049 columns.

l1_canon <- matrix( nrow = 1, ncol = 6049, data = "_" )

Next, I have blocks that differentiate major regions of this element:

l1_canon[,1:909] <- "5' UTR"
l1_canon[,910:1923] <- "ORF1"
l1_canon[,1990:5814] <- "ORF2"
l1_canon[,49:420] <- "CPG"
l1_canon[,5815:6049] <- "3' UTR"
l1_canon[,211:225] <- "RXRA::VDR"

I have assigned colors to the different categories:

l1_colors <- list()
l1_colors[["5' UTR"]] <- "#26A064" # "#ea0064"
l1_colors[["ORF1"]] <- "#3095C7" # "#008a3f"
l1_colors[["ORF2"]] <- "#CA6BAA" # "#116eff"
l1_colors[["CPG"]] <- "#B38241" # "#cf00dc"
l1_colors[["3' UTR"]] <- "#CCCCCC" # "#dddddd"
l1_colors[["RXRA::VDR"]] <- "#FFFFFF"
l1_colors[["_"]] <- "#000000"

But I can't figure out how to plot this. I am looking for something like the enter image description here

I tried assigning colors like so

for ( i in l1_canon ){
  l1_color <- l1_colors[ l1_canon ]
}

and using it in the code that was used to generate the color ramp plots, but I am getting errors. I am aware that having 6000 columns is going to make this weird visually, but, it's what I need! I am hoping I can make the individual color blocks small enough to fit on a screen. Eventually, this bar is going to be annotation above another image.

TY for your help! :)

CodePudding user response:

I don't fully understand what you want, but you could use ggplot2 as follows:

# Find the run lengths of the regions
rle1 = rle(l1_canon[1,])

# Turn the run lengths into a data frame
df=data.frame(lengths=rle1$lengths, V=rle1$values)

# Align the colours with the regions
df$color <- unlist(l1_colors)[df2$V]

# Plot a single stacked bar on its side with no annotation
ggplot(df, aes(x=1,group=seq_along(V),label=V, fill=color,y=lengths))   
  geom_bar(stat="identity",color="black") 
  scale_fill_identity()   
  theme_void()   
  coord_flip()  
  scale_y_reverse()

enter image description here

  • Related