How to specify the color of some genes
uuwuyi opened this issue · 3 comments
Hi Wilkox,
I'm now using a palette to colour the genes. But I hope to specify the colour of genes with the name 'na' and keep other genes using the colour from the palette. I can't achieve this by adding a second colour layer. Could you please tell me how can I make this using gggenes? Thank you in advance!
Hi @ugobananas – I don't quite understand what you're trying to do. Could you post a reprex of what you've done so far and explain what you want it to look like? Thanks!
Hi Wilkox,
Sorry for not posting the codes. I want to keep the genes labeled as 'n' in grey and color other genes using the palette I generated. But I can only get this figure with all the genes colored by the palette.
I tried to extract the 'n' genes into another dataframe, made an additional figure and then tried p1+p2
to merge them together. But it gave the error 'Can't add ggplot(data, aes(xmin = start, xmax = end, y = molecule, fill = gene,
to a ggplot object'. Below is my code.
library(ggplot2)
library(gggenes)
library(RColorBrewer)
dummies <- make_alignment_dummies(
data4,
aes(xmin=start, xmax=end, y=molecule, id=gene),
on = "homolog"
)
mycolors <- colorRampPalette(brewer.pal(9, "OrRd"))(46)
p1<-ggplot(data,aes(xmin=start, xmax=end, y=molecule, fill=gene, label=gene, forward=direction))+
geom_gene_arrow()+facet_wrap(~ molecule, scales="free", ncol=1)+
theme_genes()+geom_blank(data=dummies, aes(forward=1))+
scale_fill_manual(values=mycolors)+
geom_gene_label(align="left")
dummies <- make_alignment_dummies(
data41,
aes(xmin=start, xmax=end, y =molecule, id=gene))
p1+ggplot(data41,aes(xmin=start, xmax=end, y=molecule, fill=gene, label=gene, forward=direction))+
geom_gene_arrow()+facet_wrap(~ molecule, scales="free", ncol=1)+
theme_genes()+geom_blank(data=dummies, aes(forward=1))+
scale_color_manual(values=c('n'='grey'))
Could you please give me some suggestions on how to achieve this? Many thanks!
See below an example of how this can be done using the forcats
package.
library(ggplot2)
library(gggenes)
library(RColorBrewer)
library(forcats)
# Prepare a palette of 11 colours + white at the end
mycolours <- c(colorRampPalette(brewer.pal(9, "OrRd"))(11), "#FFFFFF")
# Convert the "gene" variable into a factor with "protD" at the end
example_genes$gene <- fct_relevel(example_genes$gene, "protD", after = Inf)
# Draw the plot with the custom palette
ggplot(example_genes, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
geom_gene_arrow() +
facet_wrap(~ molecule, scales = "free", ncol = 1) +
scale_fill_manual(values = mycolours) +
theme_genes()
Created on 2020-10-17 by the reprex package (v0.3.0)