[英]How to draw multiple lines on a single plot in R all with different colours?
I have a data set that consists of cumulative counts of three species of fish for successive transects so this consists of 3 columns (species, count, transect); 我有一个数据集,该数据集包含连续断面的三种鱼类的累积计数,因此它由3列(种类,计数,断面)组成;
eg 例如
Species Count Transect
Cod 25 1
Cod 36 2
Cod 45 3
Haddock 12 1
Haddock 23 2
Haddock 34 3
Etc… I'm likely to have lots of species that I want to plot in a single plot so I have tried to use a loop (my first go at properly trying to use loops!!) to speed this up and have done the following 等等...我可能想在一个图中绘制很多物种,因此我尝试使用循环(我首先尝试正确地使用循环!)加快了速度,并做了以下工作
xrange <- range(Data$Transect)
ymax <- max(Data$Count)
plot(xrange,y=c(0,ymax+10), yaxs = "i", type="n", las=1, xlab="Transect Number", ylab="Total Number")
for (i in Data$Species){
species <- subset(Data, Species==i)
lines(species$Transect, species$Count, type="o",lwd=2,lty=1,pch=NA)}
This produces a plot with all the species in the data set with a separate line but I would like each of these lines to have a unique colour and am struggling to do this. 这会产生一个图,其中包含数据集中的所有物种,并使用单独的线条,但是我希望这些线条中的每一个都具有唯一的颜色,并且正为此而努力。
I have tried the following but it doesn't work and all of the lines still come out as the same colour and I'm not sure what to try next? 我已经尝试了以下方法,但是它不起作用,所有行仍然都显示为相同的颜色,我不确定下一步该怎么做?
nspecies <- levels(Data$Species)
colors<- rainbow(nspecies)
for (i in Data$Species) {
species <- subset(Trial, Species==i)
lines(species$Haul, species$Catch, type="o",lwd=2,lty=1,pch=NA, col=colors[i])}
Any help would be great thanks! 任何帮助将非常感谢!
The problem is that with each call to lines()
, you are applying the same colour. 问题在于,每次调用
lines()
,您将应用相同的颜色。 To prevent this, you would want to iterate over a vector of colours each time you call lines()
. 为了避免这种情况,您每次调用
lines()
时都要遍历颜色的向量。 Note that you are actually overwriting lines three times here since you are iterating through Data$Species
-- you probably intended unique(Data$Species)
. 请注意,由于要遍历
Data$Species
,因此实际上您实际上在这里重写了三行-您可能打算使用unique(Data$Species)
。
But as per @Iris's comment, you can do this more easily using ggplot2
: 但是根据
ggplot2
的评论,您可以使用ggplot2
更轻松地完成此ggplot2
:
require(ggplot2)
ggplot(data = Data, aes(x = Transect, y = Count, group = Species)) +
xlab("Transect Number") + ylab("Total Number") +
geom_line(aes(color = Species))
Short answer is look at ggplot2
as that's far superior than using plot. 简短的答案是看
ggplot2
因为它比使用plot优越得多。 Just to see the above working, here's a working example (your code slightly modified): 只是为了看上面的工作,这是一个工作示例(您的代码略有修改):
tt <- "Species Count Transect
Cod 25 1
Cod 36 2
Cod 45 3
Haddock 12 1
Haddock 23 2
Haddock 34 3"
Data <- read.table(text=tt,header = T)
xrange <- range(Data$Transect)
ymax <- max(Data$Count)
my.levels <- levels(Data$Species)
my.colors<- rainbow(length(my.levels))
plot(xrange,
y=c(0,ymax+10),
yaxs = "i",
type="n",
las=1,
xlab="Transect Number",
ylab="Total Number")
for (i in 1:length(my.levels))
{
species <- subset(Data, Species==my.levels[i])
lines(species$Transect, species$Count, type="o",lwd=2,lty=1,pch=NA, col = my.colors[i])
}
When executed will plot: 执行后将绘制:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.