## Use latex in plot labels library(latex2exp) # install.packages("latex2exp") plot(x, xlim=c(0, 4), ylim=c(0, 10), xlab='x', ylab=TeX('$\\alpha x^\\alpha$, where $\\alpha \\in 1\\ldots 5$'), type='n', main=TeX('Using $\\LaTeX$ for plotting in base graphics!')) ## Color fill dens <- density(mtcars$mpg) plot(dens, frame=F, main = "Density plot of mpg") polygon(dens, col = "steelblue") ### ----------------------------------- ### Power analysis with color fill pdf plottwo <- function(n,mu,sigma=10){ layout(matrix(1:2, 2, 1)) muA =sqrt(n)*(mu-5)/sigma t=seq(-6,6,.1) t2=seq(1.64,6,.1) plot(t, dnorm(t,0,1), frame=F, type='l', lwd=2, xlim=c(-5,5), xlab="", ylab="", main=TeX('$H_0:$ $\\mu = 5$')) polygon(c(1.64,t2), c(0,dnorm(t2,0,1)), col = "red") abline(h=0, v=1.64) text(2.5, .1, TeX('$\\alpha = .05$')) plot(t, dnorm(t,muA,1), frame=F, type='l', lwd=2, xlim=c(-5,5), xlab="", ylab="", main=TeX('$H_0:$ $\\mu > 5$')) polygon(c(1.64,t2), c(0,dnorm(t2,muA,1)), col = "steelblue") abline(h=0, v=1.64) text(3, .2, TeX('$Power = $')) text(3.5, .15, paste(round(1-pnorm(1.64, muA, 1),3))) text(-3, .4, TeX('$\\mu = $')) text(-2.5,.4, paste(mu)) text(-3, .35, TeX('$n = $')) text(-2.5,.35, paste(n)) } plottwo(n=100, mu=8) #--------------------------------------- # Raw Flip coin Hypothesis testing with colored hist itt=1000 n=50 X0 <- rbinom(itt, n, .5) X1 <- rbinom(itt, n, .6) layout(matrix(1:2, 2, 1)) htgm0 <- hist( X0, breaks=seq(0,n,1), plot=FALSE) htgm0$counts <- dbinom(htgm0$breaks, n, .5) red0.ix = (htgm0$breaks>=30) plot(htgm0, col=ifelse(red0.ix, "red", "gray80"), xlim=c(0,n), xlab="Number of Heads", ylab="", main=paste("n=50: If p=.5") ) text(30+5, .02, paste(round(sum(htgm0$counts[red0.ix]), 3))) htgm1 <- hist( X1, breaks=seq(0,n,1), plot=FALSE) htgm1$counts <- dbinom(htgm1$breaks, n, .6) red1.ix = (htgm1$breaks>=30) plot(htgm1, col=ifelse(red1.ix, "red", "gray80"), xlim=c(0,n), xlab="Number of Heads", ylab="", main=paste("If p=.6") ) text(30+10, 0.02, paste(round(sum(htgm1$counts[red1.ix]), 3))) sum(htgm1$counts[red1.ix]) cat( sprintf("Perc. of days with pos. profit %s", mean(Av.X>0))) ###--- pdf with colored section (ggplot2) library(tidyverse) b=3 theta=2 t=seq(0,10,.01) dframe <- tibble(t=t, f=dexp(t, 1/theta)) ggplot(dframe, aes(x=t, y=f)) + geom_line(lwd=1.3) + geom_area(aes(x=ifelse(5% mutate(A=as.factor(A)) ggplot(dframe, aes(x=t, y=f)) + geom_area(aes(fill=as.factor(A)), lwd=1.5, alpha=1) + geom_vline(xintercept = b, lwd=1) + theme(legend.position="left") # "top" "none" "right" "left" "bottom" ###--- Bar Plot vs Area Plot data("diamonds") head(diamonds) #- Bar plot ggplot(diamonds, aes(x=price, fill=cut)) + geom_bar(stat="bin") #- Area plot ggplot(diamonds, aes(x=price, fill=cut)) + geom_area(stat="bin") + scale_fill_brewer(palette="Dark2")