There are at least two things wrong here, only one of which has to do
with ggplot2.
# (1):
mydata<- data.frame(cbind(FactorA,FactorB,YearQtr))
You want YearQtr to be continuous (i.e., numeric), but you input it as
a character string. cbind() in this case will create a matrix of
character strings, since in a matrix, all elements must be of the same
mode. Wrapping data.frame around this converts it to a data frame, but
since you didn't use the option stringsAsFactors = FALSE, everything
gets read in as a factor. Observe:
> mydat<- data.frame(cbind(FactorA,FactorB,YearQtr))
> str(mydat)
'data.frame': 10 obs. of 3 variables:
$ FactorA: Factor w/ 3 levels "Away","Home",..: 2 1 2 3 2 2 1 3 2 3
$ FactorB: Factor w/ 2 levels "Group1","Group2": 1 2 2 2 2 2 2 1 2 1
$ YearQtr: Factor w/ 10 levels "2009","2009.25",..: 10 9 8 7 6 5 4 3 2 1
This is why you got the error message when you tried to use
scale_y_continuous(); as you have it, YearQtr is a factor, not a
numeric variable.
This is better for the purposes you intend:
mydata<- data.frame(FactorA, FactorB, YearQtr = as.numeric(YearQtr))
> str(mydata)
'data.frame': 10 obs. of 3 variables:
$ FactorA: Factor w/ 3 levels "Away","Home",..: 2 1 2 3 2 2 1 3 2 3
$ FactorB: Factor w/ 2 levels "Group1","Group2": 1 2 2 2 2 2 2 1 2 1
$ YearQtr: num 2011 2011 2011 2010 2010 ...
# (2)
In the call to generate the rectangle, you need to distinguish between
aesthetics that are mapped (variables) and those that are set to
single values (parameters/constants). In particular, you needed
geom_rect(aes(xmin=1, xmax=2, ymin=2009.5, ymax=2010.25), fill="gray")
Putting this all together, the following may be what you're after:
mydata<- data.frame(FactorA, FactorB, YearQtr = as.numeric(YearQtr))
# Create the plot with ggplot2
my.gg <- ggplot(mydata) + theme_bw(11) +
geom_point(aes(FactorA,YearQtr,colour=factor(FactorB)),size=4) +
scale_y_continuous(breaks=c(2008,2009,2010,2011)) +
scale_color_manual(name="Group",values=c("darkolivegreen4","darkorange")) +
geom_rect(aes(xmin=1, xmax=2, ymin=2009.5, ymax=2010.25),
fill="gray", alpha = 0.5) +
opts(title = "View of Something Over Time") +
opts(axis.text.x=theme_text(angle=90, hjust=1))
my.gg
It's not necessary to rotate the x-labels in this example - you don't
want your audience to strain to read the labels without need. Perhaps
your real example has many more levels and the rotation avoids
overplotting. I also inserted alpha transparency into the rectangle so
that the points are at least partially visible within it.
HTH,
Dennis
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
The problem is that x (FactorA) is a factor, so is not a continuous
variable. To precede geom_point() by geom_rect(), x has to be numeric
because you set xmin = 1 and ymin = 2. The error message doesn't occur
if geom_point() precedes geom_rect() because in that case, 1 is
associated with the first level of FactorA and 2 with the second level
of FactorA. In ggplot2, all the layers (and their scales) have to be
coordinated; usually, the order of layers doesn't matter that much,
but sometimes it does, and this is one such case, in large part
because you're trying to mix factors and numerics in the same scale.
One way around this is to make both x and y continuous scales. To do
this, we convert FactorA to numeric and use scale_x_continuous to
relabel at the appropriate values:
my.gg <- ggplot(mydata) + theme_bw(11) +
scale_y_continuous(breaks=c(2008,2009,2010,2011)) +
geom_rect(aes(xmin=1, xmax=2, ymin=2009.5, ymax=2010.25), fill="gray") +
geom_point(aes(as.numeric(FactorA), YearQtr, colour=FactorB), size=4) +
scale_color_manual(name="Group",values=c("darkolivegreen4","darkorange")) +
scale_x_continuous(breaks = 1:3, labels = c('Away', 'Home', 'Neutral')) +
labs(title = "View of Something Over Time", x = 'FactorA') +
opts(axis.text.x=theme_text(angle=90, hjust=1))
my.gg
HTH,
Dennis