I am trying to create horizontal bar plot and would like to fill the individual bar as per their log fold values, white for lowest value and darkest for highest value. However, I am not able to do it. Here is my work, can somebody help me fix it?
df <- data.frame(LogFold = c(14.20, 14.00, 8.13, 5.3, 3.8, 4.9, 1.3, 13.3, 14.7, 12.2),
Tissue = c("liver", "adrenal", "kidney", "heart", "limb", "adipose", "brown", "hypothalamus", "arcuate", "lung"))
df1<-df%>%
arrange(desc(LogFold))
ggplot(data=df1, aes(x=Tissue, y=LogFold, fill = Tissue)) +
geom_bar(stat="identity")+
scale_colour_gradient2()+
coord_flip()+
ylim(0, 15)+
scale_x_discrete(limits = df1$Tissue)+
theme_classic()
Thank You in advance!
Here's what you need to think about:
geom_col
is less typing than geom_bar(stat = ...)
scale_fill_gradient2()
theme_classic
So you could try something like this:
library(tidyverse)
df1 %>%
ggplot(aes(reorder(Tissue, LogFold), LogFold)) +
geom_col(aes(fill = LogFold)) +
scale_fill_gradient2(low = "white",
high = "blue",
midpoint = median(df1$LogFold)) +
coord_flip() +
labs(x = "Tissue")
But I don't know that the color gradient really adds anything much in terms of interpreting the information. So here's the result without it, you be the judge:
df1 %>%
ggplot(aes(reorder(Tissue, LogFold), LogFold)) +
geom_col() +
coord_flip() +
labs(x = "Tissue") +
theme_classic()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With