[R] profile plot in R
Brian Diggs
diggsb at ohsu.edu
Mon Jun 20 21:56:13 CEST 2011
On 6/20/2011 11:25 AM, Vickie S wrote:
>
> Hi Dennis,
> It looks like something is wrong about configuration of ggplot dependency with plyr.Since i saw some threads about this particular error message.
> I tried several times by installing different versions of plyr but it did not work.
>
> ========
> sampledata= matrix(c(1.002, 1.76, 0.67, 0.99, 1.38, 1.0, 0.5, 0.78, 1.003,0.57, 0.99, 0.58, 0.76, 1.23, 1.45, .78,1.43, 1.34, 1.0, 0.9), ncol = 5, dimnames = list(c("cond1", "cond2","cond3", "cond4"), c("col1","col2", "col3", "col4", "col5")))
> sdata<- melt(sampledata) # melt method for matrices
> sdata$grp<- factor(c('grp1', 'grp1', 'grp2', 'grp3'))
> sdata$time<- rep(1:5, each = 4)
>
> library(ggplot2)
> h<- ggplot(sdata, aes(time, value))
> h + geom_point(aes(colour = cond), size = 2.5) + geom_line(aes(colour = cond), size = 1)
> Error in get("make_aesthetics", env = x, inherits = TRUE)(x, ...) :
> could not find function "empty"
>
>
> sessionInfo()
> R version 2.10.1 (2009-12-14)
> x86_64-apple-darwin9.8.0
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] grid stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] plyr_1.1 ggplot2_0.8.9 proto_0.3-8 reshape_0.8.3
>
> loaded via a namespace (and not attached):
> [1] tools_2.10.1
>
>
> Any suggestions ?
This works for me:
library("reshape")
library("ggplot2")
# note that the dimnames are now a named list, the names
# of which become the column names when melted.
sampledata <- matrix(c(1.002, 1.76, 0.67, 0.99, 1.38,
1.0, 0.5, 0.78, 1.003,0.57, 0.99, 0.58, 0.76,
1.23, 1.45, .78,1.43, 1.34, 1.0, 0.9),
ncol = 5,
dimnames = list(cond=c("cond1", "cond2","cond3", "cond4"),
time = c("col1","col2", "col3", "col4", "col5")))
sdata <- melt(sampledata) # melt method for matrices
# This works without error
ggplot(sdata, aes(time, value)) +
geom_point(aes(colour = cond), size = 2.5) +
geom_line(aes(colour = cond), size = 1)
# This might be what you meant, though. By default, the group
# aesthetic is an interaction of all the categorical variables
# (here, time and cond) and a separate thing (point or line) is
# drawn for each group. So there is only one point along each
# line (which doesn't make for much of a line). Override this to
# make one line per cond.
ggplot(sdata, aes(time, value, colour=cond)) +
geom_point(size = 2.5) +
geom_line(aes(group = cond), size = 1)
> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.5.2
loaded via a namespace (and not attached):
[1] digest_0.5.0
In particular, the most recent version of plyr is 1.5.2; I assume it
would work with R 2.10.1, but I don't know. You may need to install it
as a source package.
> Vickie S
>
> ----------------------------------------
>> Date: Mon, 20 Jun 2011 07:28:12 -0700
>> Subject: Re: [R] profile plot in R
>> From: djmuser at gmail.com
>> To: isvik at live.com
>> CC: jwiley.psych at gmail.com; r-help at r-project.org
>>
>> Hi:
>>
>> Typically a profile plot is a plot over time for each of n subjects
>> (aka a 'spaghetti plot'). Here's a revision of your code to yield a
>> profile plot in ggplot2, although it's not clear for what the grouping
>> variable is meant.
>>
>> sampledata= matrix(c(1.002, 1.76, 0.67, 0.99, 1.38, 1.0, 0.5, 0.78,
>> 1.003,0.57, 0.99, 0.58, 0.76, 1.23,
>> 1.45, .78,
>> 1.43, 1.34, 1.0, 0.9), ncol = 5,
>> dimnames = list(c("cond1", "cond2",
>> "cond3", "cond4"),
>> c("col1",
>> "col2", "col3", "col4", "col5")))
>> sdata<- melt(sampledata) # melt method for matrices
>> sdata$grp<- factor(c('grp1', 'grp1', 'grp2', 'grp3'))
>> sdata$time<- rep(1:5, each = 4)
>>
>> library(ggplot2)
>> h<- ggplot(sdata, aes(time, value))
>> h + geom_point(aes(colour = cond), size = 2.5) +
>> geom_line(aes(colour = cond), size = 1)
>>
>> Hope this is enough to get you started...
>>
>> Dennis
>>
>> On Mon, Jun 20, 2011 at 5:20 AM, Vickie S<isvik at live.com> wrote:
>>>
>>> Hi thanks for the help.
>>>
>>> Here is the error message i got. I could not figure out what goes wrong here.
>>>
>>> ======
>>> require(ggplots2)
>>> sampledata=data.frame(c(1.002, 1.76, 0.67, 0.99), c(1.38,1.0,0.5, 0.78), c(1.003,0.57,0.99, 0.58),c(0.76,1.23, 1.45, .78), c(1.43, 1.34, 1.0, 0.9))
>>> rownames(sampledata)=c("cond1", "cond2", "cond3", "cond4")
>>> colnames(sampledata)=c("col1", "col2", "col3", "col4", "col5")
>>> grp=c("grp1", "grp1", "grp2", "grp3")
>>> sampledata=cbind(sampledata, grp)
>>> long.dat=melt(sampledata[,1:5], idvars=as.character(rownames(sampledata)), measure.vars=as.character(colnames(sampledata)[1:5]), variable_name="cols")
>>>
>>> ggplot(long.dat, aes(x = cols, y = value)) +
>>> + geom_line(aes(colour = sampledata$grp)) +
>>> + facet_grid(~ rownames(sampledata))
>>> Error in get("make_aesthetics", env = x, inherits = TRUE)(x, ...) :
>>> could not find function "empty"
>>>
>>> =====
>>>
>>>
>>> thanks again,
>>> Vickie S
>>>
>>> ----------------------------------------
>>>> CC: r-help at r-project.org
>>>> From: jwiley.psych at gmail.com
>>>> Subject: Re: [R] profile plot in R
>>>> Date: Sat, 18 Jun 2011 08:24:01 -0700
>>>> To: isvik at live.com
>>>>
>>>> Hi,
>>>>
>>>> Another easy option would be to convert your data to "long" format and then use ggplot2 or lattice. Something like (though not exact without a reproducible example):
>>>>
>>>> require(ggplot2)
>>>> long.dat<- melt(your_data, arguments)
>>>>
>>>> ggplot(long.dat, aes(x = variable, y = value)) +
>>>> geom_line(aes(colour = group)) +
>>>> facet_grid(~ condition)
>>>>
>>>> HTH,
>>>>
>>>> Josh
>>>>
>>>> On Jun 17, 2011, at 12:43, Vickie S<isvik at live.com> wrote:
>>>>
>>>>>
>>>>> Hi friends,
>>>>>
>>>>> I have a matrix with following format.
>>>>>
>>>>> group var1 var2 .......varN
>>>>>
>>>>> c1 group1 1.2399 1.4990....-1.4829
>>>>>
>>>>> c2 group4 0.8989 0.7849.....1.8933
>>>>>
>>>>> ...
>>>>>
>>>>> ...
>>>>>
>>>>> c100 group10 .....
>>>>>
>>>>>
>>>>>
>>>>> I want to draw a profile plot
>>>>> of each condition c1 to c100, which rows in above matrix and each line
>>>>> representing a row should be uniquely colored according to the group(1
>>>>> to 10).
>>>>>
>>>>> I think this is simple task but I could not figure out how to set the colors.
>>>>>
>>>>>
>>>>>
>>>>> Any help would be appreciated.
>>>>>
>>>>>
>>>>>
>>>>> thanks
>>>>>
>>>>> -Vickie
>>>>> [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
More information about the R-help
mailing list