Build several rows (data series), each of which has a unique color in R

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

I am trying to create a graph in R that has several rows (data series). Each of these lines is a category, and I want it to have a unique color.

My code is currently configured in this way:

First, I create 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 draw lines on this empty section using the "for" loop, for example:

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

There are 8 categories, and therefore 8 lines are produced on the chart. As you can see, I am trying to select a color from the rainbows () function to generate a color for each row.

However, when the graph is generated, I find that there are several lines that have the same color. For example, 3 of these 8 lines are green.

How to make each of these 8 lines unique?

In addition, how can I reflect this uniqueness in the story legend? I tried to find the legend() function, however it is not clear which parameter should I use to reflect this unique color for each category?

Any help or suggestions would be greatly appreciated.

+47
colors r plot unique lines
Feb 13 '13 at 18:07
source share
8 answers

If your data is in a wide matplot format for this and you often forget about:

  dat <- matrix(runif(40,1,20),ncol=4) # make data matplot(dat, type = c("b"),pch=1,col = 1:4) #plot legend("topleft", legend = 1:4, col=1:4, pch=1) # optional legend 

There is also an added bonus for those unfamiliar with things like ggplot , that most charting options like pch , etc. are the same using matplot() like plot() . enter image description here

+55
Feb 13 '13 at 18:33
source share

If you want to use the ggplot2 solution, you can do it if you can generate your data in this format (see example below)

 # dummy data set.seed(45) df <- data.frame(x=rep(1:5, 9), val=sample(1:100, 45), variable=rep(paste0("category", 1:9), each=5)) # plot ggplot(data = df, aes(x=x, y=val)) + geom_line(aes(colour=variable)) 

ggplot2_geom_line

+40
Feb 13 '13 at 18:11
source share

You have the right overall strategy for this, using basic graphics, but as mentioned, you basically say R to select a random color from a set of 10 for each row. Given this, it is not surprising that you sometimes get two lines of the same color. Here is an example of using basic graphics:

 plot(0,0,xlim = c(-10,10),ylim = c(-10,10),type = "n") cl <- rainbow(5) for (i in 1:5){ lines(-10:10,runif(21,-10,10),col = cl[i],type = 'b') } 

enter image description here

Note the use of type = "n" to suppress all graphs in the original call to set up the window and index cl within the for loop.

+20
Feb 13 '13 at 18:17
source share

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

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

enter image description here

+7
Feb 13 '13 at 19:30
source share

Here is a nice description of adding rows to

plot ()

using function -

n (new = T)

options

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

To color them in different ways you will need

Col ()

option. To avoid unnecessary axis descriptions, use

xaxt = "n"

and

yaxt = "n"

for the second and subsequent graphs.

+2
Nov 17 '13 at 23:05
source share

I know his old post to answer, but as I came across a search for the same post, someone else could trade here

Adding: color in the ggplot function, I could get lines with different colors related to the group present on the chart.

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

and

 geom_line() 

Line Graph with Multiple colors

+2
Aug 19 '16 at 22:28
source share

More than one line can be drawn on a single chart using the lines() function

 # Create the data for the chart. v <- c(7,12,28,3,41) t <- c(14,7,6,19,3) # Give the chart file a name. png(file = "line_chart_2_lines.jpg") # Plot the bar chart. plot(v,type = "o",col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart") lines(t, type = "o", col = "blue") # Save the file. dev.off() 

OUTPUT enter image description here

+2
Mar 07 '17 at 10:38 on
source share

Here is an example of code that includes a legend, if interested.

 # 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 <- rainbow(22) # Now fill plot with the log transformed coverage data from the # files one by one. for(i in 1:length(data)) { lines(density(log(data[[i]]$coverage)), col = cl[i]) plotcol[i] <- cl[i] } legend("topright", legend = c(list.files()), col = plotcol, lwd = 1, cex = 0.5) 
+1
Apr 13 '15 at 15:32
source share



All Articles