library(xlsx)
## Loading required package: rJava
## Loading required package: xlsxjars
dataset <- read.xlsx("~/R/Infestats/kpi.xlsx", 1)
dataset
## Year Sales Profits Costs
## 1 2000 500 88.00 412.0
## 2 2001 450 74.88 375.1
## 3 2002 490 85.46 404.5
## 4 2003 425 76.84 348.2
## 5 2004 480 89.86 390.1
## 6 2005 480 82.18 397.8
## 7 2006 405 71.93 333.1
## 8 2007 430 66.74 363.3
## 9 2008 485 83.03 402.0
## 10 2009 475 88.16 386.8
## 11 2010 425 55.76 369.2
## 12 2011 430 69.49 360.5
## 13 2012 430 82.56 347.4
library(ggplot2)
plot_sales<-ggplot(dataset,aes(x=factor(Year),y=Sales,fill=Sales))+ xlab('Year') + ylab('Sales ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green') + # add a visual indicator
ggtitle('Yearly sales') + # add a title
theme(axis.text.x = element_text(angle = -35)) # tilt the axis labels
plot_sales
library(ggplot2)
plot_profits<-ggplot(dataset,aes(x=factor(Year),y=Profits,fill=Profits))+ xlab('Year') + ylab('Profits ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green') + # add a visual indicator
ggtitle('Yearly profits') + # add a title
theme(axis.text.x = element_text(angle = -35)) # tilt the axis labels
plot_profits
## Warning: package 'gridExtra' was built under R version 3.2.5
Last updated on: 2016-09-08 19:12:57
Some additional text to add context to the KPI interpretation. The .R file:
library(knitr) library(ggplot2) library(gridExtra)
library(xlsx) dataset<-read.xlsx(‘dataset.xlsx’,1)
plot_sales<-ggplot(dataset,aes(x=factor(Year),y=Sales,fill=Sales))+ xlab(‘Year’) + ylab(‘Sales ($)’) + geom_bar(stat=‘identity’,colour=‘black’) + # make a barchart of the data scale_fill_gradient(low=‘red’,high=‘green’) + # add a visual indicator ggtitle(‘Yearly sales’) + # add a title theme(axis.text.x = element_text(angle = -35)) # tilt the axis labels
plot_profits<-ggplot(dataset,aes(x=factor(Year),y=Profits,fill=Profits))+ xlab(‘Year’) + ylab(‘Profits ($)’) + geom_bar(stat=‘identity’,colour=‘black’) + # make a barchart of the data scale_fill_gradient(low=‘red’,high=‘green’) + # add a visual indicator ggtitle(‘Yearly profits’) + # add a title theme(axis.text.x = element_text(angle = -35)) # tilt the axis labels