I looked through manuals & did a brief search in this group, I
seem to be missing how to do something simple with ggplot / qplot.
Can someone help me with how I could graph 2 different columns of a
data frame? E.g.:
df <- data.frame(x=c(1:10), y1=c(10:1), y2=c(1:10))
qplot(df$x, df$y1, df$y2, geom="line")
I know this isn't right, and it doesn't work. But I don't know what
info I add to say that I have, in essence, two sets of y data. I also
know that I could put it all into 1 longer column & create a
categorical column "y1" & "y2". For reasons that aren't worth
deliberating upon here, that is not particularly easy to do with the
data set that I have.
How do I write something like the code above where I would have 2
separate lines, one that is a straight line with slope = -1 (y1) and
another with slope = +1 (y2).
Much obliged!!
Mike Williamson
I only ever use ggplot() because I can't keep qplot() straight
(strange but true).
ggplot(df,aes(x=x)) + geom_line(aes(y=y1)) + geom_line(aes(y=y2))
appears to work.
--
Ben Bolker
Associate professor, Biology Dep't, Univ. of Florida
*** NEW E-MAIL ADDRESSES:
*** bbo...@gmail.com , bol...@math.mcmaster.ca
bol...@ufl.edu / people.biology.ufl.edu/bolker
GPG key: people.biology.ufl.edu/bolker/benbolker-publickey.asc
The first to add a new layer for each additional column in your
data.frame.
df <- data.frame(x=c(1:10), y1=c(10:1), y2=c(1:10))
qplot(df$x, df$y1, geom="line") + geom_line(aes(y=df$y2))
The second way is to reshape your data into a long dataframe, as
opposed to a wide dataframe, and then using the group= parameter in
qplot:
library(reshape)
df <- data.frame(x=c(1:10), y1=c(10:1), y2=c(1:10))
mdf <- melt(df, id.vars="x")
qplot(x, value, group=variable, data=mdf, geom="line")
Andrie
Now if I may follow up with another question: I usually stay away
from ggplot (as opposed to qplot) because it seems less forgiving in
that it forces you to use a data frame more often. As I show above, I
like to call the "x" column df$x and provide it as a vector instead of
as just the data frame column name. There are 2 reasons why I
currently prefer qplot:
(1) It allows for more flexibility: I don't have to have all of my
columns of interest in the same data frame (or in a data frame at all)
(2) More importantly, it allows me to put the plot into a loop more
easily, which I am ALWAYS doing. E.g., I can write:
qplot(x=df$time, y=df[,i],...)
and put this in a loop through several of the columns that I want to
plot, for instance
But I cannot seem to get this to function well in ggplot, because I
need to provide the data frame & then put the NAME of the column in
the aes. Is there any way around this? If so, I will always use
ggplot, like the others above. E.g., is there any way to write:
ggplot(df, aes(x=time, y=eval(i))
or even better:
ggplot(aes(x=df$time, y=df[,i])) (ignoring the required dataframe
somehow)
I know that "eval" is not the right function for my call above. But
is there something that exists to fix this? I really like the ggplot2
package, it is amazing. But this reliance upon data frames &
especially upon column names is a serious Achilles' heel.
Thanks again!
Mike
On May 27, 4:03 am, Andrie de Vries <apdevr...@gmail.com> wrote:
> I also tend to use ggplot() rather than qplot(), for the same reason
> as Ben. However, there are two ways to achieve your objective using
> qplot().
>
> The first to add a new layer for each additional column in your
> data.frame.
>
> df <- data.frame(x=c(1:10), y1=c(10:1), y2=c(1:10))
> qplot(df$x, df$y1, geom="line") + geom_line(aes(y=df$y2))
>
> The second way is to reshape your data into a long datafram
Have a look at aes_string.
Hadley
> --
> 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
>
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
Thanks, it worked! I had tried aes_string in the past & had
trouble with it, then I forgot it over time. Maybe it was my mistake
back then...
Regards,
Mike
Using ggplot in a loop is quite easy with aes_string, or subsetting,
or a melted data.frame. Consider this example where we want to plot n
data.frames, n=1, 2, 3, and for each one we want to plot the n+1-th
column against x. Below are three solutions:
l <- replicate(3, data.frame(x = 1:10,
y1 = rnorm(10),
y2 = rnorm(10),
y3 = rnorm(10)), simplify=FALSE)
library(ggplot2)
p <- list()
for( ii in seq_along(l)){
yname <- names(l[[ii]])[ii+1]
p[[ii]] <- ggplot(l[[ii]]) +
geom_path(aes_string(x="x", y=yname))
}
p2 <- list()
for( ii in seq_along(l)){
tmp <- subset(l[[ii]], select=c(1, ii+1))
names(tmp) <- c("x", "y")
p2[[ii]] <- ggplot(tmp) +
geom_path(aes(x, y))
}
## test
## do.call(arrange, p2)
m <- melt(l, id="x")
p3 <- dlply(subset(m, as.numeric(variable) == L1), .(L1), function(d){
print( str(d))
ggplot(d) +
geom_path(aes(x, value))
})
## test
## do.call(arrange, p3)
HTH,
baptiste