简体   繁体   English

我需要给 plot 添加渐变色

[英]I need to add gradient colour to the plot

I want to add gradient colour to the plot.我想给 plot 添加渐变色。

Here is the data:这是数据:

Name姓名 WT1 WT1 MUT1 MUT1 WT2 WT2 MUT2 MUT2
DNA damage DNA损伤 50 50 40 40 45 45 5 5个
Telomerase端粒酶 60 60 55 55 55 55 2 2个
DNA repair DNA修复 90 90后 80 80 80 80 4 4个
Envelope repair信封修复 45 45 30 30 35 35 6 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:创建的 plot 如下所示: 在此处输入图像描述

I need to add gradient colour to this plot. Thank you for the help!我需要给这个plot添加渐变色,谢谢大家的帮助!

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:在 ggplot 中没有创建这样的原生渐变填充的选项,因此您必须自己创建效果:

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')

在此处输入图像描述

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 .这会将aes中的fill命令从Name更改为value并添加scale_fill_gradient Here I chose blue to red but those can be changed to desired colors.在这里,我选择了bluered ,但可以将它们更改为所需的 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")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM