Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Reference: http://jason.bryer.org/likert/
install.packages("PSAboot", repos = "http://cran.r-project.org")
or
devtools::in...
> lalonde.bal <- balance(boot.lalonde)
> plot(lalonde.bal)
boxplot(lalonde.bal)
Example:
install.packages('TriMatch',repos='http://cran.r-project.org')
require(TriMatch)
vignette('TriMatch')
demo(tutori...
timeline
install.packages('timeline', repos='http://cran.r-project.org')
require(timeline)
data(ww2)
timeline(ww2, ww2.eve...
timelineShinyDemo()
Example:
> require(devtools)
> install_github('likert', 'jbryer')
require(likert)
data(pisaitems)
> it...
+ ST24Q09 = "I cannot sit still and read for m
ore than a few minutes.", ST24Q10 = "I like to express my opinions about bo...
Example:
>require(likert)
>library(irutils)
>library(reshape)
> resp <- data.frame(replicate(66, sample(1:5, 82, replace=T...
Example:
set.seed(123)
> xvar <- round (rnorm (100, 54, 10), 0)
> xyvar <- round (rnorm (100, 54, 10), 0)
> myd <- data.fr...
Example:
> set.seed (123)
> xvar <- round (rnorm (100, 54, 10), 0)
> xyvar <- round (rnorm (100, 54, 10), 0)
> myd <- data...
> popViewport()
>
> grid.grill(gp=gpar(fill=NA,col='white',lwd=3),
+ h = unit(seq(0,levels), "native"))
> grid.rect(gp=gpa...
Example:
> library(scales)
> xy.poly <- data.frame(Freq=c(xy.pop$Freq, rep(0,nrow(xy.pop))),
+ Var1=c(xy.pop$Var1, rev(xy....
> par(mar=c(5,0,4,4))
> plot(xx.poly,type="n", main="Women", xaxs="i", xlab="", ylab="",
+ xlim=xrange, ylim=yrange, axes=...
Age = sample(18:60, 1000, replace=TRUE))
AgesFactor <- ordered(cut(df$Age, breaks = c(18,seq(20,60,5)),
include.lowest = T...
Ref: http://stackoverflow.com/questions/4559229/drawing-pyramid-plot-using-r-and-ggplot2
Rgraphics-pyramid plot,likert,timeline,....
Rgraphics-pyramid plot,likert,timeline,....
Rgraphics-pyramid plot,likert,timeline,....
Rgraphics-pyramid plot,likert,timeline,....
Rgraphics-pyramid plot,likert,timeline,....
Upcoming SlideShare
Loading in …5
×

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

193 views

Published on

R Data Visualization Examples:
Rgraphics-pyramid plot,likert,timeline,....
>require(likert)
>library(irutils)
>library(reshape)
>library(irutils)
......
.............

References:
http://jason.bryer.org/likert/
http://stackoverflow.com/questions/14245361/population-pyramid-density-plot-in-r
....

Published in: Data & Analytics
  • Be the first to comment

  • Be the first to like this

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

  1. 1. 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
  2. 2. > lalonde.bal <- balance(boot.lalonde) > plot(lalonde.bal)
  3. 3. boxplot(lalonde.bal)
  4. 4. Example: install.packages('TriMatch',repos='http://cran.r-project.org') require(TriMatch) vignette('TriMatch') demo(tutoring) demo(nmes)
  5. 5. 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)
  6. 6. 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.", ST24Q 02 = "Reading is one of my favorite hobbies.", + ST24Q03 = "I like talking about books with o ther people.", ST24Q04 = "I find it hard to finish books.", ST24Q05 = "I fe el happy if I receive a book as a present.", + ST24Q06 = "For me, reading is a waste of tim e.", ST24Q07 = "I enjoy going to a bookstore or a library.", ST24Q08 = "I r ead only to get information that I need.",
  7. 7. + ST24Q09 = "I cannot sit still and read for m ore than a few minutes.", ST24Q10 = "I like to express my opinions about bo oks I have read.", + ST24Q11 = "I like to exchange books with my friends")) > l28 <- likert(items28) > plot(l28)
  8. 8. 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 simpli city > resp.likert <- likert(resp) > resp.likert > plot(likert(resp[,1:12]))
  9. 9. 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))
  10. 10. 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 li ne 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 li ne 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()
  11. 11. > 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='w hite'),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()
  12. 12. 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() >
  13. 13. > 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),
  14. 14. 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 )
  15. 15. Ref: http://stackoverflow.com/questions/4559229/drawing-pyramid-plot-using-r-and-ggplot2

×