2016-05-08 20 views
1

データフレームd(以下dput)があるとします。私はTaskEventで端が塗りつぶされた塗りつぶされた棒でそれをプロットしたいと思いますし、各バーを構成する値の数で注釈を付けました。ggplot2でfacet_gridと注釈を使用

ggplot(inter2, aes(Aud, fill = value)) + 
    geom_bar(position = 'fill') + 
    facet_grid(Task~Event) + 
    annotate('text', label = as.character(table(inter2$Aud)), x = 1:nlevels(inter2$Aud), y = 1) 

問題は、私はannotate()labelベクトルを配置する方法を見つけ出すことができないということです。

私はfacet_grid()を使用する場合は、その後、私は面取りを得る:

ggplot(inter2, aes(Aud, fill = value)) + 
    geom_bar(position = 'fill') + 
    facet_grid(Task~Event) 

grid

私はannotate()を使用する場合は、その後、私は番号を取得:

ggplot(inter2, aes(Aud, fill = value)) + 
    geom_bar(position = 'fill') + 
    annotate('text', label = as.character(table(inter2$Aud)), x = 1:nlevels(inter2$Aud), y = 1) 

numbers

でも、私は出来ません label引数をannotate()に設定してfacet_gridに設定する方法を解説します。どんな助け?

d = structure(list(Event = c("Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", 
        "Lecture", "Lecture", "Lecture", "Lecture", "Lecture", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session", "Interactive session", 
        "Interactive session", "Interactive session"), Task = c("Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", "Prepare", 
                      "Prepare", "Prepare", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", "Deliver", 
                      "Deliver", "Deliver"), Aud = structure(c(5L, 5L, 5L, 5L, 5L, 
                                 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 
                                 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 
                                 2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 
                                 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 
                                 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 
                                 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
                                 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Funders/Donors", 
                                              "Non-scientific, lay audiences", "Policy makers", "Scientists with expertise dissimilar to mine", 
                                              "Scientists with expertise similar to mine"), class = "factor"), 
      value = c("A little more confident", "Much more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "A little more confident", "A little more confident", "Much more confident", 
        "A little more confident", "Much more confident", "Much more confident", 
        "A little more confident", "Much more confident", "Much more confident", 
        "A little more confident", "A little more confident", "Much more confident", 
        "A little more confident", "A little more confident", "Much more confident", 
        "A little more confident", "A little more confident", "About the same as before", 
        "A little more confident", "A little more confident", "Much more confident", 
        "A little more confident", "A little more confident", "A little more confident", 
        "A little more confident", "A little more confident", "About the same as before", 
        "A little more confident", "A little more confident", "Much more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "Much more confident", "A little more confident", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "A little more confident", "A little more confident", "Much more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "Much more confident", "Much more confident", "A little more confident", 
        "Much more confident", "A little more confident", "Much more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "A little more confident", "Much more confident", "A little more confident", 
        "About the same as before", "About the same as before", "Much more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "A little more confident", "A little more confident", "A little more confident", 
        "About the same as before", "About the same as before", "Much more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "Much more confident", "A little more confident", "Much more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "About the same as before", "Much more confident", "Much more confident", 
        "About the same as before", "Much more confident", "A little more confident", 
        "A little more confident", "A little more confident", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "About the same as before", "Much more confident", "Much more confident", 
        "About the same as before", "Much more confident", "Much more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "A little more confident", "A little more confident", "Much more confident", 
        "About the same as before", "About the same as before", "About the same as before", 
        "A little more confident", "About the same as before", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "About the same as before", "About the same as before", "About the same as before", 
        "A little more confident", "About the same as before", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "A little more confident", "Much more confident", "Much more confident", 
        "About the same as before", "Much more confident", "A little more confident", 
        "A little more confident", "A little more confident", "A little more confident", 
        "A little more confident", "Much more confident", "A little more confident", 
        "Much more confident", "Much more confident", "Much more confident", 
        "About the same as before", "A little more confident", "A little more confident", 
        "Much more confident", "About the same as before", "A little more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "About the same as before", "Much more confident", "Much more confident", 
        "About the same as before", "A little more confident", "Much more confident", 
        "Much more confident", "About the same as before", "A little more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "A little more confident", "A little more confident", "Much more confident", 
        "About the same as before", "About the same as before", "About the same as before", 
        "Much more confident", "About the same as before", "A little more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "A little more confident", "Much more confident", "A little more confident", 
        "About the same as before", "About the same as before", "About the same as before", 
        "Much more confident", "About the same as before", "A little more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "A little more confident", "Much more confident", "Much more confident", 
        "About the same as before", "Much more confident", "A little more confident", 
        "Much more confident", "About the same as before", "A little more confident", 
        "A little more confident", "Much more confident", "About the same as before", 
        "Much more confident")), row.names = c(NA, -207L), class = "data.frame", .Names = c("Event", 
                             "Task", "Aud", "value")) 
+1

注釈パネル固有のアノテーションを許可していない、ファセッティング変数とgeom_text層を作るために、データを事前処理する必要があります。そのためには、ジオメトリレイヤーを使用する必要があります。 – baptiste

+1

あなたのコードは実行されず、ほとんどのRエディタは 'dput'出力が動作するようにラップされている必要があります。 – hrbrmstr

+0

@baptiste、データフレームの各行に対して同じテキストがそれ自身の上に積み重ねられないように 'geom_text()'を使う方法はありますか? @hrbrmstr、私は最初のプロットが動作しないことを知っている、それは私の問題コードです。私は 'dput'問題について教えてくれてありがとう。修正されました。 – CephBirk

答えて

3

あなたは

library(plyr) 
d2 <- ddply(d, .(Task, Event), function(.d) data.frame(table(.d$Aud, dnn=list("Aud")))) 

ggplot(d, aes(Aud, fill = value)) + 
    geom_bar(position = 'fill') + 
    facet_grid(Task~Event) + 
    geom_text(data=d2, aes(Aud, y=1, label=Freq), 
      vjust=1, fontface=4, col="white", inherit.aes=FALSE) + 
    theme(axis.text.x=element_text(angle = 90, hjust=1, vjust=0.5)) 

enter image description here

関連する問題