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

plot multiple lines in r ggplot2
r plot multiple lines from data frame
r plot two graphs on top of each other
r plot legend
how to plot multiple curves in same graph in r
r multiple plots on same graph
plot multiple columns in r
line plot in r

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.

If your data is in wide format matplot is made for this and often forgotten 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 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().

Plotting line graphs in R, How do you plot two lines on a graph in R? Solution 1: just plot one data series and then use the points or lines commands to plot the other data series in the same figure, creating the multiple data series plot: > plot ( time , series1, type = 'l' , xlab = 't /s' , ylab = 's1' ) > points ( time , series2, type = 'l' )

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
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))

R tutorials, multiple curves, multiple plots, same plot R, labels for the x and y axis, and add a title (with the main argument). plot vertical color line in R. [1,] 4.698478 0 [2,] 4.698205 1 [3,] 4.698569 0 [4,] 4.697385 -1 to plot a blue color vertical line when [,2] is 1, and a red line when [,2] is -1, wonder if someone can help, thanks!

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 <- rainbow(5)

for (i in 1:5){
    lines(-10:10,runif(21,-10,10),col = cl[i],type = 'b')

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.

Which feature in R allows to add new graph on top another existing , How do you plot multiple curves in the same graph explain with example? you mean multiple series (instead of y axis); right? – M-- Jun 13 '17 at 13:17 just like the last image, mm/dd on x-axis vs sales of each year with different colors.

More than one line can be drawn on the same chart by 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.


How to Create a GGPlot with Multiple Lines, Which feature in R allows to add new graph on top another existing graph? Line Plots in R How to create line aplots in R. Examples of basic and advanced line plots, time series line plots, colored charts, and density plots. New to Plotly? Plotly is a free and open-source graphing library for R.

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

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

Plotting two lines on one plot with ggplot2, This tutorial describes how to create a ggplot with multiple lines. GGPlot2 Essentials for Great Data Visualization in R Data. The US economics time series datasets are used. This is a data frame with 478 Line plot of the variable 'psavert' by date: ggplot(economics, aes(x=date)) + geom_line(aes(y = psavert)​, color  Kickstarting R - Plotting more than one data series. Adding points or lines to a plot If you only want to overlay data series on the same axes, it is sufficient to specify that you don't want to "erase" the first plot and suppress display of the axes after the first plot:

Line Plots - R Base Graphs - Easy Guides - Wiki, The lines() creates curves by joining a sequence of given points with line segments. In the R script shown below, we plot the curves of three data sets (x,y1​),(x,y2) legends, typically one per graph col -----> A vector of colors for 'col' parameter. How to plot multiple data series in ggplot for quality graphs? I've already shown how to plot multiple data series in R with a traditional plot by using the par(new=T) , par(new=F) trick. Today I'll discuss plotting multiple time series on the same plot using ggplot() .

Mastering R Plot – Part 1: colors, legends and lines, Plot two lines on one graph in ggplot and R any number of lines on the plot; A legend is provided by default; each line has a different colour  Basic line plot. Line plot of the variable ‘psavert’ by date: Plot with multiple lines. Well plot both ‘psavert’ and ‘uempmed’ on the same line chart. Solution 1: Make two calls to geom_line(): Solution 2: Prepare the data using the tidyverse packages.

Colors for Plotting, Basic commands to plot line graphs with one or more series in R. Without any other arguments, R plots the data with circles and uses the change the color to green, make the line width be 5, specify different labels Notice that the range of the plot does not expand to include all of the line plotted by the lines command. Colors for Plotting. The difference between a simple graph and a visually stunning graph is of course a matter of many features. But one of the biggest contributors to the “wow” factors that often accompanies R graphics is the careful use of color. By default, R graphs tend to be black-and-white and, in fact, rather unattractive.