Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(RColorBrewer)
- library(ggplot2)
- library(reshape2)
- library(grid)
- library(gridExtra)
- # simply function for getting badrate ----------------------
- getBadRate <- function(xVar, yVar){
- tapply(yVar, xVar, sum) / tapply(yVar, xVar, length)
- }
- # ploting all things I'm talking about ---------------------
- doVarStab <- function(yVar, xVar, year, name=NA, dir=getwd()){
- yearUniq <- sort(unique(year))
- xVarUniq <- sort(unique(xVar))
- if(length(xVarUniq) > 9) {stop("zbyt wiele do dpzoailu")}
- mBadRate <- matrix(0, nrow = length(xVarUniq), ncol = length(yearUniq))
- w <- 1
- for (i in yearUniq){
- idx <- which(year==i)
- mBadRate[w : (w + length(xVarUniq) - 1)] <- getBadRate(xVar = xVar[idx], yVar = yVar[idx])
- w <- w + length(xVarUniq)
- }
- #line plots for badrate
- mBadRate <- melt(mBadRate)
- colnames(mBadRate) <- c("variable", "year", "badrate")
- mBadRate[, 2] <- as.factor(mBadRate[, 2])
- p1 <- ggplot(mBadRate, aes(variable, badrate, group = year, colour = year)) +
- geom_line(size=1) +
- geom_point(size=3, shape=1) +
- scale_colour_brewer(palette = "Set1") +
- theme_minimal()
- #barplots for structure of variable
- dt <- data.frame(xVar, year)
- p2 <- ggplot(dt, aes(year, fill=xVar)) +
- geom_bar( position="fill") +
- scale_fill_brewer(palette = "Set1") +
- theme_minimal()
- #save graphs
- grid.arrange(p2, p1, ncol = 2, top=name)
- }
- # example -----------------------------------
- dt <- data.frame(def=rep(c(TRUE,FALSE),each=50),
- b=c(rep(c("a","b"),each=30), rep("c",40)),
- c=sample(c("xx","yy","zz"),100,replace=T),
- rok=sample(2012:2015,100,replace=T))
- dt
- doVarStab(yVar=dt$def, xVar=dt$b, year=dt$rok, name="trololo")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement