# SVmatrix package # Version 0.1.3 # About matrix http://svmatrix.online/ # Contact package author z@svmatrix.online # Neccessary packages # Installing (1 time) install.packages("data.table") install.packages("ggplot2") install.packages("dplyr") install.packages("circlize") install.packages("RColorBrewer") # Loading packages (each time with R launch) library(data.table) library(ggplot2) library(dplyr) library(circlize) library(readxl) library("RColorBrewer") # for nicer colors, not neccessary dataset <- read_excel("data_template.xlsx") # template file from the website # Translate the dataset to fit the package colnames(dataset)[1] <- "company" long <- melt(setDT(dataset), id.vars = c("company"), variable.name = "Year") # transform from wide to long # Getting data to create matrix svmatrix_prep <- function(data, # long dataset years, # vector of years studied share_column, # number of column with market share year_column, # number of column with year N # number of companies ) { matrix_data <- data.frame(matrix(NA, 10, 5)) colnames(matrix_data) <- c("Lind", "CRSV", "HTSV", "Quadrant", "Year") count = 1 for (i in years) { colnames(data)[year_column] <- "Year" sv_data <- data%>% filter(as.numeric(as.character(Year)) == as.numeric(i)) colnames(sv_data)[share_column] <- "Value" sv_vec <- c(t(sv_data$Value)) sv_vector <- sort(sv_vec, decreasing = TRUE) Qsum <- NA CR_i <- cumsum(sv_vector) Qmat <- matrix(NA, nrow = N, ncol = N-1) for (m in c(1:N-1)) { for (l in c((m+1):(N))) { Qmat[l, m] <- (CR_i[m]/m)/((CR_i[l]-CR_i[m])/(l-m)) } Qsum <- c(Qsum, sum(Qmat[m+1,], na.rm = TRUE)) } Qsum <- Qsum[2:N] L <- c(rep(NA, N)) for (m in c(2:N)) { L[m] <- Qsum[m]*(1/(m*(m-1))) } Lind <- NA for (m in c(2:N)) { if (L[m+1]>L[m]) { Lind = m break } } if (is.na(Lind) == TRUE) { break } CRSV <- sum(sv_vector[1:Lind]) CR_norm <- sv_vector/sum(sv_vector[1:Lind])*c(1:N) HTn <- 1/(2*cumsum(CR_norm)-1) HTSVn <- (HTn-1/c(1:N))/(1-1/c(1:N)) HTSV <- HTSVn[Lind] if (CRSV > 0.65 & HTSV > 0.1) { Quad = "G" }else{ if (CRSV < 0.65 & HTSV > 0.1) { Quad = "I" }else{ if (CRSV > 0.65 & HTSV < 0.1) { Quad = "B4" }else{ Quad = "RO" } } } matrix_data[count,] <- as.numeric(c(as.character(Lind), as.character(CRSV), as.character(HTSV), NA, i)) matrix_data[count, 4] <- Quad count = count + 1 } count = 1 matrix_data <- na.omit(matrix_data) return(matrix_data) } # function to calculate data to build matrix SV_data <- svmatrix_prep(long, years = c(2007:2021), share_column = 3, year_column = 2, 15) # Building matrix (the current version throws an error, but the matrix is built) svmatrix <- function(svmatrix_prep_dataset, # Dataset made by svmatrix_prep function, must be inputted label_x_offset = -0.005, # Year label offset on x axis, single number or vector with length of number of years label_y_offset = 0.004, # Year label offset on y axis, single number or vector with length of number of years legend_position = "topleft", # Legend position legend_inset = c(-0.2, 0), # Legend offset (x, y) labels.off = FALSE, # Turn off year labels, TRUE|FALSE Lind.off = FALSE, # Turn of Lind coefficients on points, TRUE|FALSE legend.off = FALSE, # Turn off legend, TRUE|FALSE point_size = svmatrix_prep_dataset$Lind, # Size of points B4_label = c(0.825, 0.03), # Position of B4 label on matrix G_label = c(0.825, 0.3), # Position of G label on matrix RO_label = c(0.475, 0.03), # Position of RO label on matrix I_label = c(0.475, 0.3), # Position of I label on matrix xlim = c(0.3, 1), # x axis limits ylim = c(0.01, 1), # y axis limits cex = 0.8, # Legend size text.cex = 0.7, # Year labels size bg_col = "white", # Background color plot_col = "white", # Plot window color quad_col = "grey", # Quadrant labels color quad_size = 4, # Quadrant labels size pch = 16, # Point type point_col = 2:nrow(svmatrix_prep_dataset), # Point colors, vector (recommended to use pre-made palettes) axis_font = "Arial", # Axes labels font (from Windows standart fonts) labels_font = "Arial", # Year labels font (from Windows standart fonts) axis_font_type = 2, # Axes labels font type labels_font_type = 1, # Year labels font type add_line = FALSE, # Add line successively connecting points by year, TRUE/FALSE line_col = "red", # Line color line_size = 1, # Line width line_type = "dashed" # Line type ){ windowsFonts(A = windowsFont(axis_font)) windowsFonts(B = windowsFont(labels_font)) par(mar = c(5, 5, 4, 8), bg = bg_col) plot.new() rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col = plot_col) par(new = TRUE) plot(x = 1, y = 1, log = "y", xlim = xlim, ylim = ylim, col = plot_col, pch = 19, xlab="CRSV - доля отраслевого рынка, контролируемая группой доминирующих альфа-компаний", ylab="HTSV - уровень дифференициации между доминирующими в подотраслях альфами", family = "A", font = axis_font_type) + text(B4_label[1], B4_label[2], expression("B4"), cex = quad_size, col = quad_col) + text(G_label[1], G_label[2], expression("G"), cex = quad_size, col = quad_col) + text(RO_label[1], RO_label[2], expression("RO"), cex = quad_size, col = quad_col) + text(I_label[1], I_label[2], expression("I"), cex = quad_size, col = quad_col) + abline(v = 0.65) + abline(h = 0.1) + lines(svmatrix_prep_dataset$CRSV*(100*as.integer(add_line == FALSE)+1), svmatrix_prep_dataset$HTSV, col = line_col, lwd = line_size, lty = line_type) + points(svmatrix_prep_dataset$CRSV, svmatrix_prep_dataset$HTSV, cex = point_size, pch = pch, col = point_col) + text(svmatrix_prep_dataset$CRSV+label_x_offset*point_size/2*(100*as.integer(labels.off == TRUE)+1), svmatrix_prep_dataset$HTSV+label_y_offset*point_size/2, labels = svmatrix_prep_dataset$Year, cex = text.cex, family = "B", font = labels_font_type) + text(svmatrix_prep_dataset$CRSV*(100*as.integer(Lind.off == TRUE)+1), svmatrix_prep_dataset$HTSV, labels = svmatrix_prep_dataset$Lind, cex = max(0.25, point_size/8), col = "grey26") + legend(legend_position, inset = legend_inset+c(100*as.integer(legend.off == TRUE), 0), legend = c(svmatrix_prep_dataset$Year), cex = cex, pch = 19, title = "Year", col = 2:nrow(svmatrix_prep_dataset), xpd = TRUE) } # Function to build SV matrix svmatrix(SV_data, label_x_offset = c(rep(0.009, 2), rep(-0.009, 13)), label_y_offset = c(rep(-0.001, 4), rep(0.001, 11)), ylim = c(0.002, 0.1), xlim = c(0.4, 1), text.cex = 1, point_size = SV_data$Lind, legend_position = "topleft", legend_inset = c(0,100), cex = 0.75, point_col = brewer.pal(n = 15, name = "Dark2"), bg_col = "lightblue2", plot_col = "white", quad_col = "darkgrey", quad_size = 8, axis_font = "Arial", add_line = TRUE, line_col = "red", line_size = 2, pch = 19)