简体   繁体   English

R获得与绘图线颜色相同的图例

[英]R get legend with same color as plot lines

Hi I am new to R and am trying to plot multiple files as lines in a scatter plot. 嗨,我是R的新手,我正在尝试将多个文件绘制为散点图中的线条。 I was able to get the plot but not when I try to add legend to the plot. 我能够得到情节但不是当我尝试将图例添加到情节时。 I want the legend with name of the file in the same color as the color of the line made from that file. 我希望文件名称的图例颜色与该文件的颜色相同。 I tried using the following suggestion from a previous thread - 我尝试使用上一个帖子中的以下建议 -

xlist<-list.files(pattern = NULL)  
first=TRUE
cl <- rainbow(22)
for(i in xlist) {
    table <- read.table((i),header=T,sep="\t")  
    table <- table[, 1:2]  
    if (first) {  
        plot(table,xlab='Distance from         center',ylab='Coverage',ylim=c(0,70),col=1, type="n")  
        lines(table) #plot and add lines  
        legend("top", y=NULL, legend = i, col=1)  
        par(new=T)  
        first=FALSE   
    }  
    else {  
        lines(table,col=cl[i]) #To add another file to the plot as another line  
        par(new=F)  
        plotcol[i] <- cl[i]  
        legend("top", y=NULL, legend = i, col=plotcol)  
    }
}  

The error is get is - Error in plotcol[i] <- cl[i] : object 'plotcol' not found. 错误是get is - plotcol [i] < - cl [i]中的错误:找不到对象'plotcol'。 Please let me know what I am missing or if there is a better way to plot the lines with different colors and get legend with names of the files with the same color as the lines. 请让我知道我缺少的是什么,或者是否有更好的方法来绘制不同颜色的线条,并获得与线条颜色相同的文件名称的图例。 Thank you. 谢谢。

I had to make some reproducible examples to get it to work, but the following script works to make the lengends and line colors the same: 我必须制作一些可重现的示例才能使其工作,但以下脚本可以使lengends和line颜色相同:

#random data
test.df1=data.frame(runif(100)*0.2,runif(100)*0.2)
test.df2=data.frame(runif(100)*0.5,runif(100)*0.5)
test.df3=data.frame(runif(100),runif(100))
test.df4=data.frame(runif(100)*2,runif(100)*2)

test.list=list(test.df1=test.df1,test.df2=test.df2,test.df3=test.df3,test.df4=test.df4) # I used this instead of reading in files from read.table, you shouldn't need this

xlist=c('test.df1','test.df2','test.df3','test.df4') #the list of files

first=TRUE
cl <- rainbow(length(xlist)) #colors dedicated to your list
names(cl)=xlist #this names the vector elements so you can reference them
for(i in xlist) {
    i.table <- test.list[[i]]
    i.table <- i.table[,c(1:2)]  
    if (first) {  
        plot(i.table,xlab='Distance from center',ylab='Coverage',xlim=c(0,2),ylim=c(0,2),col=cl[i], type="n")  
        lines(i.table,col=cl[i]) #plot and add lines  
        par(new=T)  
        first=FALSE   
    }  
    else {  
        lines(i.table,col=cl[i]) #To add another file to the plot as another line  
        par(new=F)  
        plotcol <- c(plotcol,cl[i])# pulls colors from the cl vector
    }
}  

legend("top", y=NULL, legend =xlist, text.col=cl) #label colors should now match

Try ggplot2 package in R. You wouldn't have to code as much too! 在R中尝试ggplot2包。你也不必编写代码! https://www.rstudio.com/wp-content/uploads/2015/12/ggplot2-cheatsheet-2.0.pdf https://www.rstudio.com/wp-content/uploads/2015/12/ggplot2-cheatsheet-2.0.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM