Reputation: 199
I am trying to annotate one section of a plot:
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)
which gives the following data frame:
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
the plot code:
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
with the plot
I now want to place some text in one of the facets and have found others examples but can't get to work. For example:
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
But get the following error: Error in factor(2011, levels = c("2010", "2011"), diveLocation = factor(Lease, : unused argument (diveLocation = factor(Lease, levels = c("Comp", "Lease")))
Any help is appreciated
Upvotes: 1
Views: 63
Reputation: 69201
You have a typo in your definition of ann_text
. You also need to add an additional geom layer to the plot. Here's an example to get you started:
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))
Created on 2019-01-11 by the reprex package (v0.2.1)
Upvotes: 1