# Correctness and Performance of graph plotting on R

### Current State:

I have two CSV files, their sample attached below:

1. 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

2. 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 <- "XXX/XXX/XXX/XXX";

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

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

# Install extra packages :- ggplot2, reshape2.
list.of.packages <- c("ggplot2","reshape2");
new.packages <- 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 <- 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:

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

Thanks!.

• "First, since I am very new to ggplot. Therefore, need assurity on the correctness of my R script." <--- This is off-topic on this site. – Igor Soloydenko Nov 19 '19 at 7:51

## 1 Answer

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"] <- "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 -> Set Working Directory -> 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 <- fread("FileOutputWithoutBuffer-Metrics.csv"), col.names = c("File.Length", "value"))
FileOutputWithoutBufferMetrics["Time"] <- "Non.Buffered.Time.Taken..ns."

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

df <- rbind(FileOutputWithoutBufferMetrics, FileOutputWithBufferMetrics)

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

• Each and everything is well explained. Thanks, @JAD! – surajs1n Nov 19 '19 at 18:05