注释构面图的一部分

时间:2019-01-12 03:45:15

标签: r ggplot2

我试图注释剧情的一部分:

region<-    c('vic',    'vic',  'vic',  'sa',   'vic',  'vic',  'sa',    
'sa',   'vic',  'sa',   'sa',   'sa',   'vic',  'vic',  'sa',   'sa')
year<-  c(2010, 2010,   2010,   2010,   2011,   2011,   2011,   2011,    
2010,   2010,   2010,   2010,   2011,   2011,   2011,   2011)
diveLocation<-  c('Comp',   'Comp', 'Comp', 'Comp', 'Comp', 'Comp', 'Comp',  
'Comp', 'Lease',    'Lease',    'Lease',    'Lease',    'Lease',    'Lease',     
'Lease',    'Lease')
newy<-c('yes',  'yes',  'no',   'no',   'yes',  'no',   'yes',  'no',    
'no',   'yes',  'yes',  'yes',  'no',   'no',   'yes',  'yes')

df<- data.frame(region, year,   diveLocation,   newy)

给出以下数据框:

region   year   diveLocation  newy
<fctr>   <fctr>  <fctr>       <fctr>

  vic   2010    Comp       yes  
  vic   2010    Comp       yes  
  vic   2010    Comp       no   
  sa    2010    Comp       no   
  vic   2011    Comp       yes  
  vic   2011    Comp       no   
  sa    2011    Comp       yes  
  sa    2011    Comp       no   
  vic   2010    Lease      no   
  sa    2010    Lease      yes
  sa    2010    Lease      yes  
  sa    2010    Lease      yes  
  vic   2011    Lease      no   
  vic   2011    Lease      no   
  sa    2011    Lease      yes  
  sa    2011    Lease      yes

情节代码:

 t<-df%>%
 ggplot(aes(x=region, fill = newy)) +
 geom_bar(stat = 'Count', position = 'stack') + 
 facet_grid(diveLocation~year) + 
  guides(fill=guide_legend(title="Levels")) +
  coord_cartesian(ylim=c(0, 10)) + #optional line
  theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + 
 xlab("Region") + ylab("Count") + ggtitle("Noncompliance at both")

 t

与情节

facet plot

我现在想在其中一个方面放置一些文本,并找到其他示例,但无法使用。例如:

 ann_text <- data.frame(y = 1,x = 2,lab = "Text",
                   year = factor(2011,levels = c('2010','2011'),
                                 diveLocation = factor(Lease, levels = 
c('Comp', 'Lease'))))

t+ann_text

但是出现以下错误: 因子误差(2011,等级= c(“ 2010”,“ 2011”),潜水位置=因子(租赁,:   未使用的参数(diveLocation = factor(Lease,level = c(“ Comp”,“ Lease”)))

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您在ann_text的定义中有错字。您还需要向图中添加一个额外的几何层。这是一个入门的示例:

library(ggplot2)
library(magrittr, warn.conflicts = FALSE)
region<-    c('vic',    'vic',  'vic',  'sa',   'vic',  'vic',  'sa',    
              'sa',   'vic',  'sa',   'sa',   'sa',   'vic',  'vic',  'sa',   'sa')
year<-  c(2010, 2010,   2010,   2010,   2011,   2011,   2011,   2011,    
          2010,   2010,   2010,   2010,   2011,   2011,   2011,   2011)
diveLocation<-  c('Comp',   'Comp', 'Comp', 'Comp', 'Comp', 'Comp', 'Comp',  
                  'Comp', 'Lease',    'Lease',    'Lease',    'Lease',    'Lease',    'Lease',     
                  'Lease',    'Lease')
newy<-c('yes',  'yes',  'no',   'no',   'yes',  'no',   'yes',  'no',    
        'no',   'yes',  'yes',  'yes',  'no',   'no',   'yes',  'yes')

df<- data.frame(region, year,   diveLocation,   newy)
#You need to pass in values for all the aesthetics used in the main plot, i.e.
#region, newy, diveLocation, year.
ann_text <- data.frame(diveLocation = "Lease",
                       year = 2011,
                       newy = "yes",
                       x = "vic",
                       y = 5,
                       label = "your text here")


df%>%
  ggplot(aes(x=region, fill = newy)) +
  geom_bar(stat = 'Count', position = 'stack') + 
  facet_grid(diveLocation~year) + 
  guides(fill=guide_legend(title="Levels")) +
  coord_cartesian(ylim=c(0, 10)) + #optional line
  theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + 
  xlab("Region") + ylab("Count") + ggtitle("Noncompliance at both") +
  geom_text(data = ann_text, aes(x = x, y = y, label = label))

reprex package(v0.2.1)于2019-01-11创建