+ All Categories
Home > Data & Analytics > Rgraphics-pyramid plot,likert,timeline,....

Rgraphics-pyramid plot,likert,timeline,....

Date post: 23-Jan-2018
Category:
Upload: volkan-oban
View: 141 times
Download: 0 times
Share this document with a friend
20
Reference: http://jason.bryer.org/likert/ install.packages("PSAboot", repos = "http://cran.r-project.org") or devtools::install_github("PSAboot", "jbryer") prepared by Volkan OBAN >library(PSAboot) > lalonde.formu <- treat ~ age + I(age^2) + educ + I(educ^2) + black + hisp an + married + nodegree + + re74 + I(re74^2) + re75 + I(re75^2) + re74 + re75 > boot.lalonde <- PSAboot(Tr = lalonde$treat, Y = lalonde$re78, X = lalonde , formu = lalonde.formu, M = 100, seed = 2112) > plot(boot.lalonde) > boxplot(boot.lalonde
Transcript
Page 1: Rgraphics-pyramid plot,likert,timeline,....

Reference: http://jason.bryer.org/likert/

install.packages("PSAboot", repos = "http://cran.r-project.org")

or

devtools::install_github("PSAboot", "jbryer")

prepared by Volkan OBAN

>library(PSAboot)

> lalonde.formu <- treat ~ age + I(age^2) + educ + I(educ^2) + black + hispan + married + nodegree + + re74 + I(re74^2) + re75 + I(re75^2) + re74 + re75 > boot.lalonde <- PSAboot(Tr = lalonde$treat, Y = lalonde$re78, X = lalonde, formu = lalonde.formu, M = 100, seed = 2112) > plot(boot.lalonde) > boxplot(boot.lalonde

Page 2: Rgraphics-pyramid plot,likert,timeline,....
Page 3: Rgraphics-pyramid plot,likert,timeline,....

> lalonde.bal <- balance(boot.lalonde) > plot(lalonde.bal)

Page 4: Rgraphics-pyramid plot,likert,timeline,....

boxplot(lalonde.bal)

Page 5: Rgraphics-pyramid plot,likert,timeline,....

Example:

install.packages('TriMatch',repos='http://cran.r-project.org')

require(TriMatch)

vignette('TriMatch')

demo(tutoring)

demo(nmes)

Page 6: Rgraphics-pyramid plot,likert,timeline,....
Page 7: Rgraphics-pyramid plot,likert,timeline,....
Page 8: Rgraphics-pyramid plot,likert,timeline,....
Page 9: Rgraphics-pyramid plot,likert,timeline,....
Page 10: Rgraphics-pyramid plot,likert,timeline,....

timeline

install.packages('timeline', repos='http://cran.r-project.org')

require(timeline)

data(ww2)

timeline(ww2, ww2.events, event.spots=2, event.label='', event.above=FALSE)

Page 11: Rgraphics-pyramid plot,likert,timeline,....

timelineShinyDemo()

Example:

> require(devtools) > install_github('likert', 'jbryer')

require(likert) data(pisaitems) > items28 <- pisaitems[, substr(names(pisaitems), 1, 5) == "ST24Q"] > items28 <- rename(items28, c(ST24Q01 = "I read only if I have to.", ST24Q02 = "Reading is one of my favorite hobbies.", + ST24Q03 = "I like talking about books with other people.", ST24Q04 = "I find it hard to finish books.", ST24Q05 = "I feel happy if I receive a book as a present.", + ST24Q06 = "For me, reading is a waste of time.", ST24Q07 = "I enjoy going to a bookstore or a library.", ST24Q08 = "I read only to get information that I need.",

Page 12: Rgraphics-pyramid plot,likert,timeline,....

+ ST24Q09 = "I cannot sit still and read for more than a few minutes.", ST24Q10 = "I like to express my opinions about books I have read.", + ST24Q11 = "I like to exchange books with my friends")) > l28 <- likert(items28) > plot(l28)

Page 13: Rgraphics-pyramid plot,likert,timeline,....

Example:

>require(likert) >library(irutils) >library(reshape) > resp <- data.frame(replicate(66, sample(1:5, 82, replace=TRUE))) > resp <- data.frame(lapply(resp, factor, ordered=TRUE, + levels=1:5, + labels=c("Strongly disagree","Disagree", + "Neutral","Agree","Strongly Agree"))) > grp <- gl(2, 82/2, labels=LETTERS[1:2]) # say equal group size for simplicity > resp.likert <- likert(resp) > resp.likert > plot(likert(resp[,1:12]))

Page 14: Rgraphics-pyramid plot,likert,timeline,....

Example:

set.seed(123) > xvar <- round (rnorm (100, 54, 10), 0) > xyvar <- round (rnorm (100, 54, 10), 0) > myd <- data.frame (xvar, xyvar) > valut <- as.numeric (cut(c(myd$xvar,myd$xyvar), 12)) > myd$xwt <- valut[1:100] > myd$xywt <- valut[101:200] > xy.pop <- data.frame (table (myd$xywt)) > xx.pop <- data.frame (table (myd$xwt)) > > > library(plotrix) > par(mar=pyramid.plot(xy.pop$Freq,xx.pop$Freq, + main="Population Pyramid",lxcol="blue",rxcol= "pink", + gap=0,show.values=F))

Page 15: Rgraphics-pyramid plot,likert,timeline,....

Example:

> set.seed (123) > xvar <- round (rnorm (100, 54, 10), 0) > xyvar <- round (rnorm (100, 54, 10), 0) > myd <- data.frame (xvar, xyvar) > valut <- as.numeric (cut(c(myd$xvar,myd$xyvar), 12)) > myd$xwt <- valut[1:100] > myd$xywt <- valut[101:200] > xy.pop <- data.frame (table (myd$xywt)) > xx.pop <- data.frame (table (myd$xwt)) > > > stickBoy <- function() { + grid.circle(x=.5, y=.8, r=.1, gp=gpar(fill="red")) + grid.lines(c(.5,.5), c(.7,.2)) # vertical line for body + grid.lines(c(.5,.6), c(.6,.7)) # right arm + grid.lines(c(.5,.4), c(.6,.7)) # left arm + grid.lines(c(.5,.65), c(.2,0)) # right leg + grid.lines(c(.5,.35), c(.2,0)) # left leg + grid.lines(c(.5,.5), c(.7,.2)) # vertical line for body + grid.text(x=.5,y=-0.3,label ='Male', + gp =gpar(col='white',fontface=2,fontsize=32)) # vertical line for body + } > > stickGirl <- function() { + grid.circle(x=.5, y=.8, r=.1, gp=gpar(fill="blue")) + grid.lines(c(.5,.5), c(.7,.2)) # vertical line for body + grid.lines(c(.5,.6), c(.6,.7)) # right arm + grid.lines(c(.5,.4), c(.6,.7)) # left arm + grid.lines(c(.5,.65), c(.2,0)) # right leg + grid.lines(c(.5,.35), c(.2,0)) # left leg + grid.lines(c(.35,.65), c(0,0)) # horizontal line for body + grid.text(x=.5,y=-0.3,label ='Female', + gp =gpar(col='white',fontface=2,fontsize=32)) # vertical line for body + } > > xscale <- c(0, max(c(xx.pop$Freq,xy.pop$Freq)))* 5 > levels <- nlevels(xy.pop$Var1) > barYscale<- xy.pop$Var1 > vp <- plotViewport(c(5, 4, 4, 1), + yscale = range(0:levels)*1.05, + xscale =xscale) > > > pushViewport(vp) > > grid.yaxis(at=c(1:levels)) > pushViewport(viewport(width = unit(0.5, "npc"),just='right', + xscale =rev(xscale))) > grid.xaxis() > popViewport() > > pushViewport(viewport(width = unit(0.5, "npc"),just='left', + xscale = xscale)) > grid.xaxis()

Page 16: Rgraphics-pyramid plot,likert,timeline,....

> popViewport() > > grid.grill(gp=gpar(fill=NA,col='white',lwd=3), + h = unit(seq(0,levels), "native")) > grid.rect(gp=gpar(fill=rgb(0,0.2,1,0.5)), + width = unit(0.5, "npc"),just='right') > > grid.rect(gp=gpar(fill=rgb(1,0.2,0.3,0.5)), + width = unit(0.5, "npc"),just=c('left')) > > vv.xy <- xy.pop$Freq > vv.xx <- c(xx.pop$Freq,0) > > grid.polygon(x = unit.c(unit(0.5,'npc')-unit(vv.xy,'native'), + unit(0.5,'npc')+unit(rev(vv.xx),'native')), + y = unit.c(unit(1:levels,'native'), + unit(rev(1:levels),'native')), + gp=gpar(fill=rgb(1,1,1,0.8),col='white')) > > grid.grill(gp=gpar(fill=NA,col='white',lwd=3,alpha=0.8), + h = unit(seq(0,levels), "native")) > popViewport() > > ## some fun here > vp1 <- viewport(x=0.2, y=0.75, width=0.2, height=0.2,gp=gpar(lwd=2,col='white'),angle=30) > pushViewport(vp1) > stickBoy() > popViewport() > vp1 <- viewport(x=0.9, y=0.75, width=0.2, height=0.2,,gp=gpar(lwd=2,col='white'),angle=330) > pushViewport(vp1) > stickGirl() > popViewport()

Page 17: Rgraphics-pyramid plot,likert,timeline,....

Example:

> library(scales) > xy.poly <- data.frame(Freq=c(xy.pop$Freq, rep(0,nrow(xy.pop))), + Var1=c(xy.pop$Var1, rev(xy.pop$Var1))) > xx.poly <- data.frame(Freq=c(xx.pop$Freq, rep(0,nrow(xx.pop))), + Var1=c(xx.pop$Var1, rev(xx.pop$Var1))) > xrange <- range(c(xy.poly$Freq, xx.poly$Freq)) > yrange <- range(c(xy.poly$Var1, xx.poly$Var1)) > > par(mfcol=c(1,2)) > par(mar=c(5,4,4,0)) > plot(xy.poly,type="n", main="Men", xlab="", ylab="", xaxs="i", + xlim=rev(xrange), ylim=yrange, axes=FALSE) > rect(-1,0,100,100, col="blue") > abline(h=0:15, col="white", lty=3) > polygon(xy.poly, col=alpha("grey",0.6)) > axis(1, at=seq(0,20,by=5)) > axis(2, las=2) > box() >

Page 18: Rgraphics-pyramid plot,likert,timeline,....

> par(mar=c(5,0,4,4)) > plot(xx.poly,type="n", main="Women", xaxs="i", xlab="", ylab="", + xlim=xrange, ylim=yrange, axes=FALSE) > rect(-1,0,100,100, col="red") > abline(h=0:15, col="white", lty=3) > axis(1, at=seq(5,20,by=5)) > axis(4, las=2) > polygon(xx.poly, col=alpha("grey",0.6)) > box()

Reference: http://stackoverflow.com/questions/14245361/population-

pyramid-density-plot-in-r

Example:

library(ggplot2) library(plyr) library(gridExtra) ## The Data df <- data.frame(Type = sample(c('Male', 'Female', 'Female'), 1000, replace=TRUE),

Page 19: Rgraphics-pyramid plot,likert,timeline,....

Age = sample(18:60, 1000, replace=TRUE)) AgesFactor <- ordered(cut(df$Age, breaks = c(18,seq(20,60,5)), include.lowest = TRUE)) df$Age <- AgesFactor ## Plotting gg <- ggplot(data = df, aes(x=Age)) gg.male <- gg + geom_bar( data=subset(df,Type == 'Male'), aes( y = ..count../sum(..count..), fill = Age)) + scale_y_continuous('', labels = scales::percent) + theme(legend.position = 'none', axis.title.y = element_blank(), plot.title = element_text(size = 11.5), plot.margin=unit(c(0.1,0.2,0.1,-.1),"cm"), axis.ticks.y = element_blank(), axis.text.y = theme_bw()$axis.text.y) + ggtitle("Male") + coord_flip() gg.female <- gg + geom_bar( data=subset(df,Type == 'Female'), aes( y = ..count../sum(..count..), fill = Age)) + scale_y_continuous('', labels = scales::percent, trans = 'reverse') + theme(legend.position = 'none', axis.text.y = element_blank(), axis.ticks.y = element_blank(), plot.title = element_text(size = 11.5), plot.margin=unit(c(0.1,0,0.1,0.05),"cm")) + ggtitle("Female") + coord_flip() + ylab("Age") ## Plutting it together grid.arrange(gg.female, gg.male, widths=c(0.4,0.6), ncol=2 )

Page 20: Rgraphics-pyramid plot,likert,timeline,....

Ref: http://stackoverflow.com/questions/4559229/drawing-pyramid-plot-using-r-and-ggplot2


Recommended