We all know that, using standard tool in Microcoft Word to plot a diagram quite mediocre (and the result is not as fancy as using MATLAB). I want something different. Something that look like a research. So, with my little talent using R Programming, I decided to use this platform to generate graph.
Consider I have a dummy dataset in Excel format. Generally, I load the dataset into the environment. Then I melt the dataset using ‘melt function’ in ‘Reshape package’. After that, I plot it using ggplot. Sounds simple, right?
The steps to plot the diagram is provided below;
I actually just click at ‘Import Dataset’ in the environment box. Then it will automatically import the dataset into the environment. But you can just type code below, and it will work the same way. Just make sure, your dataset (excel) located in your working file.
library(readxl)
data_accuracy<-read_excel("Accuracy_2.xlsx")
print(data_accuracy)
## # A tibble: 5 x 6
## scales UDA Esper Instans `NFA-HTS` LCA
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1000 100 100 100 100 100
## 2 2000 100 100 100 100 100
## 3 3000 100 100 100 100 100
## 4 4000 100 100 100 100 100
## 5 5000 100 100 100 100 100
Ohh.. This part is not important. It is for me, for my future benefit.
library(tibble)
#change first column (scales) to row name
data_accuracy_col_to_row <- column_to_rownames(data_accuracy, var = "scales")
print(data_accuracy_col_to_row)
## UDA Esper Instans NFA-HTS LCA
## 1000 100 100 100 100 100
## 2000 100 100 100 100 100
## 3000 100 100 100 100 100
## 4000 100 100 100 100 100
## 5000 100 100 100 100 100
This is the most important part for me. I thought I need to delete the ‘scale’ name in the row. Turns out it is unnecessary. I just straight away melt the data using melt function in ‘reshape2 package’.
library(reshape2)
## Warning: package 'reshape2' was built under R version 3.6.2
accuracy_melt <- melt(data_accuracy, id.vars = "scales")
head(accuracy_melt)
## scales variable value
## 1 1000 UDA 100
## 2 2000 UDA 100
## 3 3000 UDA 100
## 4 4000 UDA 100
## 5 5000 UDA 100
## 6 1000 Esper 100
This is the exciting part. Did you know any library that can create a fancy diagram just like GGPLOT did ? Here is my best reference.
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
figure_accuracy <- ggplot(accuracy_melt, aes(x=scales, y=value, fill=variable)) +
geom_bar(stat = "identity", position = position_dodge()) +
labs(y = "Accuracy (%)", x="Event Stream Scales") + ggtitle("Detection Accuracy")+
theme(plot.title = element_text(hjust = 0.5), legend.title = element_blank())
print(figure_accuracy)
Please note that this diagram was created using dummy data. This steps is for my future reference.