Gradient bar plot in ggplot2
1
0
Entering edit mode
4.0 years ago
suvratha ▴ 60

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?

R ggplot2 RNA-Seq • 3.5k views
ADD COMMENT
0
Entering edit mode

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.

df <- data.frame(Genes=c('ADCYAP1R’
__________________________________^

should be

df <- data.frame(Genes=c('ADCYAP1R'
ADD REPLY
2
Entering edit mode
4.0 years ago
venu 7.1k

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

enter image description here

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.

ADD COMMENT
0
Entering edit mode

Thank you very much!

ADD REPLY
0
Entering edit mode

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.

Upvote|Bookmark|Accept

ADD REPLY
0
Entering edit mode

is there a function to use the gradient colours as different shades of grey with a white background?

ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Official ggplot2 website has all this info and how to modify components of a plot. E.g. Sequential grey colour scales

ADD REPLY

Login before adding your answer.

Traffic: 1706 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6