Few questions on adding styles in my ggplot

486 views
Skip to first unread message

Christofer Bogaso

unread,
Dec 16, 2012, 4:21:46 AM12/16/12
to ggp...@googlegroups.com
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

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)

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.

I also want to have some Background color and Border for legend-area

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)

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.

Ista Zahn

unread,
Dec 16, 2012, 10:00:26 AM12/16/12
to Christofer Bogaso, ggp...@googlegroups.com
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

Christofer Bogaso

unread,
Dec 19, 2012, 1:43:43 PM12/19/12
to Ista Zahn, ggp...@googlegroups.com
Hello Ista,

Sorry for this late reply. And thank you so much for your helpful
reply. All of your points helped to solve my problem.

Thanks and regards,

Christofer Bogaso

unread,
Dec 25, 2012, 11:09:36 AM12/25/12
to Ista Zahn, ggp...@googlegroups.com
Hello Ista,

I happened to know that, there is no option to put transparency in the
Legend's background. I basically want to put some sort of transparency
in the legend background color so I tried following however got error:

library(scales)
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)); 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)


p + theme(legend.position=c(.2, .8), legend.background =
element_rect(fill="green", color="blue", alpha = 0.5))

Is there any option to put transparency there?

Thanks for your input.

Happy Christmas!



On Sun, Dec 16, 2012 at 8:45 PM, Ista Zahn <ista...@gmail.com> wrote:
> (legend.position=c(.2, .8)

Brandon Hurr

unread,
Dec 25, 2012, 11:42:51 AM12/25/12
to Christofer Bogaso, Ista Zahn, ggplot2
Not sure what exactly you're after, but you can supply the alpha as part of the color. 

p+theme(legend.position=c(.2, .8), legend.background =
element_rect(fill="green", color=alpha("blue", alpha=0.5)))

Christofer Bogaso

unread,
Dec 25, 2012, 11:48:11 AM12/25/12
to Brandon Hurr, Ista Zahn, ggplot2
Hello Brandon,

You are done almost! I just needed below:

p+theme(legend.position=c(.2, .8), legend.background =
element_rect(fill=alpha("green", alpha = 0.3)))

Thanks for your pointer.

Happy Christmas!
Reply all
Reply to author
Forward
0 new messages