2017-05-11 11 views
1

ggplotを使用して棒グラフを作成していて、その年のように「セカンダリ」x軸を作成しようとしています。私は以下の持っているコードは、私が作成しようとしています何を複製します:私は、単一のグラフを作成していたときにグループ化された棒グラフのセカンダリx軸を作成する

library('ggplot2') 

d1 <- structure(list(year = c(2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 
           2011L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 2013L, 
           2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2016L, 
           2016L, 2016L, 2016L), 
        group = c("Group 1", "Group 2", "Group 4", 
           "Group 1", "Group 2", "Group 3", "Group 4", "Group 1", "Group 2", 
           "Group 3", "Group 4", "Group 1", "Group 2", "Group 3", "Group 4", 
           "Group 1", "Group 2", "Group 3", "Group 4", "Group 1", "Group 2", 
           "Group 3", "Group 4", "Group 1", "Group 2", "Group 3", "Group 4"), 
        count = c(111L, 216L, 22L, 125L, 231L, 22L, 57L, 126L, 256L, 
           22L, 70L, 137L, 302L, 41L, 81L, 132L, 299L, 30L, 108L, 140L, 
           337L, 22L, 95L, 168L, 383L, 31L, 96L)), 
       .Names = c("year", "group", "count"), 
       row.names = c(3L, 4L, 5L, 17L, 18L, 19L, 20L, 32L, 
           33L, 34L, 35L, 47L, 48L, 49L, 50L, 62L, 63L, 64L, 65L, 77L, 78L, 
           79L, 80L, 92L, 93L, 94L, 95L), 
       class = "data.frame") 

ggplot(d1, aes(x = group, y = count, group = year, fill = group)) + 
    geom_bar(stat = 'identity', position = 'dodge', color = 'black') + 
    scale_y_continuous(breaks = seq(0, 400, 25), 
        limits = c(-20, 400)) + 
    geom_text(aes(x = group, y = 0, group = year, label = year), 
      position = position_dodge(width = .92), size = 4, angle = -90, hjust = -.1) + 
    guides(fill = FALSE) 

は今、これは私の目的のためにうまく動作しますが、私はそのような中、このよう数字を組み込むしようとしていますこの例でやったように、データの範囲が大きくて、yの下限を-20に設定するだけの光沢のあるアプリは、バーの下に年を表示する効果的な方法ではありません。

geom_textを使用して回避することなく、私がここで何をしているかを複製する方法はありますか?

答えて

1

説明した内容に基づいています。私はファセットプロットを探していると思います。

ファセットプロットを作成するには、facet_wrap,facet_gridを使用します。このコードスニペットは、geom_textを使用せずに同様の処理を行う必要があります。当然の

ggplot(d1, aes(x=year,y=count,fill=group)) + 
    geom_bar(stat = 'identity', position = 'dodge', color = 'black') + 
    facet_wrap(~group) + 
    guides(fill = FALSE) + 
    scale_y_continuous(breaks = seq(0, 400, 25), 
        limits = c(-20, 400)) 

enter image description here

+0

Ahhhhhhh ......。ファセットはこの特定のケースで実際に私のために働くでしょう。ありがとう! – Sam

関連する問題