R 在ggplot2中使用刻面网格和注释

R 在ggplot2中使用刻面网格和注释,r,plot,ggplot2,R,Plot,Ggplot2,假设我有数据帧d(dput)。我想用以任务和事件为分面的填充条绘制它,并用组成每个条的值的数量进行注释 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

假设我有数据帧
d
dput
)。我想用以
任务
事件
为分面的填充条绘制它,并用组成每个条的值的数量进行注释

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()
中排列
标签
向量

如果我只使用
facet\u grid()
,那么我会得到facetting:

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

如果我只使用
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)

但是我不知道如何设置
annotate()
label
参数来使用
facet\u 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"))

您必须对数据进行预处理,以制作带有刻面变量的geom_文本层

 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))
库(plyr)

d2注释不允许面板特定的注释;你必须使用geom层。你的代码没有运行,大多数R编辑器都需要将
dput
输出包装起来才能工作。@baptiste,有没有办法使用
geom_text()
这样的方法,使数据框中的每一行都不会将相同的文本堆叠在其自身的顶部@hrbrmstr,我知道第一个情节不起作用,这是我的问题代码。感谢您让我了解
dput
问题。更正。为批注创建一个新的数据框,每个批注一行。这样就不会与
geom_text
@Gregor叠加,如果您可以将其扩展为一个答案,这将非常有用。