Plot multiple lines (data series) each with unique color in R

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I am fairly new to R and I have the following queries :

I am trying to generate a plot in R which has multiple lines (data series). Each of these lines is a category and I want it to have a unique color.

Currently my code is setup in this way :

First, I am creating an empty plot :

plot(1,type='n',xlim=c(1,10),ylim=c(0,max_y),xlab='ID', ylab='Frequency') 

Then for each of my category, I am plotting lines in this empty plot using a "for" loop like so :

for (category in categories){ lines(data.frame.for.this.category, type='o', col=sample(rainbow(10)), lwd=2) } 

There are 8 categories here, and so there are 8 lines produced in the plot. As you can see, I am trying to sample a color from the rainbows() function to generate a color for each line.

However, when the plot is generated, I find that there are multiple lines which have the same color. For instance, 3 of those 8 lines have green color.

How do I make each of these 8 lines have a unique color ?

Also, how do I reflect this uniqueness in the legend of the plot ? I was trying to lookup the legend() function, however it was not clear which parameter I should use to reflect this unique color for each category ?

Any help or suggestions would be much appreciated.

回答1:

If your data is in wide format matplot is made for this and often forgotten about:

 dat 

There is also the added bonus for those unfamiliar with things like ggplot that most of the plotting paramters such as pch etc. are the same using matplot() as plot().



回答2:

If you would like a ggplot2 solution, you can do this if you can shape your data to this format (see example below)

# dummy data set.seed(45) df 



回答3:

You have the right general strategy for doing this using base graphics, but as was pointed out you're essentially telling R to pick a random color from a set of 10 for each line. Given that, it's not surprising that you will occasionally get two lines with the same color. Here's an example using base graphics:

plot(0,0,xlim = c(-10,10),ylim = c(-10,10),type = "n")  cl 

Note the use of type = "n" to suppress all plotting in the original call to set up the window, and the indexing of cl inside the for loop.



回答4:

Using @Arun dummy data :) here a lattice solution :

xyplot(val~x,type=c('l','p'),groups= variable,data=df,auto.key=T) 



回答5:

More than one line can be drawn on the same chart by using the lines()function

# Create the data for the chart. v 

OUTPUT



回答6:

Here are nice description how to add lines to

plot()

using function -

par(new=T)

option

http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_addat.html

To color them differently you will need

col()

option. To avoid superfluous axes descriptions use

xaxt="n"

and

yaxt="n"

for second and further plots.



回答7:

I know, its old a post to answer but like I came across searching for the same post, someone else might turn here as well

By adding : colour in ggplot function , I could achieve the lines with different colors related to the group present in the plot.

ggplot(data=Set6, aes(x=Semana, y=Net_Sales_in_pesos, group = Agencia_ID, colour = as.factor(Agencia_ID)))     

and

geom_line()  



回答8:

Here is a sample code that includes a legend if that is of interest.

# First create an empty plot. plot(1, type = 'n', xlim = c(xminp, xmaxp), ylim = c(0, 1),       xlab = "log transformed coverage", ylab = "frequency")  # Create a list of 22 colors to use for the lines. cl 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!