I am looking for a way to display color rectangles in a ggplot2 background according to a factor level.
Here is a dput of the data :
structure(list(date = structure(c(6L, 8L, 3L, 4L, 5L, 7L, 1L,
2L), .Label = c("01/05/2012", "10/05/2012", "17/05/2011", "18/05/2011",
"19/05/2011", "20/12/2013", "24/04/2012", "26/12/2013"), class = "factor"),
Polyols = c(122, 5.25, 48.21, 41.59, 84.97, 64.75, 153.97,
49.14), Year = c(2013L, 2013L, 2011L, 2011L, 2011L, 2012L,
2012L, 2012L), Season = structure(c(2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("Spring", "Winter"), class = "factor"),
Site = structure(c(3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("GAP",
"OPE-ANDRA", "Peyrusse Vieille"), class = "factor"), Typology = structure(c(1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Rural", "Urban"), class = "factor")), .Names = c("date",
"Polyols", "Year", "Season", "Site", "Typology"), class = "data.frame", row.names = c(NA,
-8L))
What I'd like to get is a color rectangle behind each "typology" as shown in the picture below
I found This topic but couldn't find a way to adjust it to my case.
I tried
g <- ggplot(data) +
geom_bar(aes(x= Site, y= Polyols, fill= Season, colour= Season),
show.legend = TRUE,stat="identity",position= position_dodge()) +
theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0))
g <- g + geom_rect(data=data, (aes(xmin=Typology, xmax=Typology,
ymin=0,ymax=200,fill=factor(Typology))))
g
Could someone help ?

