[R] plot function creating bars instead of lines
R. Michael Weylandt
michael.weylandt at gmail.com
Tue Apr 24 02:04:34 CEST 2012
It is indeed the fact you're plotting factors, but unless you say what
"as intended" is, it's hard to provide exactly what you're seeking.
Perhaps this will help though:
X <- factor(sample(letters[1:5], 15, TRUE))
Y <- rnorm(15)
dats <- data.frame(X, Y)
plot(Y ~ X, data = dats) # No good
plot(X ~ Y, data = dats) # Also probably not what you want
plot(Y ~ as.numeric(X), data = dats) # Good but ugly lables
plot(Y ~ as.numeric(X), data = dats, xaxt = "n", xlab = "X")
axis(1, at = seq_along(levels(X)), labels = levels(X)) # Good
But perhaps easier is
library(ggplot2)
qplot(X,Y, dats)
Michael
On Mon, Apr 23, 2012 at 11:25 AM, la mer <melissarosenkranz at gmail.com> wrote:
> Hello,
>
> I am having a problem where code that plots lines using a different data
> frame plots bars with the current data frame (I am intended to plot lines).
> The code specifies lines (see below), so I can't figure out why the results
> are bars. I suspect that it may have something to do with the fact that in
> the data frame where the code worked as intended, the both variables
> specifying different lines were numeric, whereas in the current data frame
> one of those variables (challenge) is a factor with 2 levels. Any
> suggestions for getting this to plot as intended would be much appreciated.
>
> Thank you!
>
> ************ This is meant to plot a separate line for each subject for each
> challenge*************
> for (subj in unique(lab.samples$subid)) {
> #par(new=T)
> plot.new()
> par(mfrow=c(2,1))
> par(mfg=c(1,1))
> plot(data=lab.samples, subset=(subid==subj), cortisol ~ Sample, type='n',
> main=paste('Cortisol and Amylase for subject ', as.character(subj)))
>
> for ( t in unique(subset(lab.samples,subid==subj)$challenge) ) {
> par(mfg=c(1,1))
> lines(data=lab.samples, subset=(subid==subj & challenge==t),
> cortisol ~ Sample, type='b', pch=as.character(t), col=rainbow(2)[t])
> }
> par(mfg=c(2,1))
> plot(data=lab.samples, subset=(subid==subj), amylase ~ Sample, type='n')
> for ( t in unique(subset(lab.samples,subid==subj)$challenge) ) {
> par(mfg=c(2,1))
> lines(data=lab.samples, subset=(subid==subj & challenge==t),
> amylase ~ Sample, type='b', pch=as.character(t), col=heat.colors(2)[t])
> }
> }
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/plot-function-creating-bars-instead-of-lines-tp4580765p4580765.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list