手动使用不同标签在不同标签上标注具有不同标签的ggplot [英] Manual annotate a ggplot with different labels, in different facets

查看:119
本文介绍了手动使用不同标签在不同标签上标注具有不同标签的ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JD Long帮助我:关于手动注释的问题

但是有可能在一个小平面图上做类似的事情,例如标签样式对应于linestyle(aestetics),并且可以用不同的方式来注释有些数据:

 有趣<  - 结构 (清单(机构=结构(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,3L,3L,3L,1L,1L,1L,1L,1L,2L,2L,2L,2L,3L,3L,3L,1L,1L,1L, ,2L,2L,2L,2L,3L,3L,3L,3L),标签= c(Q分支,
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(Kortevideregåendeuddannelser,
Mammas beer,Mellemlangevideregåendeuddannelser,味道很好,
未知,您的),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,2007L, ,2006L,2006L,2006L,2006L,2006L,2006L,2006L,2006L,2006L,
2006L),Mndr = c(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)

感谢您的帮助! 解决方案

不仅要定义标签,还要定义面板,颜色等的所有变量。以下代码在不同的面板中添加了两个标签。

  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),#面板
Type = c(未知,品味良好),#color
Mndr = c(7,12),#标签的x坐标
Data = c(170,50),#标签的y坐标
Text = c(Label 1,Label 2 ))#标签文本
#添加标签到图:
pl < - pl + geom_text(aes(label = Text),data = nd,hjust = 0,legend = FALSE)

legend = FALSE 选项会确保表示文本的小a不会添加到图例中。您不必为标签设置数据框,您可以为每个标签分别设置 geom_text ,但我觉得这样更简单。


JD Long helped me with this: question about manual annotation.

But is it possible to do something similar on a facetted plot, such that the label style corresponds to the linestyle (aestetics) and in a way that I can annotate different facets individually?

Some data:

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))

And a facetted plot:

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

Thanks in advance for your help!

解决方案

The idea is that for each manual annotation you have to define not only the label, but all the variables that define the panel, color, etc. The following code adds two labels in different panels.

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

The legend=FALSE option will ensure that the small a's denoting the text are not added to the legend. You don't have to have a data frame for the labels, you could have a separate geom_text for each, but I find this way simpler.

这篇关于手动使用不同标签在不同标签上标注具有不同标签的ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆