Correctness and Performance of graph plotting on R Current State:

I have two CSV files, their sample attached below:


FileOutputWithoutBuffer-Metrics.csv

File Length , Non-Buffered Time Taken (ns)
1000 , 5499114
2000 , 10971957
3000 , 15736008
4000 , 18970057
5000 , 22173215
6000 , 24612263
7000 , 26118520
8000 , 29934220
9000 , 31919477
10000 , 34940645

FileOutputWithBuffer-Metrics.csv

File Length , Buffered Time Taken (ns)
1000 , 412991
2000 , 224509
3000 , 269990
4000 , 461664
5000 , 485668
6000 , 479069
7000 , 413657
8000 , 438734
9000 , 760068
10000 , 576458



What I want:

I want to plot a compare and contrast graph between these two CSV files corresponding to their File Length column.

What I did:

I wrote the below script for the same:

# This line has to be updated on every place, possible.
scriptPath &lt;- "XXX/XXX/XXX/XXX";

# Read in all csv files.
FileOutputWithoutBufferMetrics &lt;- read.csv(file = file.path(scriptPath, "FileOutputWithoutBuffer-Metrics.csv"), header = TRUE, sep = ",");
FileOutputWithBufferMetrics    &lt;- read.csv(file = file.path(scriptPath, "FileOutputWithBuffer-Metrics.csv"), header = TRUE, sep = ",");

FileOutputMetircs &lt;- merge(FileOutputWithoutBufferMetrics, FileOutputWithBufferMetrics, by=c('File.Length'), all=T);
#colnames(FileOutputMetircs) &lt;- c("File Length", "Non-Buffered OutputStream Time", "Buffered OutputStream Time");
show(FileOutputMetircs);

# Install extra packages :- ggplot2, reshape2.
list.of.packages &lt;- c("ggplot2","reshape2");
new.packages &lt;- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])];
if(length(new.packages)) {
    install.packages(new.packages)
}
require(ggplot2);
require(reshape2);

# Create a graph of FileOutputStream.
df &lt;- melt(FileOutputMetircs, id.vars = 'File.Length', variable.name = 'Time');
ggplot(df, aes(File.Length, value)) + geom_line(aes(colour = Time));


And got this output:



Where help is needed:


First, since I am very new to ggplot. Therefore, need assurity on the correctness of my R script.
Other ways (performace-wise) to optimize the script. 


Thanks!.
The plot looks about right with the data, and I see no egrerious errors in your code. I do have a few remarks.


In r it's unnecessary to end your lines with a semi-colon, and most styleguides advise against it. The only real purpose it has is allowing multiple statements on a single line, but why would we want that.
With regards to your code where you install packages. There are two ways of loading a library: library(name) and require(name). One big difference is that require returns a FALSE when the package is missing, library throws an error. What you can do is try and load the library, and if it fails install it:


for(libraryName in c("ggplot2", "reshape2")){
  if(!require(libraryName)){
    install.package(libraryName)
    require(libraryName)
  }
}



If your csv files are going to be huge, I recommend using data.table::fread for reading your csv files. In my experience it is the fastest reading large datasets. It also allows for efficient dataframe operations.
If at reading time you specify the correct columnnames, you can prevent having to do the melt operation later: add the argument col.names = c("File.Length", "value"). Add the column FileOutputWithoutBufferMetrics["Time"] &lt;- "Non.Buffered.Time.Taken..ns." to both dataframes, and simply rbind them. This is most likely more efficient than melt, especially on big datasets.
# This line has to be updated on every place, possible. This is generally accounted for using the "working directory" of the R process. If you run the script from commandline, the working directory is the current directory. If you run it from Rstudio you can set it using the setwd(dir) function, or in Session -&gt; Set Working Directory -&gt; To Source File Location in the Rstudio ui. Once that is set, you don't have to add that path to the filename in read.csv.




In summary:

for(libraryName in c("ggplot2", "data.table")){
  if(!require(libraryName)){
    install.package(libraryName)
    require(libraryName)
  }
}

FileOutputWithoutBufferMetrics &lt;- fread("FileOutputWithoutBuffer-Metrics.csv"), col.names = c("File.Length", "value"))
FileOutputWithoutBufferMetrics["Time"] &lt;- "Non.Buffered.Time.Taken..ns."

FileOutputWithBufferMetrics    &lt;- fread("FileOutputWithBuffer-Metrics.csv"), col.names = c("File.Length", "value"))
FileOutputWithBufferMetrics["Time"] &lt;- "Buffered.Time.Taken..ns."

df &lt;- rbind(FileOutputWithoutBufferMetrics, FileOutputWithBufferMetrics)


ggplot(df, aes(File.Length, value)) + geom_line(aes(colour = Time))