library(pracma) data(titanium) head(titanium) xi = seq(595,1075,1) yi = interp1(titanium$x, titanium$y, xi = xi, method = "cubic") plot(xi,yi,type="l") xi[which(yi==max(yi))] points(xi[which(yi==max(yi))],max(yi),col=2,pch=16) N <- 10 # predpokladany pocet bodu npeaks <- identify(xi,yi,pos=FALSE,n=N,plot=FALSE) ## zadej N bodu npeaks <-sort(npeaks) xtime <- xi[npeaks] ytic <- yi[npeaks] min(npeaks) max(npeaks) plot(xi,yi,type="l") points(xtime,ytic,type="p",col=2,lwd=2) bsln.loess <- loess(ytic ~ xtime) pred <- predict(bsln.loess, xi) points(xi,pred, type="l",lty=2,col=4,lwd=2) intsc <- yi-pred plot(xi,intsc,type="l") abline(h=0,lty=2) library(pracma) plsplint = trapz(xi[min(npeaks):max(npeaks)], intsc[min(npeaks):max(npeaks)]) plsplint ####################### GRAFY ############################################################## ### data Iris data(iris) summary(iris) head(iris) # edit(iris) rownames(iris)=seq(1:length(iris[,1])) #### pairwise scatterplot pairs(iris[,-5]) library(ellipse) ellipse::pairs(iris[,-5]) library(gclus) cpairs(iris[,-5], order = NULL, panel.colors = NULL, border.color = "grey70",show.points = TRUE) library(ggplot2) library(GGally) ggpairs(iris[,-5]) library(psych) pairs.panels(iris[,-5],smooth=FALSE, scale=FALSE,density=TRUE,ellipses=FALSE,digits=2, method="spearman", pch = 20,lm=FALSE,cor=TRUE,jiggle=FALSE,factor=2, hist.col="cyan",show.points=TRUE,rug=FALSE) library(pgirmess) pairsrp(iris[,-5], meth = "spearman", pansmo = FALSE, abv = FALSE, lwt.cex = NULL) library(PerformanceAnalytics) chart.Correlation(iris[,-5], histogram=TRUE, pch=19) library(TeachingDemos) TeachingDemos::pairs2(iris[,c("Petal.Length", "Petal.Width")], iris[,c("Sepal.Length", "Sepal.Width")], panel = points, row1attop = TRUE, gap =.5) #### Korelacni matice library(mvoutlier) corr.plot(iris$Petal.Length, iris$Petal.Width, quan=1/2, alpha=0.025) library(TeachingDemos) cor.rect.plot(iris$Petal.Length,iris$Petal.Width, corr = TRUE, col = c("#ff000055", "#0000ff55")) cP <- cor(iris[,-5],method="pearson") cS <- cor(iris[,-5],method="spearman") cK <- cor(iris[,-5],method="kendall") library(robustbase) cR <- covMcd(iris[,-5],alpha=.75,nsamp=500,seed=NULL,use.correction=TRUE,control=rrcov.control(alpha=0.75),cor=T)$cor # robustni korelace MCD library(ellipse) plotcorr(cP, outline = TRUE, col = "gray", main="",numbers = FALSE, type = "full",cex.lab=0.8) colors <- c("#A50F15","#DE2D26","#FB6A4A","#FCAE91","#FEE5D9","white", "#EFF3FF","#BDD7E7","#6BAED6","#3182BD","#08519C") plotcorr(cP, col=colors) library(DescTools) cols =colors PlotCorr(cP, cols = colors, breaks = seq(-1, 1, length = length(cols) + 1), border = "grey", lwd = 1, args.colorlegend = NULL, xaxt = par("xaxt"), yaxt = par("yaxt"), cex.axis = 0.8, las = 2, mar = c(3, 8, 8, 8)) library(corrplot) corrplot(cR, method = "pie", type = "upper", order = "original") corrplot(cR, method="ellipse") corrplot(cR, type="full", order="FPC", method = "ellipse", p.mat = NULL, sig.level = 0.05, insig = "blank",tl.col=1,tl.cex = 1,cl.cex =0.8) # method = c("circle", "square", "ellipse", "number", "shade","color", "pie") # type = c("full", "lower", "upper"), # order = c("original","AOE", "FPC", "hclust", "alphabet") # hclust.method = c("complete", "ward","ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid"), library(openair) corPlot(iris[,-5], pollutants = NULL, type = "default",cluster = TRUE, cols = "hue", r.thresh = 0.8,text.col = c("black", "black"), auto.text = FALSE) cols =c("default","increment","heat","spectral","hue", "brewer1","greyscale") corPlot(m, pollutants = NULL, type = "default",cluster = TRUE, cols = "hue", r.thresh = 0.8,text.col = c("black", "black"), auto.text = FALSE) cols =c("default","increment","heat","spectral","hue", "brewer1","greyscale") ### Heat map col<- colorRampPalette(c("blue", "white", "red"))(20) heatmap(x = cK, col = col, symm = TRUE) heatmap(x = cor(m), col = col, symm = TRUE) ############# ## matematicke funkce curve(5*x**3 + 6*x**2 - 5, -2, 2 , col = "blue", main = "CUBIC CURVE") curve(3 * exp(-x/2), from = 0, to = 10, ylim = c(0, 2), ylab = "",col = "red", lwd = 2) curve(4 * exp(-x), add = T, lty = 4, col = "blue", lwd = 2) curve(2.5 * exp(-x/3), add = T, lty = 3, col = "darkgreen", lwd = 2) text(3, 1.5, expression(paste(3 * e^(-x/2))), col = "red") text(0.5, 1, expression(paste(4 * e^(-x))), col = "blue" ) text(7, 0.6, expression(paste(2.5 * e^(-x/3))), col = "darkgreen" ) x <- seq(-4, 4, 0.2) y <- 2*x^2 + 4*x - 7 plot(x, y) plot(x, y, pch = 16, col = "red") plot(x, y, pch = 16, col = 1, xlim = c(-8, 8), ylim = c(-20, 50), main = "MY PLOT", xlab = "X VARIABLE" , ylab = "Y VARIABLE") plot(x, y,pch=16,xlab="First",ylab="Second", main="RATINGS", cex = 1.5, cex.lab = 1.5, cex.main = 1.6, xlim=c(0,100), ylim=c(0,100)) points(x, y,col=4, pch=19) rug(x) # jen osa x rug(y, side=2) # jen osa y lines(x, y,col=2) abline(h = 0) abline(v = 0) abline(-10, 2) # primka: usek -10 a smernice 2. abline(lm(y ~ x)) text(4, 20, "TEXT") text(location, "text", pos=2) mtext("text to place", side, line=n, ...) legend(-8,36,"This is my Legend") legend(bottomleft,"This is my Legend") title(main="PLOT", col.main="red", font.main=2) x <- seq(-pi, pi, length = 50) y <- sin(x) plot(x, y, pch = 17, cex = 0.7, col = "darkgreen") lines(x, y, col = "darkgreen") x <- seq(-pi, pi, length = 1000) y <- sin(x) plot(x, y, type = "l") points(x, y, col = "darkgreen") X <- c(1, 2, 3, 4, 5, 6, 7) Y1 <- c(2, 4, 5, 7, 12, 14, 16) Y2 <- c(3, 6, 7, 8, 9, 11, 12) plot(X, Y1, type="b", pch = 17, cex=1.2, col="darkgreen", ylim=c(0,20)) lines(Y2, type="b", pch=16, lty=2, col="blue") plot(X, Y1, type="b", pch = 17, cex=1.2, col="darkgreen", ylim=c(0,20)) points(X, Y2, type="b", pch=16, lty=2, col="blue") plot(X, Y1, type="h", pch = 17, cex=1.2, col="darkgreen", ylim=c(0,20)) points(X, Y1, type="b", pch=16, lty=2, col="blue") plot(X, Y1, type="s", pch = 17, cex=1.2, col="darkgreen", ylim=c(0,20)) points(X, Y1, type="p", pch=16, lty=2, col="blue") # growth of orange trees data(Orange) head(Orange) plot(Orange$age, Orange$circumference) plot(circumference~age, data=Orange) library(lattice) xyplot(circumference~age, data=Orange) library(ggplot2) qplot(age, circumference, data=Orange) qplot(Orange$age, Orange$circumference) ggplot(Orange,aes(x=age,y=circumference))+ geom_point() ggplot(Orange)+ geom_point(aes(x=age,y=circumference)) ggplot()+ geom_point(aes(x=age,y=circumference),data=Orange) plot(Petal.Length~Petal.Width, data=iris) rug(iris$Petal.Width,col=3,ticksize=0.02) rug(iris$Petal.Length,side=2,col=2,ticksize=0.02) rug(iris$Petal.Width,side=3,col=3,ticksize=0.02) rug(iris$Petal.Length,side=4,col=2,ticksize=0.02) plot(circumference~age, data=Orange) abline(lm(circumference~age, data=Orange), col="blue",lty=1,lwd=1) lines(loess.smooth(Orange$age,Orange$circumference), col="red",lty=1,lwd=1) library(lattice) myPanel <- function(x,y){ panel.xyplot(x,y,col="black") # Add the observations panel.lmline(x,y,col="blue") # Add the regression panel.loess(x,y,col="red") # Add the smooth line } xyplot(circumference~age, data=Orange, panel=myPanel) qplot(age,circumference, data=Orange, geom = c("point","smooth"), xlab = "age", ylab = "circumference", method = "lm") + geom_smooth(color="red") #### coplot(circumference ~ age | Tree, data = Orange, show.given = FALSE) library(ggplot2) qplot(age, circumference, data=Orange, facets=~Tree) qplot(circumference,age, data=Orange, geom=c("line","point"),facets=~Tree) library(lattice) xyplot(circumference ~ age|Tree, data=Orange) xyplot(circumference ~ age|Tree, data=Orange, type="b") library(lattice) myPanel <- function(x,y){ panel.xyplot(x,y, type="b") #the observations } xyplot(circumference ~ age|Tree, data=Orange, panel=myPanel) par(mfrow=c(2,3)) plot(circumference~age, data=Orange[Orange$Tree==1,], type="p") plot(circumference~age, data=Orange[Orange$Tree==2,], type="p") plot(circumference~age, data=Orange[Orange$Tree==3,], type="p") plot(circumference~age, data=Orange[Orange$Tree==4,], type="p") plot(circumference~age, data=Orange[Orange$Tree==5,], type="p") par(mfrow=c(1,1)) library(ggplot2) g1 = qplot(age,circumference, data=Orange[Orange$Tree==1,], geom="point") g2 = qplot(age,circumference, data=Orange[Orange$Tree==2,], geom="point") g3 = qplot(age,circumference, data=Orange[Orange$Tree==3,], geom="point") g4 = qplot(age,circumference, data=Orange[Orange$Tree==4,], geom="point") g5 = qplot(age,circumference, data=Orange[Orange$Tree==5,], geom="point") library(gridExtra) grid.arrange(g1,g2,g3,g4,g5,nrow=2,ncol=3) #### plot(circumference~age, data=Orange[Orange$Tree==1,]) plot(circumference~age, data = Orange, subset = Tree == 2) plot(circumference~age, data=Orange, col=0) points(circumference~age, data=Orange[Orange$Tree==1,]) library(ggplot2) qplot(age,circumference, data=Orange[Orange$Tree==1,], geom="point") qplot(age,circumference, data=Orange[Orange$Tree==1,],geom=c("line","point")) plot(iris$Petal.Length,iris$Petal.Width) points(iris$Petal.Length[ff=="setosa"],iris$Petal.Width[ff=="setosa"],col=2,pch=19) points(iris$Petal.Length[ff=="versicolor"],iris$Petal.Width[ff=="versicolor"],col=3,pch=19) points(iris$Petal.Length[ff=="virginica"],iris$Petal.Width[ff=="virginica"],col=4,pch=19) library(lattice) xyplot(Petal.Width ~ Petal.Length, data=iris, groups=Species, type="p") library(ggplot2) ggplot(iris, aes(Petal.Length, Petal.Width, colour=Species)) + geom_point() #### Ellipsoid Hull or Spanning Ellipsoid of a Point Set library(car) dataEllipse(iris$Petal.Length, iris$Petal.Width) dataEllipse(iris$Petal.Length, iris$Petal.Width,groups = as.factor(iris$Species),col = c(2,3,4)) library(ggplot2) ggplot(iris, aes(Petal.Length, Petal.Width, colour=Species)) + geom_point() + stat_ellipse() ggplot(iris, aes(Petal.Length, Petal.Width, colour=Species)) + geom_point() + stat_ellipse(geom = "polygon",aes(fill = Species),alpha = 0.25) #### Convex Hull library(siar) x <- iris$Petal.Length y <- iris$Petal.Width ff = iris$Species plot(x,y, col=1,xlab="Sepal.Length",ylab="Sepal.Width") points(x[ff=="setosa"], y[ff=="setosa"], pch=16, col=2, cex=1.2) chnsu <-convexhull(x[ff=="setosa"], y[ff=="setosa"]) points(chnsu$xcoords, chnsu$ycoords,type="b", col=2, cex=1.2) points(x[ff=="versicolor"], y[ff=="versicolor"], pch=16, col=3, cex=1.2) chnxt <-convexhull(x[ff=="versicolor"], y[ff=="versicolor"]) points(chnxt$xcoords, chnxt$ycoords,type="b", col=3, cex=1.2) points(x[ff=="virginica"], y[ff=="virginica"], pch=16, col=4, cex=1.2) chnps <-convexhull(x[ff=="virginica"], y[ff=="virginica"]) points(chnps$xcoords, chnps$ycoords,type="b", col=4, cex=1.2) plot(circumference~age, data=Orange[Orange$Tree==1,],col=0) text(circumference~age,data=Orange[Orange$Tree==1,],names=rownames(Orange),cex=0.7,col=2) plot(circumference~age, data=Orange[Orange$Tree==1,],col=1) text(circumference~age,data=Orange[Orange$Tree==1,],labels=rownames(Orange[Orange$Tree==1,]),pos=3,cex=0.7,col=2) plot(circumference~age, data=Orange[Orange$Tree==1,],col=1) ind = identify(Orange[Orange$Tree==1,]$age,Orange[Orange$Tree==1,]$circumference,pos=FALSE,n=1,plot=FALSE) points(Orange[Orange$Tree==1,]$age[ind],Orange[Orange$Tree==1,]$circumference[ind],pch=19,col=3) text(Orange[Orange$Tree==1,]$age[ind],Orange[Orange$Tree==1,]$circumference[ind],labels=rownames(Orange)[ind],pos=3,cex=0.7,col=4) plot(circumference~age, data=Orange[Orange$Tree==1,],col=1) ind = locator(1) points(ind$x,ind$y,pch=19,col=3) text(ind$x,ind$y,labels=":-)",pos=3,cex=1,col=4) library(plotrix) plot(circumference~age, data=Orange[Orange$Tree==1,],col=1) plotCI(Orange[Orange$Tree==1,]$age,Orange[Orange$Tree==1,]$circumference,uiw=Orange[Orange$Tree==1,]$age*0.1,err="x",add=TRUE,col=1,scol=1,sfrac=.005) plotCI(Orange[Orange$Tree==1,]$age,Orange[Orange$Tree==1,]$circumference,uiw=Orange[Orange$Tree==1,]$circumference*0.1,err="y",add=TRUE,col=1,scol=1,sfrac=.005) library(ggplot2) ggplot(Orange[Orange$Tree==1,], aes(x=age, y=circumference)) + geom_point()+ geom_errorbar(aes(xmin=age*0.9, xmax=age*1.1), width=.2, position=position_dodge(0.05))+ geom_errorbar(aes(ymin=circumference*0.9, ymax=circumference*1.1), width=.2, position=position_dodge(0.05)) plot(circumference~age, data=Orange, col=0) points(circumference~age, data=Orange[Orange$Tree==1,], type="b", col=2) points(circumference~age, data=Orange[Orange$Tree==2,], type="b", col=3) points(circumference~age, data=Orange[Orange$Tree==3,], type="b", col=4) points(circumference~age, data=Orange[Orange$Tree==4,], type="b", col="brown") points(circumference~age, data=Orange[Orange$Tree==5,], type="b", col=6) legend("topleft",col=c(2:4,"brown",6), legend = c(1:5),pch = 1,lty = 1) library(ggplot2) qplot(age,circumference,data=Orange,color=Tree, geom=c("line","point")) ggplot(data=Orange, aes(y=circumference,x=age, color=Tree))+ geom_point()+ geom_line() library(lattice) xyplot(circumference ~ age, data=Orange, groups=Tree, type="b") qplot(age,circumference,data=Orange, color=Tree, geom=c("point","smooth"),method="lm",se=FALSE) ggplot(data=Orange, aes(x=circumference,y=age, color=Tree))+ geom_point()+ stat_smooth(method="lm",se=FALSE) ### Bublinovy graf ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width)) + geom_point(aes(size=Sepal.Length)) ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width)) + geom_point(aes(size=Sepal.Length, color=Species)) ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width)) + geom_point(aes(size=Sepal.Length, color=Sepal.Width)) #### Symbols library(ggplot2) library(ggChernoff) ggplot(iris, aes(Sepal.Length, Sepal.Width, smile = Petal.Length, fill = Species)) + geom_chernoff() ggplot(iris, aes(Petal.Length, Petal.Width, smile = Sepal.Width, fill = Species)) + geom_chernoff() library(symbols) # type of symbol plot:bar,profile,star,sun,polygon,face,stick,icon. symbol(iris,type="face",coorx=iris$Sepal.Length,coory=iris$Sepal.Width,colout=3) symbol(iris,type="face",coorx=iris$Petal.Length,coory=iris$Petal.Width,colout=3) symbol(iris,type="icon",coorx=iris$Petal.Length,coory=iris$Petal.Width,colout=3) library(MASS) x <- iris$Petal.Width y <- iris$Petal.Length z <- kde2d(x, y) plot(x, y, pch = 19, col=4) contour(z, lwd = 1, add = TRUE, col = 1) smoothScatter(iris$Petal.Width,iris$Petal.Length) library(hexbin) x <- iris$Petal.Width y <- iris$Petal.Length hex <- hexbin(x, y) plot(hex,legend = FALSE) plot(hex,legend = FALSE, colramp = colorRampPalette(hcl.colors(12, "GnBu")))