ggplot2 - 叠加和闪避的条形图

jsl*_*che 30 r ggplot2

我正在尝试创建一个条形图,使用ggplot2我在一个变量堆叠并由另一个变量躲避的地方.

这是一个示例数据集:

df=data.frame(
  year=rep(c("2010","2011"),each=4),
  treatment=rep(c("Impact","Control")),
  type=rep(c("Phylum1","Phylum2"),each=2),
  total=sample(1:100,8))
Run Code Online (Sandbox Code Playgroud)

我想创建一个条形图,其中x=treatment,y=total堆叠变量是type,并且躲闪变量是year.当然我可以做其中一个:

ggplot(df,aes(y=total,x=treatment,fill=type))+geom_bar(position="dodge",stat="identity")

ggplot(df,aes(y=total,x=treatment,fill=year))+geom_bar(position="dodge",stat="identity")
Run Code Online (Sandbox Code Playgroud)

但不是两个!感谢任何能提供建议的人.

Mat*_*ker 23

这是使用刻面而不是躲避的另一种选择:

ggplot(df, aes(x = year, y = total, fill = type)) +
    geom_bar(position = "stack", stat = "identity") +
    facet_wrap( ~ treatment)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

随着泰勒的建议改变:

在此输入图像描述

  • 添加`+ theme(panel.margin = unit(-1.25,"lines"))`可以使它们看起来更像是在同一个视野中,但仍然不是OP所追求的.不错的最佳选择.+1 (11认同)

Mai*_*ura 10

您可以获得的最接近的是在dodged条形图周围绘制边框以突出显示堆叠type值.

ggplot(df, aes(treatment, total, fill = year)) + 
geom_bar(stat="identity", position="dodge", color="black")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Rom*_*man 9

你可以玩一些阿尔法:

df %>% 
  group_by(year, treatment) %>% 
  mutate(cum_tot = cumsum(total)) %>% 
  ggplot(aes(treatment, cum_tot, fill =year)) + 
  geom_col(data = . %>% filter( type=="Phylum1"), position = position_dodge(width = 0.9), alpha = 1) +
  geom_col(data = . %>% filter( type=="Phylum2"), position = position_dodge(width = 0.9), alpha = 0.4) +
  geom_tile(aes(y=NA_integer_, alpha = factor(type))) + 
  scale_alpha_manual(values = c(1,0.4))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

现在您可以添加theme(panel.background = element_rect(fill ="yellow"))一些背景填充来混合颜色:

在此输入图像描述

最后你必须使用 inkscape 修复图例。


Ken*_*son 7

您可以使用interaction(year, treatment)x轴变量作为替代dodge.

library(tidyverse)

df=data.frame(
  year=rep(c("2010","2011"),each=4),
  treatment=rep(c("Impact","Control")),
  type=rep(c("Phylum1","Phylum2"),each=2),
  total=sample(1:100,8)) %>% 
  mutate(x_label = factor(str_replace(interaction(year, treatment), '\\.', ' / '), ordered=TRUE))

ggplot(df, aes(x=x_label, y=total, fill=type)) +
  geom_bar(stat='identity') + labs(x='Year / Treatment')
Run Code Online (Sandbox Code Playgroud)

reprex包(v0.2.0)创建于2018-04-26.


Mic*_*don 5

这是可以完成的,但是它很棘手/繁琐,您基本上必须对条形图进行分层。

这是我的代码:

library(tidyverse)

df=data.frame(
  year=rep(c(2010,2011),each=4),
  treatment=rep(c("Impact","Control")),
  type=rep(c("Phylum1","Phylum2"),each=2),
  total=sample(1:100,8))

# separate the by the variable which we are dodging by so 
# we have two data frames impact and control
impact <- df %>% filter(treatment == "Impact") %>% 
  mutate(pos = sum(total, na.rm=T))

control <- df %>% filter(treatment == "Control") %>% 
  mutate(pos = sum(total, na.rm=T))

# calculate the position for the annotation element
impact_an <- impact %>% group_by(year) %>% 
  summarise(
    pos = sum(total) + 12
    , treatment = first(treatment)
  )

control_an <- control %>% group_by(year) %>% 
  summarise(
    pos = sum(total) + 12
    , treatment = first(treatment)
  )

# define the width of the bars, we need this set so that
# we can use it to position the second layer geom_bar 
barwidth = 0.30

ggplot() +
  geom_bar(
    data = impact
    , aes(x = year, y = total, fill = type)
    , position = "stack"
    , stat = "identity"
    , width = barwidth
  ) + 
  annotate(
    "text"
    , x = impact_an$year
    ,y = impact_an$pos
    , angle = 90
    , label = impact_an$treatment
  ) +
  geom_bar(
    data = control
    # here we are offsetting the position of the second layer bar
    # by adding the barwidth plus 0.1 to push it to the right
    , aes(x = year + barwidth + 0.1, y = total, fill = type)
    , position = "stack"
    , stat = "identity"
    , width = barwidth
  ) +
  annotate(
    "text"
    , x = control_an$year + (barwidth * 1) + 0.1
    ,y = control_an$pos
    , angle = 90
    , label = control_an$treatment
  ) +
  scale_x_discrete(limits = c(2010, 2011))
Run Code Online (Sandbox Code Playgroud)

堆叠躲闪巴查尔 这并不能很好地扩展,但是您可以通过多种方式对其进行编码以使其适合您的情况,这要归功于我最初从以下帖子中学到了这种方法: https: //community.rstudio.com/t/ ggplot-position-dodge-with-position-stack/16425