簡體   English   中英

我需要給 plot 添加漸變色

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

我想給 plot 添加漸變色。

這是數據:

姓名 WT1 MUT1 WT2 MUT2
DNA損傷 50 40 45 5個
端粒酶 60 55 55 2個
DNA修復 90后 80 80 4個
信封修復 45 30 35 6個

它使用以下代碼嘗試了上述數據:


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

創建的 plot 如下所示: 在此處輸入圖像描述

我需要給這個plot添加漸變色,謝謝大家的幫助!

我可能誤解了你在找什么,但我認為你正在尋找每個欄中的漸變填充。 在 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')

在此處輸入圖像描述

你可以試試這個,它會在所有的條上放置一個漸變。 這會將aes中的fill命令從Name更改為value並添加scale_fill_gradient 在這里,我選擇了bluered ,但可以將它們更改為所需的 colors。

數據

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)

代碼

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