Thank you very much!
I've 3 variables, for a set of genes - the log2FC, the log2(basemeanB) and the difference in expression of a gene in fraction of cells. below are my vectors -
df <- data.frame(Genes=c('ADCYAP1R’, ‘GRM1’, ‘GRM3’, ‘HTR1F’, ‘HTR2A’, ‘MCHR2’, ‘RXFP1’), Log2FC=c(-2.299, 0.52, 2.439, 1.783, 2.459, 5.196, 4.071))
df2 <- data.frame(Genes=c('ADCYAP1R’, ‘GRM1’, ‘GRM3’, ‘HTR1F’, ‘HTR2A’, ‘MCHR2’, ‘RXFP1’), grad=c(7.20, 7.79, 6.45, 7.43, 5.79, 0.79, 5.48))
df3 <- data.frame(Genes=c('ADCYAP1R’, ‘GRM1’, ‘GRM3’, ‘HTR1F’, ‘HTR2A’, ‘MCHR2’, ‘RXFP1’), beta=c(0.283, 0.35, 0.405, 0.451, 0.594, 0.523, 0.65))
I want a gradient bar plot with the height of the bar from the 1st vector, the gradient from the 2nd vector and the points on the bar from the 3rd vector.
I was following the below code, but i'm not able to get the output i want.
p <- ggplot(df, aes(x=Genes, y=Log2FC))
p <- p + geom_bar(stat = "identity", aes(fill = Log2FC)) + scale_fill_gradient2(low='plum1', mid='plum', high='plum4')
p + geom_point(data=df3, aes(x=as.numeric(Genes), y=total), colour = "blue", shape = 15, size = 3) + scale_y_continuous(sec.axis = sec_axis(~./3, name = "Beta Value"))
can anyone please help me?
1 answer
Here is something for you to start with, you don't need 3 different dataframes.
df <- data.frame(Genes = c('ADCYAP1R', 'GRM1', 'GRM3', 'HTR1F', 'HTR2A', 'MCHR2', 'RXFP1'),
Log2FC = c(-2.299, 0.52, 2.439, 1.783, 2.459, 5.196, 4.071),
grad = c(7.20, 7.79, 6.45, 7.43, 5.79, 0.79, 5.48),
beta = c(0.283, 0.35, 0.405, 0.451, 0.594, 0.523, 0.65))
df <- df %>%
dplyr::arrange(Log2FC) %>%
tibble::rowid_to_column(var = "POS")
df$Genes <- factor(df$Genes, levels = df$Genes)
ggplot(df, aes(x = Genes, y = Log2FC, fill = grad))+
geom_bar(stat = "identity")+
geom_point(aes(x = POS, y = beta))+
scale_fill_viridis_c()
This produces

You can play with secondary y-axis for geom_point.
P.S: I'd rather suggest to add text labels of beta on top of bars than adding points on the bars.
If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they work.

is there a function to use the gradient colours as different shades of grey with a white background?
Official ggplot2 website has all this info and how to modify components of a plot. E.g. Sequential grey colour scales
There are functions, such as colorRamp and colorRampPalette that take a set of discrete colors and make a continuous scale out of them. See here:
You may want to use them in conjunction with gray.colors
Log in to answer this question.
Your code contains stylized single quotes mixed in with regular ASCII single quotes. This could result in unexpected errors. Please make sure that your code only has ASCII characters.
should be