按特定顺序排列ggplot,同时保留色散

问题描述 投票:0回答:1

我有一个包含三列的df:

-“ sampple”〜包含样本名称-“组”〜包含组(第一次,第二次,保持上升,保持下降,第二次下降,第一次下降)-“值”〜包含每个组中的数字。

我已按所需顺序依次订购了样本,并将其作为因子来对x轴上的刻度进行排序。我以类似的方式将组按gorder排序,并将它们作为因子应用于图中的堆栈。

我对图例中的组(及其颜色)的顺序感到满意,并希望对堆栈进行相同的排序。我尝试重新排列这些因素,但是由于没有手工挑选颜色,所以我未能设法按与图例相同的顺序获得纸叠。您有什么建议吗?

require(dplyr)
require(ggplot2)

gorder<-c("first up", "second up", "remain up", "remain down", "second down", "first down")

sorder<-c("55_NST", "40_NST","25_NST","ad_NST", "RH_NST", "FT_ST",  "55_ST", "25_ST")

set.seed(1)
df<-data.frame(
  "sample" =rep(sorder, each=6),
  "group"=rep(gorder, times=8),
  "value"=c(abs(rnorm(48,mean=3000, sd=500))))
df<-df%>%mutate(value =case_when(group %in% c("remain down", "second down", "first down") ~ value *(-1),
                                 !group %in% c("remain down", "second down", "first down") ~ value))


df$sample<-factor(df$sample, levels = sorder)
df$group<-factor(df$group, levels = gorder)

ggplot(df, aes(fill=group, y=value, x=sample)) + 
    geom_bar(position="stack", stat="identity") +
 theme_bw()+
  scale_x_discrete(breaks=sorder, labels=c("55", "40", "25", "AD", "RH", "FT (ST)", "55 (ST)", "25 (ST)"))+
  scale_y_continuous(breaks = seq(from = -12000,to = 12000, by = 2000))+
  labs(y="number of genes", x="RWC")+
  scale_fill_brewer(type = "div", palette = "RdYlGn",direction = -1)

Example graph, I would like the stacks in the same order as the legend colors

r ggplot2 bar-chart
1个回答
0
投票

困难似乎是使用因素与ggplot处理position_stack中负值的方式之间的冲突。

从文档中:“正值和负值的堆叠分别进行,因此正值从x轴向上堆叠,而负值向下堆叠。”似乎堆积如山的因素。

因此需要一些手动干预:

1)重新排序gorder以处理负值的堆叠反转2)使用scale_fill_manual和colorBrewer调色板的重建版本在图表中以正确的顺序获取填充颜色。按要求的顺序贴标签。3)覆盖图例指南,以使颜色与原始标签顺序匹配。

可能会有更有效的方法来实现这一目标...

library(ggplot2)
library(dplyr)
library(forcats)
library(RColorBrewer)

gorder<-c("first up", "second up", "remain up", "remain down", "second down", "first down")

gorder_col <- c("first up", "second up", "remain up", rev(c("remain down", "second down", "first down")))

sorder<-c("55_NST", "40_NST","25_NST","ad_NST", "RH_NST", "FT_ST",  "55_ST", "25_ST")

set.seed(1)

df<-data.frame(
  "sample" = rep(sorder, each=6),
  "group" = rep(gorder, times=8),
  "value" = c(abs(rnorm(48,mean=3000, sd=500))))

df<-
  df %>%
  mutate(value = case_when(group %in% c("remain down", "second down", "first down") ~ value * (-1),
                           !group %in% c("remain down", "second down", "first down") ~ value),
         sample = factor(sample, levels = sorder),
         group = factor(group, levels = gorder_col))


ggplot(df, aes(fill = group, y = value, x = sample)) + 
  geom_bar(position="stack", stat="identity") +
  theme_bw()+
  scale_x_discrete(breaks = sorder, labels = c("55", "40", "25", "AD", "RH", "FT (ST)", "55 (ST)", "25 (ST)"))+
  scale_y_continuous(breaks = seq(from = -12000,to = 12000, by = 2000))+
  labs(y="number of genes", x="RWC")+
  scale_fill_manual(values = c(brewer.pal(name = "RdYlGn", n = 6)[6:4], brewer.pal(name = "RdYlGn", n = 6)[1:3]),
                    labels = gorder)+
  guides(fill = guide_legend(override.aes = list(fill = brewer.pal(name = "RdYlGn", n = 6)[6:1])))

“”

© www.soinside.com 2019 - 2024. All rights reserved.