plotting 2 dependent variables on same scatterplot

597 views
Skip to first unread message

Robert Strother

unread,
Nov 10, 2011, 9:24:37 AM11/10/11
to ggp...@googlegroups.com
I am certain there is a simple solution for this, but I cannot figure this out. 

I want to plot time against 2 dependent variables (in this case, a drug concentration and metabolite concentration), with the two variables presented in different colours.  

The data is structured as
ID   OCC  TIME  DT DTM
1     1          0      0.005 0.000
1      1         0.5   0.01   0.005   

This code allows me to plot the parent compound, by subject ID in a facet, but I cannot determine how to add metabolite
DTM1 <- subset(DTM_M,OCC==1)
DTM1_plot <- ggplot(DTM1, aes(Time, DT))
DTM1_plot+geom_point(colour="red")+facet_wrap(~ID)

any help appreciated

Matthew

Johannes Radinger

unread,
Nov 10, 2011, 10:59:25 AM11/10/11
to Robert Strother, ggp...@googlegroups.com
Hi,
I am not really sure if I understand you correctly,
but you want in each of the two facets plots points in
two different colors depending on your variable?

In this case you need probably a melted dataframe (have
a look at the melt function (in package reshape or reshape2).

Then your dataframe should look like:
ID time variable concentration

so all concentrations in one column and the column
variable states if it is the drug concentration or
the metabolite concentration.

Then you can use
ggplot(DTM1, aes(Time, DT), colour=variable)
So that the points for the drug concentration have a
different color then these for the metabolite conc.

But maybe you want something totally different and I
misunderstood you completely.

cheers
/j

-------- Original-Nachricht --------
> Datum: Thu, 10 Nov 2011 09:24:37 -0500
> Von: Robert Strother <rstr...@gmail.com>
> An: ggp...@googlegroups.com
> Betreff: plotting 2 dependent variables on same scatterplot

> --
> 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

--
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!
Jetzt informieren: http://www.gmx.net/de/go/freephone

Dennis Murphy

unread,
Nov 10, 2011, 1:29:43 PM11/10/11
to Robert Strother, ggp...@googlegroups.com
Hi:

Here's a small reproducible example to illustrate Johannes' point.

# Vars: subject, time, two response variables
df <- data.frame(subj = rep(1:4, each = 30),
time = rep(1:15, 8),
DT = rnorm(120),
DTM = 3 + rnorm(120))
library('ggplot2')

# melt() stacks the two response variables into a
# factor (variable) and its values (value): this makes it
# easier to plot in ggplot2 (and lattice, for that matter)
dfm <- melt(df, id = c('subj', 'time'))


# Use the new factor as the variable defining the color
# aesthetic and use the value variable as the response
ggplot(dfm, aes(x = time, y = value, colour = variable)) +
geom_point() +
labs(x = 'Time', y = 'Value', colour = 'Conc') +
scale_colour_manual(breaks = levels(dfm$variable),
values = c('blue', 'orange')) +
facet_wrap(~ subj, nrow = 2)

# Add fitted lines with SE envelopes to the graph:
ggplot(dfm, aes(x = time, y = value, colour = variable)) +
geom_point() +
geom_smooth(method = 'lm') +
labs(x = 'Time', y = 'Value', colour = 'Compound') +
scale_colour_manual(breaks = levels(dfm$variable),
values = c('blue', 'orange')) +
facet_wrap(~ subj, nrow = 2)

HTH,
Dennis

Robert Strother

unread,
Nov 10, 2011, 1:31:37 PM11/10/11
to Dennis Murphy, ggp...@googlegroups.com
thanks - works well for my needs.
Matthew
--
R. Matthew Strother, MD
Clinical Pharmacology
Medical Oncology
Reply all
Reply to author
Forward
0 new messages