49

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

、、、スタック変数がでありx=treatment、回避変数がである棒グラフを作成したいと思います。もちろん、私はどちらか一方を行うことができます:y=totaltypeyear

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

しかし、両方ではありません!アドバイスを提供できる人に感謝します。

4

5 に答える 5

32

回避の代わりにファセットを使用する別の方法は次のとおりです。

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

ここに画像の説明を入力してください

タイラーの提案した変更で:+ theme(panel.margin = grid::unit(-1.25, "lines"))

ここに画像の説明を入力してください

于 2012-10-03T19:49:40.910 に答える
12

interaction(year, treatment)の代わりに、x軸変数として使用できますdodge

library(dplyr)
library(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)) %>% 
  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')

reprexパッケージ(v0.2.0)によって2018-04-26に作成されました。

于 2018-04-26T13:48:47.650 に答える
11

dodged最も近い方法は、バーの周囲に境界線を描画して、積み上げられたtype値を強調表示することです。

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

ここに画像の説明を入力してください

于 2012-10-03T19:48:25.380 に答える
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))

積み重ねられた回避されたbarchar これは実際にはうまく拡張できませんが、状況に合わせてコード化する方法があります。これは、私が最初に次の投稿からこの方法を学んだことによるものです。https ://community.rstudio.com/t/ ggplot-position-dodge-with-position-stack / 16425

于 2019-10-06T10:54:30.563 に答える
4

あなたはいくつかのアルファで遊ぶことができます:

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

ここに画像の説明を入力してください

theme(panel.background = element_rect(fill ="yellow"))これで、背景の塗りつぶしを追加して色を混ぜることができます。

ここに画像の説明を入力してください

最後に、inkscapeを使用して凡例を修正する必要があります。

于 2021-02-09T10:45:00.547 に答える