ggplot2: change background colour, depending on factor levels on a discrete x axis

1,681 views
Skip to first unread message

Ricardo Ramiro

unread,
Feb 21, 2013, 6:48:18 AM2/21/13
to ggp...@googlegroups.com
Dear all,

I have been trying to find a way in which I can partition the background of a plot into different sections depending on my factor levels.


in which they manage to do what I want for a continuous x axis, though I have not been able to apply this to my discrete x. If anyone has an idea of how to do it, I would greatly appreciate to get to know it.

Thanks!!

Ben Bond-Lamberty

unread,
Feb 21, 2013, 7:00:05 AM2/21/13
to ggp...@googlegroups.com
You should be able to use geom_rect, as that stackoverflow example
shows, but with a couple tweaks:

d <- data.frame(x=c("A","B","C"),y=c(1,2,3))

ggplot(d,aes(x,y)) +geom_blank() +
geom_rect(aes(xmin=as.numeric(x)-.5,xmax=as.numeric(x)+.5,ymin=-Inf,ymax=Inf,fill=x))
+ geom_bar(stat="identity")

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages