I want to add gradient colour to the plot.
Here is the data:
Name | WT1 | MUT1 | WT2 | MUT2 |
---|---|---|---|---|
DNA damage | 50 | 40 | 45 | 5 |
Telomerase | 60 | 55 | 55 | 2 |
DNA repair | 90 | 80 | 80 | 4 |
Envelope repair | 45 | 30 | 35 | 6 |
It tried the above data using the following code:
library("ggplot2")
library(tidyverse)
top_fun <- read.delim(file="TEST2.txt",header = TRUE)
topfun <- as.data.frame(top_fun)
topfun %>%
pivot_longer(-Name) %>%
ggplot(aes(x = Name, y = value, fill = name))
geom_col(position = position_stack(), color="black")
coord_flip() facet_wrap(~name) facet_grid(~name)
theme(axis.text = element_text(size=13))
The plot which created looks like this:
I need to add gradient colour to this plot. Thank you for the help!
CodePudding user response:
I may be misunderstanding what you are looking for, but I think you are looking for a graduated fill within each bar. There is no option to create a gradient fill like this natively in ggplot, so you would have to create the effect yourself:
topfun %>%
pivot_longer(-Name) %>%
ggplot(aes(x = Name, y = value, fill = name))
geom_col(position = position_stack(), color="black")
geom_col(data = . %>% tidyr::uncount(weight = value) %>%
group_by(Name, name) %>% mutate(rn = row_number()),
aes(y = 1, alpha = 1 - rn/75,
color = after_scale(alpha('white', alpha))),
position = position_stack(), fill = 'white')
geom_col(position = position_stack(), fill = NA, color = "black")
coord_flip()
facet_grid(~name)
theme(axis.text = element_text(size = 13))
scale_alpha_continuous(range = c(0, 0.4), guide = 'none')
CodePudding user response:
You could try this, which will put a gradient across all the bars. This changes the fill
command in aes
from Name
to value
and adds a scale_fill_gradient
. Here I chose blue
to red
but those can be changed to desired colors.
Data
topfun <- read.table(text = "Name WT1 MUT1 WT2 MUT2
DNA_damage 50 40 45 5
Telomerase 60 55 55 2
DNA_repair 90 80 80 4
Envelope_repair 45 30 35 6", header = TRUE)
Code
library(ggplot2)
library(tidyr)
topfun %>%
pivot_longer(-Name) %>%
ggplot(aes(x = Name, y = value, fill = value))
geom_bar(stat = "identity", color = "black")
coord_flip() facet_wrap(~name) facet_grid(~name)
theme(axis.text = element_text(size=13))
scale_fill_gradient(low = "blue", high = "red")