plotting with lines

68 views
Skip to first unread message

Jack Monflower

unread,
Jul 18, 2009, 8:56:16 PM7/18/09
to ggp...@googlegroups.com
Hello everybody,

I have the following R code:

===========================================
library("ggplot2")
c_len <- c(1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 );
c_dep1 <- c(0 ,0 ,0.5 ,0.55,0.94,1.13,1.568,1.8432,2.10,2.355);
c_dep2 <- c(0 ,0 ,0.58,0.62,0.91,1.15,1.57,1.799,2.007,2.08);
c_dep3 <- c(0 ,0 ,0.42,0.93,1.42,1.864,2.25,2.610,2.9,3.23);

df <- data.frame(Length = as.factor(c_len), A=c_dep1, B=c_dep2, C=c_dep3);
dfm <- melt(df, id="Length", measure=c("A", "B", "C"));

p <- qplot(Length, value, data=dfm, colour=variable, geom="line")
## adding geom="line" does not work
print(p)
==========================================

Whatever I do to draw lines (using geom="line") instead of dots, does not work. Instead I get an empty
plot. If I don't use geom="line" at all, then everything is shown correctly, only
with dots, which is what I don't want. Is there a way to show a line between all
data points (with circles on the data points themselves?)
My script followed an example from the web from Hadley, but it does not work
for me. I work with R version 2.9.

Another point: I want to use this plot in a slide. I would like it to be quite
"fancy". One way I thought it could be done is by using thick lines surrounded
with some border instead of just plain thin line and circles on the lines).
Is there a way to do it?

Thanks!

smu

unread,
Jul 19, 2009, 4:32:11 AM7/19/09
to ggp...@googlegroups.com
hey,

On Sat, Jul 18, 2009 at 08:56:16PM -0400, Jack Monflower wrote:
> Hello everybody,
>
> I have the following R code:
>
> ===========================================
> library("ggplot2")
> c_len <- c(1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 );
> c_dep1 <- c(0 ,0 ,0.5 ,0.55,0.94,1.13,1.568,1.8432,2.10,2.355);
> c_dep2 <- c(0 ,0 ,0.58,0.62,0.91,1.15,1.57,1.799,2.007,2.08);
> c_dep3 <- c(0 ,0 ,0.42,0.93,1.42,1.864,2.25,2.610,2.9,3.23);
>
> df <- data.frame(Length = as.factor(c_len), A=c_dep1, B=c_dep2, C=c_dep3);
> dfm <- melt(df, id="Length", measure=c("A", "B", "C"));
>
> p <- qplot(Length, value, data=dfm, colour=variable, geom="line")
> ## adding geom="line" does not work
> print(p)
> ==========================================
>
> Whatever I do to draw lines (using geom="line") instead of dots, does not
> work. Instead I get an empty

You use a factor variable for the x axis, because of this the space
between two values is not defined. Remove the as.factor and everything
works as expected.

>...


> Another point: I want to use this plot in a slide. I would like it to be
> quite
> "fancy". One way I thought it could be done is by using thick lines
> surrounded
> with some border instead of just plain thin line and circles on the lines).
> Is there a way to do it?

Maybe one of the themes in the ggplot-add-ons package?

http://r-forge.r-project.org/projects/ggplot-add-ons/


regards,
stefan

baptiste auguie

unread,
Jul 19, 2009, 7:24:17 AM7/19/09
to Jack Monflower, ggp...@googlegroups.com
Hi,

I'm not sure why, but it seems that line and path geoms don't like the
x-axis to be a factor. The following works for me,

library("ggplot2")
c_len <- c(1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 );
c_dep1 <- c(0 ,0 ,0.5 ,0.55,0.94,1.13,1.568,1.8432,2.10,2.355);
c_dep2 <- c(0 ,0 ,0.58,0.62,0.91,1.15,1.57,1.799,2.007,2.08);
c_dep3 <- c(0 ,0 ,0.42,0.93,1.42,1.864,2.25,2.610,2.9,3.23);

df <- data.frame(Length = as.factor(c_len), A=c_dep1, B=c_dep2, C=c_dep3);
dfm <- melt(df, id="Length", measure=c("A", "B", "C"));

p <- qplot(Length, value, data=dfm, colour=variable)
## adding geom="line" does not work
p + geom_line(aes(x=as.numeric(Length)), size=2)

I'm not sure what you meant by your "fancy" tweaks, do you want
something like type = 'b' in base graphics?

If so, the following trick might be useful,

p <- ggplot(data=dfm)

p +
geom_line(aes(x=as.numeric(Length), y=value, colour=variable), size=2) +
geom_point(aes(x=as.numeric(Length), y=value), colour="white", size=4) +
geom_point(aes(x=as.numeric(Length), y=value, colour=variable)) +
theme_bw() +
xlab("Length")

HTH,

baptiste
--
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

baptiste auguie

unread,
Jul 19, 2009, 7:32:55 AM7/19/09
to ggp...@googlegroups.com
Oops, I somehow missed smu's reply.

If you're looking after a custom theme and come up with a new design,
please do contribute to ggextra,

http://code.google.com/p/ggextra/

It would be great to build up a collection of themes for different
presentation and publication requirements. If one uses, say, Beamer
for presentations, we could imagine having colour palettes that match
the Beamer theme.

baptiste

hadley wickham

unread,
Jul 20, 2009, 8:00:48 AM7/20/09
to baptiste auguie, Jack Monflower, ggp...@googlegroups.com
On Sun, Jul 19, 2009 at 6:24 AM, baptiste auguie<bapt...@googlemail.com> wrote:
>
> Hi,
>
> I'm not sure why, but it seems that line and path geoms don't like the
> x-axis to be a factor. The following works for me,

The default grouping in ggplot2 is defined by the combination of all
categorical variables in the plot. This works well most of the time,
but if you are drawing lines and have a categorical variable on the x
axis, you will need to manually specify the grouping:

qplot(Length, value, data=dfm, colour=variable, geom="line", group = variable)

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages