使用正值和负值控制 ggplot::geom_bar position_stack 在图形和图例中的填充颜色顺序

ktm

我有一个包含三列的 df:

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

我在 中以我想要的顺序订购的样品sorder,并作为因子应用到 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)

示例图,我希望堆栈的顺序与图例颜色相同

彼得

困难似乎是使用因子与 ggplot 处理position_stack.

来自文档:“分别执行正值和负值的堆叠,以便正值从 x 轴向上堆叠,负值向下堆叠。” 似乎堆叠胜过因素。

所以需要一些人工干预:

1)重新排序gorder以处理负值的堆叠反转

2) 使用scale_fill_manualcolorBrewer 调色板的重建版本以正确的顺序在图表中获取填充颜色。带有按要求顺序的标签。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2 | geom_bar和position =“ identity”

使用geom_bar和geom_point控制ggplot中的图例

ggplot:geom_bar 堆叠顺序和标签

在 ggplot 中使用 geom_bar(position="fill, stat = "identity) 翻转填充变量

ggplot2:结合定位命令position_nudge和position_stack

控制ggplot2 geom_bar的填充顺序和组

ggplot2:合并geom_line,geom_point和geom_bar的图例

如何使用 geom_bar 优化图形 ggplot

geom_bar的宽度和间隙(ggplot2)

geom_bar ggplot2堆叠,成组的条形图,具有正值和负值-金字塔图

如何更改 ggplot geom_bar 中的条形名称和图例标题?

ggplot2 :: position_stack中的距离如何缩放?

更改 ggplot geom_bar 中 NA 值的标签和颜色?

忽略ggplot中geom_bar和geom_histogram中的异常值

如何在ggplot2中获取堆叠的geom_bar和geom_point的通用图例?

ggplot geom_bar根据条件使用单独的颜色填充连续颜色

ggplot2:在aes(..)和geom_bar(..)中使用`fill =…`。颜色重复

ggplot2:在aes(..)和geom_bar(..)中使用`fill =…`。颜色不重复

ggplot2(geom_path,geom_bar,ggbio)的核图和SNP

是否可以在ggplot2的堆叠条形图中组合position_stack和nudge_x?

为ggplot2中的负值和正值在geom_point中设置不同的颜色

在 geom_bar 中同時使用填充和組參數時 ggplot2 中的錯誤

ggplot2:如何使用geom_bar减小宽度和条之间的间隔

如何使用facet_wrap`表`所有因子列和ggplot geom_bar?

使用 ggplot2 和 geom_bar 更改 X 轴的起点

ggplot条形图(geom_bar)使用df的起点和终点

在ggplot2 r中操纵geom_bar和coord_polar的描述

更改ggplot geom_bar上填充变量的顺序

R:ggplot geom_bar填充顺序未遵循级别