11

JD Long 帮我解决了这个问题:关于手动注释的问题。

但是是否可以在多面图上做类似的事情,例如标签样式对应于线条样式(美学)并且我可以单独注释不同的方面?

一些数据:

funny <- structure(list(Institution = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("Q-branch", 
"Some-Ville", "Spectre"), class = "factor"), Type = structure(c(5L, 
6L, 1L, 3L, 5L, 6L, 2L, 4L, 5L, 6L, 2L, 4L, 5L, 6L, 2L, 4L, 5L, 
6L, 2L, 4L, 5L, 6L, 2L, 4L, 5L, 6L, 2L, 4L, 5L, 6L, 2L, 4L, 5L, 
6L, 2L, 4L, 5L, 6L, 2L, 4L, 5L, 6L, 2L, 4L, 5L, 6L, 2L, 4L), .Label = c("Korte videregående uddannelser", 
"Mammas beer", "Mellemlange videregående uddannelser", "Tastes good", 
"Unknown", "Your"), class = "factor"), År = c(2008L, 2008L, 
2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 
2008L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L), Mndr = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 27L, 27L, 27L, 
27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L), Data = c(159L, 
NA, NA, 23L, 204L, NA, NA, 12L, 256L, NA, NA, 24L, 166L, 6L, 
NA, 43L, 228L, NA, NA, 20L, 196L, 11L, NA, 37L, 99L, 14L, 9L, 
96L, 147L, 7L, 5L, 91L, 100L, 10L, 7L, 126L, 60L, 17L, 6L, 106L, 
78L, 18L, 13L, 140L, 48L, 23L, 5L, 136L)), .Names = c("Institution", 
"Type", "År", "Mndr", "Data"), class = "data.frame", row.names = c(NA, 
-48L))

还有一个多面的情节:

ggplot(funny, aes(Mndr, y=Data, group=Type, col=Type)) +
  geom_line() +
  facet_grid(.~Institution)

在此先感谢您的帮助!

4

1 回答 1

15

这个想法是,对于每个手动注释,您不仅必须定义标签,还必须定义定义面板、颜色等的所有变量。下面的代码在不同的面板中添加了两个标签。

pl <- ggplot(funny, aes(Mndr, y=Data, group=Type, col=Type))+geom_line()
      +facet_grid(.~Institution)   #your plot
nd <- data.frame(Institution=c("Q-branch","Some-Ville"),  #panel
                 Type=c("Unknown", "Tastes good"),        #color
                 Mndr=c(7,12),                            #x-coordinate of label
                 Data= c(170,50),                         #y-coordinate of label
                 Text=c("Label 1", "Label 2"))            #label text
# add labels to plot:
pl <- pl + geom_text(aes(label=Text), data=nd, hjust=0, legend=FALSE)
pl

legend=FALSE选项将确保表示文本的小 a 不会添加到图例中。您不必为标签设置一个数据框,您可以geom_text为每个标签设置一个单独的数据框,但我发现这种方式更简单。

于 2010-03-10T14:47:48.093 回答