R 多轴海图
我创建了一个R脚本,供学生学习OLAP操作。到目前为止一切正常。 我想将结果可视化为多轴图表。我相信帕库德适合这样做。 但是我不能为轴和图例插入标签。有人有什么建议吗? 这也可能是另一种绘图方式。 提前谢谢R 多轴海图,r,plot,olap,R,Plot,Olap,我创建了一个R脚本,供学生学习OLAP操作。到目前为止一切正常。 我想将结果可视化为多轴图表。我相信帕库德适合这样做。 但是我不能为轴和图例插入标签。有人有什么建议吗? 这也可能是另一种绘图方式。 提前谢谢 ### OLAP-Operations in R ### # This R-script is essentially based on the coding of Ricky Ho (2013). # https://dzone.com/articles/olap-operation-r
### OLAP-Operations in R ###
# This R-script is essentially based on the coding of Ricky Ho (2013).
# https://dzone.com/articles/olap-operation-r
# I have made minor corrections and additions.
# S. Horn, 30.05.2021
# Parts:
# (1) Generate three dimension tables
# (2) Generate the central fact table
# (3) Build the multi-dimensional cube
# (4) OLAP-operations
## (1) Generate three dimension tables (state_table, month_table, prod_table)
state_table <-
data.frame(key=c("CA", "NY", "WA", "ON", "QU"),
name=c("California", "new York", "Washington", "Ontario", "Quebec"),
country=c("USA", "USA", "USA", "Canada", "Canada"))
month_table <-
data.frame(key=1:12,
desc=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
quarter=c("Q1","Q1","Q1","Q2","Q2","Q2","Q3","Q3","Q3","Q4","Q4","Q4"))
prod_table <-
data.frame(key=c("Printer", "Tablet", "Laptop"),
price=c(200, 300, 1000))
## Generate the central fact table (sales_fact)
gen_sales <- function(no_of_recs) {
# create transaction data randomly
loc <- sample(state_table$key, no_of_recs,
replace=T, prob=c(2,2,1,1,1))
time_month <- sample(month_table$key, no_of_recs, replace=T)
time_year <- sample(c(2012, 2013), no_of_recs, replace=T)
prod <- sample(prod_table$key, no_of_recs, replace=T, prob=c(1, 3, 2))
unit <- sample(c(1,2), no_of_recs, replace=T, prob=c(10, 3))
# delete: amount <- unit*prod_table[prod,]$price # original
sales <- data.frame(month=time_month,
year=time_year,
loc=loc,
prod=prod,
unit=unit)
# get price for calculation, new
sales <- merge(sales, prod_table, by.x="prod", by.y="key")
sales$amount <- sales$unit * sales$price
# Sort the records by time order
sales <- sales[order(sales$year, sales$month),]
row.names(sales) <- NULL
return(sales)
}
# Create the sales_fact table
sales_fact <- gen_sales(500)
# Look at a few records
head(sales_fact)
## (3) Build the multi-dimensional cube (revenue_cube)
revenue_cube <-
tapply(sales_fact$amount,
sales_fact[,c("prod", "month", "year", "loc")],
FUN=function(x){return(sum(x))})
# Showing the cells of the cube
revenue_cube
## (4) OLAP Operations
# Slice
# Cutting one slice of the cube by choosing a single value for one dimension (here year = 2012).
# Scheme: ["prod", "month", "year", "loc"]
slice1 <- revenue_cube[, , "2012", ]
slice1
# Dice
# Creating a smaller subcube by choosing specific values for more dimensions.
# Scheme: ["prod", "month", "year", "loc"]
dice1 <- revenue_cube[
c("Tablet","Laptop"),
c("1","2","3"),
c("2012"),
c("CA","NY")]
dice1
# Rollup
# Aggregation of data along a dimension by collapsing dimensions (here time and location).
# Differentiation by location and month are not important now.
# It corresponds to a navigation along a hierarchy from down (more details) to top (few details).
rollup1 <- apply(revenue_cube, c("prod", "year"),
FUN=function(x) {return(sum(x, na.rm=TRUE))})
rollup1
# Drilldown
# It is the reverse operation of rollup. In our example we unfold the time dimension now (year plus months).
# We are still not interested in differentiating location.
# It corresponds to a navigation along a hierarchy from top (few details) to down (more details).
drilldown1 <- apply(revenue_cube, c("prod", "month", "year"),
FUN=function(x) {return(sum(x, na.rm=TRUE))})
drilldown1
# Pivot
# Rotation of the cube to get another view (compare pivot1 and pivot2).
pivot1 <- apply(revenue_cube, c("prod", "month", "year"),
FUN=function(x) {return(sum(x, na.rm=TRUE))})
pivot1
pivot2 <- apply(revenue_cube, c("month", "year", "prod"),
FUN=function(x) {return(sum(x, na.rm=TRUE))})
pivot2
### TEST ###
library(MASS)
mycol = palette("R3")
parcoord(dice1, # data
col = mycol, # color
var.label = TRUE, # show min. and max. values
lty = 1, # linetype
main = "Test") # title
R中的OLAP操作###
#该R脚本基本上基于Ricky Ho(2013)的编码。
# https://dzone.com/articles/olap-operation-r
#我做了一些小的修改和补充。
#美国霍恩,2021年5月30日
#部分:
#(1)生成三维表格
#(2)生成中心事实表
#(3)构建多维立方体
#(4)OLAP操作
##(1)生成三维表(状态表、月份表、生产表)
state_table对于上面的示例,最简单的选择可能是手动添加项目。我复制了代码的最后一部分来说明。轴可以标记为
mtext()
(或text()
)。图例也可以通过legend()
功能添加
我已经包含了一个函数,如果数组具有一致的格式,它应该自动生成图例条目。它提取第一个和最后一个维度名称,并根据需要重复它们
getlegnames>#从摘要中按顺序复制看起来很棒。您使用什么类型的摘要函数来获取图例参数的信息?我没有使用任何函数。我的意思是当你计算完骰子1后输出骰子1谢谢。我认为可能有一个函数可以进一步自动化绘图,例如图例的参数。我添加了一个函数,该函数可以自动处理图例名称,前提是数组的格式是一致的。很好,我甚至不知道R可以实现什么。
getlegnames <- function(array){
legend_names <- c() # will become vector of names
list_of_names <- list(dimnames(array)) # turn dimension names into list
for(x in 1:dim(array)[3]){ # loop over 3rd dimension
for(y in 1:dim(array)[1]){ # loop over first dimension
# make a legend name
legend_names <- c(legend_names,paste(list_of_names[[1]][[3]][[x]],list_of_names[[1]][[1]][[y]],sep=', '))
}
}
return(legend_names)
}
library(MASS)
mycol = palette("R3")
parcoord(dice1, # data
col = mycol, # color
var.label = TRUE, # show min. and max. values
lty = 1, # linetype
main = "Test") # title
mtext('Month',side=1,line=3) # x axis as text
mtext('Sales',side=2,line=3) # y axis as text
legend('left', # location
legend = getlegnames(dice1), # copied in order from summary
col=mycol, # same colors
lty=1, # line type
lwd=1, # line width
cex=0.8 # legend size
)