Reputation: 11
How do I get rid of the straight line from the first and last points? Why aren't the lines colored? Is there a better way to make a line plot, I get a similar problem with ggplot2.
dat<-Data_for_Analysis_1_
dat$Enrichment<-factor(dat$Enrichment)
plot(TF~Minute, data=dat, col=Enrichment, pch=20,xlab="Minute",
ylab="No. of Tongue Flicks", cex.lab=1.5, cex.axis=1.5,
cex.main=1.5, cex.sub=1.6)
lines(TF~Minute, data=dat, col=Enrichment)
Upvotes: 1
Views: 745
Reputation: 11
Thanks guys,
You didn't answer my question directly but you informed me about the group function.
I went to excel and made another column labeled "Instance" where each individual observation was labelled as 1, 2 ,3 etc and then grouped the data by that.
ggplot(dat, aes(x=Minute, y=TF, group=Instance , col=Enrichment)) + geom_line() + geom_point() + labs(y="No. of Tongue Flicks")
Just got to give it a more appropriate Key.
Thanks again, the replies are really fast and extensive.
Upvotes: 0
Reputation: 10223
Assuming your data looks like the toy data I create below, the following should do it:
# Create some toy data
m <- 10
dat <- data.frame(Minute = rep(0:60, m))
dat$Enrichment <- rep(LETTERS[1:m], each = 61)
dat$TF <- c(replicate(m, cumsum(rnorm(61, mean = 0.3))))
head(dat)
# Minute Enrichment TF
#1 0 A 0.3203584
#2 1 A 0.9571599
#3 2 A 1.5361236
#4 3 A 1.7571507
# ...
#60 59 A 19.25409068
#61 60 A 20.68664549
#62 0 B -0.05674056
#63 1 B 0.64608473
## And so on...
# Your code:
dat$Enrichment <- factor(dat$Enrichment)
plot(TF ~ Minute, data=dat, col=Enrichment, pch=20,xlab="Minute",
ylab="No. of Tongue Flicks", cex.lab=1.5, cex.axis=1.5,
cex.main=1.5, cex.sub = 1.6)
# Draw line for each group (could also be done with a for-loop)
.tmp <- lapply(split(dat, dat$Enrichment),
function(d) lines(TF ~ Minute, data=d, col=Enrichment))
I suspect your problem is that the time and TF
vectors you pass to lines
is interpreted as two long unbroken vectors of values. As such, it "resets" after each new group as the your code basically ignores your the grouping variable.
Upvotes: 1