This is only some remarks about multiple plots for sales analysis.
Data 1. All products for 3 years.
library(reshape2)
library(ggplot2)
prod1 <- c(14000, 10000, 1500)
prod2 <- c(33000, 48000, 30000)
prod3 <- c(58000, 90000, 52000)
prod4 <- c(550, 2300, 4000)
kiekiai <- data.frame(prod1, prod2, prod3, prod4)
row.names(kiekiai) <- c("2011/2012", "2012/2013", "2013/2014")
rm(prod1, prod2, prod3, prod4)
kiekiai <- as.data.frame(t(kiekiai))
kiekiai <- data.frame(pav=row.names(kiekiai), kiekiai)
kiekiai <- data.frame(id = seq_along(kiekiai$pav), kiekiai)
colnames(kiekiai) <- c("id", "Product", "11-12", "12-13", "13-14")
rownames(kiekiai) <- NULL
kiekiai.m <- melt(kiekiai[, 2:5])
colnames(kiekiai.m) <- c("Products", "Year", "Quantity")
Plot 1:
fig1 <- ggplot(data=kiekiai.m, aes(x=Year, y=Quantity, group=Products, colour=Products)) + geom_line()
fig1
Data 2. Ones specific product, divided to sub-products for 2 years.
pr.Stand <- c(26000, 12000)
pr.gran <- c(16000, 8000)
pr.K <- c(5800, 9000)
pr.BB <- c(900, 800)
Year <- c("2012/2013", "2013/2014")
pr <- data.frame(Year, pr.Stand, pr.gran, pr.K, pr.BB)
rm(pr.BB, pr.gran, pr.K, pr.Stand, Year)
pr.12.13 <- melt(pr[1, -1])
colnames(pr.12.13 ) <- c("Product", "Quantity")
pr.13.14 <- melt(pr[2, -1])
colnames(pr.13.14 ) <- c("Product", "Quantity")
Plot 2
fig2 <- ggplot(data=pr.12.13, aes(x=factor(1), y=Quantity, fill=Product)) +
geom_bar(stat="identity") +
geom_text(aes(x=factor(1), y=c(13000, 34000, 45000, 48000), label=pr.12.13$Quantity), size=3) +
coord_polar(theta = "y") +
ylab("") + xlab("") + ggtitle("2012/2013") + guides(fill=FALSE)
fig2
Plot 3
fig3 <- ggplot(data=pr.13.14, aes(x=factor(1), y=Quantity, fill=Product)) +
geom_bar(stat="identity") +
geom_text(aes(x=factor(1), y=c(6000, 16000, 24500, 29500), label=pr.13.14$Quantity), size=3) +
coord_polar(theta = "y") +
ylab("") + xlab("") + ggtitle("2013/2014")
fig3
And all small plots together in one big plot.
First of all multiplot function. This function is taken from Cookbook for R
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
Big plot
multiplot(fig1, fig2, fig3, layout = matrix(c(1,1,2,3), nrow=2, byrow=TRUE))
## Loading required package: grid
corrected pie plot, with the right data positions and 2 plots in one
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.1.1
##
## 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
# 1 meltig data to long format
pr.m <- melt(pr)
## Using Year as id variables
colnames(pr.m) <- c("Year", "Product", "Quantity")
# 2 adding mid. points to the data frame
pr.m <- pr.m %>% group_by(Year) %>% mutate(pos = cumsum(Quantity)-Quantity/2)
# 3 making plots
ggplot(data=pr.m, aes(x=factor(1), y=Quantity, fill=factor(Product))) +
geom_bar(stat="identity") +
geom_text(aes(x= factor(1), y=pos, label = Quantity), size=3) + # note y = pos
facet_grid(facets = .~Year, labeller = label_value) +
ylab("Quantity") + xlab("") + scale_fill_discrete(name="Product") +
coord_polar(theta = "y")
From the last plot we can see, that in 2013/2014 was sold less probuct and the proportion of the product is seen on the plot too.