如何使用scale_fill_viridis在ggplot中循环重复颜色?

xzabzqsa  于 2023-03-10  发布在  其他
关注(0)|答案(2)|浏览(142)

由于我在比例堆叠条形图中显示了许多代谢物,这意味着当我使用scale_fill_viridis时,很难区分连续出现的代谢物的某些颜色(见图片)。有没有办法我可以重复颜色,比如说从草绿色库中取出6种独特的颜色并循环使用。图例将按照代谢物在图中出现的顺序排列,因此循环颜色将仍然容易按照图例顺序来解释。

我没有办法告诉你我的结果除了有一个相同颜色的循环,所以这样的东西:

Ala = dark purple
allo.ille = purple
Ans = blue
Arg = green
Asp = light green
Car = yellow

然后,依次循环显示接下来6种代谢物的颜色:

Citr = dark purple
Cys = purple
Cyst.1 = blue
Cyst.2 = green
Eth = light green
GABA = yellow

下面是我用来制作图表的代码(注解掉的是我尝试使用的其他比例填充,但它们似乎根本不起作用),我需要帮助调整scale_fill_viridis,或者我也可以使用其他调色板或方法来引入颜色循环而不是渐变:

library(viridis)
library(ggplot2)

# From suggestion below, did not work
#color_mapping <- rep(viridis(6),100)

ggplot(df, aes(fill=group, y=value, x=Species)) +
geom_bar(position="fill", stat="identity") + 
#scale_color_manual(values=color_mapping[1:n_distinct(df$group)])+
#scale_colour_brewer(palette="Set5") + 
scale_fill_viridis(discrete=TRUE, name="NEFA Content") +
theme_ipsum() + theme(axis.text.x=element_text(angle=90, hjust=1,vjust=0.25)) +
theme(axis.title.x=element_blank()) + theme(axis.title.y=element_blank()) +
theme(panel.background=element_rect(colour="black")) +
theme(panel.grid.major=element_blank())
mklgxw1f

mklgxw1f1#

我有一个肮脏的解决办法,不是很喜欢(也许我的颜色选择不好),但我确实得到了我想要的颜色循环。重要的是列出颜色并重复它们,确保它们与我的代谢物或类别的数量完全匹配。
这是代码,这是输出,我重复了7次来自viridis软件包的6种颜色,因为我有42种代谢物显示在我的图上。

ggplot(df, aes(fill=group, y=value, x=Species)) + 
  geom_bar(position="fill", stat="identity") + 
  theme_ipsum() + theme(axis.text.x=element_text(angle=90, hjust=1,vjust=0.25)) +
  theme(axis.title.x=element_blank()) + theme(axis.title.y=element_blank()) +
  theme(panel.background=element_rect(colour="black")) +
  theme(panel.grid.major=element_blank()) + scale_y_continuous(expand=c(0,0)) +
  scale_fill_manual(values=c("#440154FF", "#3F4788FF", "#2D718EFF", "#29AF7FFF", "#94D840FF", "#FDE725FF",
                                         "#440154FF", "#3F4788FF", "#2D718EFF", "#29AF7FFF", "#94D840FF", "#FDE725FF",
                                         "#440154FF", "#3F4788FF", "#2D718EFF", "#29AF7FFF", "#94D840FF", "#FDE725FF",
                                         "#440154FF", "#3F4788FF", "#2D718EFF", "#29AF7FFF", "#94D840FF", "#FDE725FF",
                                         "#440154FF", "#3F4788FF", "#2D718EFF", "#29AF7FFF", "#94D840FF", "#FDE725FF",
                                         "#440154FF", "#3F4788FF", "#2D718EFF", "#29AF7FFF", "#94D840FF", "#FDE725FF",
                                         "#440154FF", "#3F4788FF", "#2D718EFF", "#29AF7FFF", "#94D840FF", "#FDE725FF"))

下面是输出:

zzzyeukh

zzzyeukh2#

您可以将草绿色指定给向量,并使用该向量手动为绘图着色:

# this is just because I was to lazy to count how many values you actually need. When calling ggplot, it will only select as many colors as it actually needs "[1:n_distinct(dsamp$clarity)]" part later
color_mapping <- rep(viridis(3),1000)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
ggplot(dsamp , aes(carat, price)) +
  geom_point(aes(colour = clarity)) +
  scale_color_manual(values = color_mapping [1:n_distinct(dsamp$clarity)])

使用您的代码,它应该看起来像这样:

library(viridis)
library(ggplot2)
color_mapping <- rep(viridis(6),1000)

ggplot(df, aes(fill=group, y=value, x=Species)) +
  geom_bar(position="fill", stat="identity") + 
  scale_fill_manual(values=color_mapping[1:n_distinct(df$group)]) +
  theme_ipsum() + theme(axis.text.x=element_text(angle=90, hjust=1,vjust=0.25)) +
  theme(axis.title.x=element_blank()) + theme(axis.title.y=element_blank()) +
  theme(panel.background=element_rect(colour="black")) +
  theme(panel.grid.major=element_blank())

相关问题