Background graph colours based on a factor value

49 views
Skip to first unread message

Rob

unread,
Nov 24, 2015, 6:22:22 AM11/24/15
to ggplot2
Hi there, 

I'm sure this is a simple task, but I'm a little lost with this task. 

I need to create a background colour for my line graph from the financial year 2011/ 2012 on-wards. I essentially want this period of time on-wards to have a light red background colour.  

Below is what I have to create the attached line graph. 

library(ggplot2)

FA_2 <- ggplot(COPP_Stats, aes(x=Fin_Year, y=Total_FA, group=Location, colour=Location))
FA_2 + geom_line(size=1))

The financial year stats are in the data table as  2007 / 2008, 2008 / 2009 etc. until 2014 / 2015.

Any help would be greatly appreciated. 

Regards

Rob
line_output.pdf

Roman Luštrik

unread,
Nov 24, 2015, 7:08:55 AM11/24/15
to Rob, ggplot2
Hi,

geom_polygon can be used to do this. Here are some examples for a bit different task, but the logic is the same.


Cheers,
Roman

--
--
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/d/optout.



--
In God we trust, all others bring data.

Dennis Murphy

unread,
Nov 24, 2015, 8:09:47 AM11/24/15
to Rob, ggplot2
I'm guessing you have something like this in mind, but we'll see.
Using some fake data as a reproducible example:

library(ggplot2)

# fake data
DF <- data.frame(year = rep(2006:2015, 3),
grp = factor(rep(LETTERS[1:3], each = 10)),
y = rpois(30, 5))

ggplot(DF, aes(x = year, y = y, color = grp)) +
theme_bw() +
geom_rect(xmin = 2012, xmax = Inf, ymin = -Inf, ymax = Inf,
fill = "red", alpha = 0.01, show_guide = FALSE) +
geom_path(size = 1) +
scale_color_manual(values = c("firebrick", "blue", "darkorange")) +
scale_x_continuous(breaks = seq(2006, 2014, by = 2))

geom_rect() tends to look better with a white background IMO, which is
why I included theme_bw(). The background rectangle should also be
drawn first so that the lines are drawn over the rectangle and not
vice versa. For some reason, you need to set a very low value for
alpha transparency in order to see the background grid lines. The
argument show_guide = FALSE avoids drawing diagonal lines in the
legend keys.

If you want to set tighter bounds for the rectangle, change the values
of the arguments. The Inf and -Inf values allow you to extend the
rectangle to the edge of the plot region without affecting the axis
limits in each direction.

Dennis

Rob

unread,
Nov 24, 2015, 7:21:14 PM11/24/15
to ggplot2, roberta...@gmail.com
Excellent Dennis - It certainly did the trick. Thank you both very much!!!
Reply all
Reply to author
Forward
0 new messages