2016-05-08 7 views
1

Nehmen wir an, ich habe den Datenrahmen d (dput unten). Ich möchte es mit gefüllten Balken zeichnen, die durch Task und Event facettiert sind und mit der Anzahl von Werten, die jeden Balken bilden, annotiert werden.Verwenden Sie facet_grid und kommentieren in ggplot2

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) 

Problem ist, dass ich nicht herausfinden kann, wie man die label Vektor innerhalb annotate() zu arrangieren.

Wenn ich nur facet_grid() verwenden dann erhalte ich die Facettierung:

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

grid

Wenn ich nur annotate() verwenden dann bekomme ich die Zahlen:

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

Aber ich kann nicht Finde heraus, wie man das label Argument von annotate() mit facet_grid arbeiten kann. Irgendeine Hilfe?

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

annotate nicht Panel-spezifische Anmerkungen erlaubt; Sie müssen dafür eine Geom-Ebene verwenden. – baptiste

+1

Ihr Code läuft nicht und die meisten R-Editoren benötigen diese 'dput'-Ausgabe, die zum Arbeiten verpackt ist. – hrbrmstr

+0

@baptiste, gibt es eine Möglichkeit, 'geom_text()' so zu verwenden, dass für jede Zeile im Dataframe nicht derselbe Text über sich selbst gestapelt wird? @hrbrmstr, ich weiß, der erste Plot funktioniert nicht, es ist mein Problemcode. Danke, dass Sie mich über das 'dput'-Problem informiert haben. Korrigiert. – CephBirk

Antwort

3

Sie müssen die Daten an-Prozess vor einer geom_text Schicht mit den Facettierschritt Variablen zu machen,

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

Verwandte Themen