Hi Chris,
Please see inline below.
On Sun, Dec 16, 2012 at 4:21 AM, Christofer Bogaso
<
bogaso.c...@gmail.com> wrote:
> Dear all, Let say I have following plot
>
> Dat <- structure(list(Date = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
> 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("12-03-2012", "12-04-2012",
> "12-05-2012", "12-06-2012", "12-07-2012"), class = "factor"),
> OBJ = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
> 3L, 3L, 3L, 3L, 3L), .Label = c("OBJ 1", "OBJ 2", "OBJ 3"
> ), class = "factor"), Time = c(11, 11.25, 11.35, 12.01, 11.14,
> 13, 13.25, 13.35, 14.01, 13.14, 14.5, 14.75, 14.85, 15.51,
> 14.64)), .Names = c("Date", "OBJ", "Time"), class = "data.frame",
> row.names = c(NA,
> -15L));
>
> p <- ggplot(Dat) +
> geom_line(aes(y = Time, x = as.Date(Dat$Date, "%m-%d-%Y"), color =
> OBJ), size = 3) +
> scale_x_date(labels = date_format("%m-%d-%Y")) +
> ylab(NULL) + xlab(NULL) +
> scale_y_continuous(limits = c(10, 17))
> p
I've rearranged this a little to make things easier later on:
Dat$Date <- as.Date(Dat$Date, "%m-%d-%Y")
p <- ggplot(Dat, aes(y = Time,
x = Date,
color = OBJ)) +
geom_line(size = 3) +
scale_x_date(labels = date_format("%m-%d-%Y")) +
ylab(NULL) + xlab(NULL)
>
> However I would like to have following:
>
> 1. In the Y-axis, instead of showing 10, 12, 14 etc, I would like to
> have 10.00, 12.00, 14.00 etc (i.e. 2 significant digits)
You can format labels with arbitrary function, e.g.
(p <- p + scale_y_continuous(labels=function(x) sprintf("%1.2f", x)))
>
> 2. I would like to readjust the Legend's position. Instead of outside
> the plot-canvas, I want to have inside it. For this I tried following:
>
> p + guides(guide_legend(label.vjust = .5))
>
> However nothing has happened.
Legend position is set by the theme:
(p <- p + theme(legend.position=c(.2, .8)))
As an aside, for this kind of plot I recommend directlabels:
library(directlabels) #may need to install first
direct.label(p)
>
> I also want to have some Background color and Border for legend-area
Also handled by theme (see ?theme for details)
(p <- p + theme(legend.background = element_rect(fill="green", color="blue")))
>
> 3. I also add one rectangle along the left-most place. Therefore I
> tried following:
>
> p + annotate("rect", xmin = -Inf, xmax = as.Date(Dat$Date,
> "%m-%d-%Y")[1], ymax = Inf, ymin = -Inf)
>
There are a couple of problems here. First (and the likely source of
your error) is that Inf and -Inf are not dates. Even once we get past
that, there we're going to have a problem because values outside of
limits set by scales are discarded. I think you'll have to set the
limits on the coordinates instead. Here is my initial attempt:
(p <- p + annotate("rect",
xmin = min(Dat$Date - 10),
xmax = min(Dat$Date),
ymax = max(Dat$Time + 10),
ymin = min(Dat$Time - 10)) +
coord_cartesian(xlim=c(min(Dat$Date) - 2, max(Dat$Date) + .1),
ylim=c(min(Dat$Time) -.5, max(Dat$Time) + .5))
)
Best,
Ista
> However with this I am having following error:
> Error: Invalid input: date_trans works with objects of class Date only
>
> Can somebody help me how to achieve these goals.
>
> Thanks for your help.
>
> --
> 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