SlideShare ist ein Scribd-Unternehmen logo
1 von 2
Downloaden Sie, um offline zu lesen
Visualization of contingency tables in R using mosaic function form vcd
                                 package
#IMPORT vcd PACKAGE
> library(vcd)
>
> #SIMULATE DATA
> set.seed(1984)
> #municipalities#
> municipality <- sample(c("Andrijevica","Bar","Berane","Bijelo
Polje","Budva","Cetinje",
+                            "Danilovgrad", "Herceg
Novi","Kolasin","Kotor","Mojkovac","Niksic",
+
"Plav","Pluzine","Pljevlja","Podgorica","Rozaje","Savnik","Tivat","Ulcinj","Zabljak"),
+                          1000, rep=TRUE)
> #enumeration area type#
> ea_type <- sample(factor(c("Urban", "Rural"), levels=c("Urban", "Rural"),
order=TRUE),
+                     1000, rep=TRUE)
>
> #CREATE CONTINGENCY TABLE
> con_table <- table(municipality, ea_type)
> con_table
               ea_type
municipality    Urban Rural
  Andrijevica      23     29
  Bar              19     18
  Berane           25     19
  Bijelo Polje     27     22
  Budva            26     21
  Cetinje          27     26
  Danilovgrad      32     20
  Herceg Novi      26     25
  Kolasin          32     18
  Kotor            33     22
  Mojkovac         18     27
  Niksic           24     16
  Plav             22     32
  Pljevlja         30     17
  Pluzine          16     25
  Podgorica        19     22
  Rozaje           27     25
  Savnik           26     27
  Tivat            18     25
  Ulcinj           22     20
  Zabljak          28     24
> #GIVE NAMES TO TABLE DIMENSIONS#
> names(dimnames(con_table)) <- c("Municipality", "Enumeration Area Type")
>
> #CREATE MOSAIC PLOT
> windows(width=18, height=17)
> mosaic(con_table,pop=FALSE, main="Visualisation of Contingency table usingn mosaic
function from vcd package",
+        highlighting=1:2, highlighting_fill=c("lightblue", "gray"),
+        main_gp=gpar(fontsize=13, fontfamily="serif"),
+        sub_gp=gpar(fontsize=12, fontfamily="serif", col="red"),
+        labeling_args=list(rot_labels=c(bottom=0,top=0, left=
0),gp_labels=gpar(fontsize=9, fontfamily="serif"),
+                            gp_varnames=gpar(fontsize=11,fontfamily="serif"),
+                            offset_labels=c(0,0,0,1.45),
+                          offset_varnames=c(0.3,0,0,2.5)))
>
> #ADD LABLES TO PLOT
> labeling_cells(text=con_table , clip_cells=FALSE,
gp_text=gpar(fontsize=8))(con_table)
>
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] grid      stats     graphics   grDevices utils   datasets   methods   base

other attached packages:
[1] RODBC_1.3-6      vcd_1.2-13       colorspace_1.2-0 MASS_7.3-22

loaded via a namespace (and not attached):
[1] tools_2.15.2

Weitere ähnliche Inhalte

Andere mochten auch

Debt Collection Report - using R in Finance
Debt Collection Report  - using R in FinanceDebt Collection Report  - using R in Finance
Debt Collection Report - using R in FinanceAndrija Djurovic
 
Roll Rate Model - Using R in Finance
Roll Rate Model - Using R in FinanceRoll Rate Model - Using R in Finance
Roll Rate Model - Using R in FinanceAndrija Djurovic
 
Seguimiento del modelo interno de riesgo
Seguimiento del modelo interno de riesgoSeguimiento del modelo interno de riesgo
Seguimiento del modelo interno de riesgoAIS
 
Data Exploration, Validation and Sanitization
Data Exploration, Validation and SanitizationData Exploration, Validation and Sanitization
Data Exploration, Validation and SanitizationVenkata Reddy Konasani
 
Model building in credit card and loan approval
Model building in credit card and loan approval Model building in credit card and loan approval
Model building in credit card and loan approval Venkata Reddy Konasani
 

Andere mochten auch (13)

Polar plots with R
Polar plots with RPolar plots with R
Polar plots with R
 
Debt Collection Report - using R in Finance
Debt Collection Report  - using R in FinanceDebt Collection Report  - using R in Finance
Debt Collection Report - using R in Finance
 
R and Access 2007
R and Access 2007R and Access 2007
R and Access 2007
 
Data aggregation in R
Data aggregation in RData aggregation in R
Data aggregation in R
 
R-Excel Integration
R-Excel IntegrationR-Excel Integration
R-Excel Integration
 
Roll Rate Model - Using R in Finance
Roll Rate Model - Using R in FinanceRoll Rate Model - Using R in Finance
Roll Rate Model - Using R in Finance
 
Seguimiento del modelo interno de riesgo
Seguimiento del modelo interno de riesgoSeguimiento del modelo interno de riesgo
Seguimiento del modelo interno de riesgo
 
Excel/R
Excel/RExcel/R
Excel/R
 
Testing of hypothesis case study
Testing of hypothesis case study Testing of hypothesis case study
Testing of hypothesis case study
 
Data Exploration, Validation and Sanitization
Data Exploration, Validation and SanitizationData Exploration, Validation and Sanitization
Data Exploration, Validation and Sanitization
 
Model building in credit card and loan approval
Model building in credit card and loan approval Model building in credit card and loan approval
Model building in credit card and loan approval
 
Decision tree
Decision treeDecision tree
Decision tree
 
Credit Risk Model Building Steps
Credit Risk Model Building StepsCredit Risk Model Building Steps
Credit Risk Model Building Steps
 

Kürzlich hochgeladen

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 

Kürzlich hochgeladen (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 

Visualization of contingency table in R - vcd package

  • 1. Visualization of contingency tables in R using mosaic function form vcd package #IMPORT vcd PACKAGE > library(vcd) > > #SIMULATE DATA > set.seed(1984) > #municipalities# > municipality <- sample(c("Andrijevica","Bar","Berane","Bijelo Polje","Budva","Cetinje", + "Danilovgrad", "Herceg Novi","Kolasin","Kotor","Mojkovac","Niksic", + "Plav","Pluzine","Pljevlja","Podgorica","Rozaje","Savnik","Tivat","Ulcinj","Zabljak"), + 1000, rep=TRUE) > #enumeration area type# > ea_type <- sample(factor(c("Urban", "Rural"), levels=c("Urban", "Rural"), order=TRUE), + 1000, rep=TRUE) > > #CREATE CONTINGENCY TABLE > con_table <- table(municipality, ea_type) > con_table ea_type municipality Urban Rural Andrijevica 23 29 Bar 19 18 Berane 25 19 Bijelo Polje 27 22 Budva 26 21 Cetinje 27 26 Danilovgrad 32 20 Herceg Novi 26 25 Kolasin 32 18 Kotor 33 22 Mojkovac 18 27 Niksic 24 16 Plav 22 32 Pljevlja 30 17 Pluzine 16 25 Podgorica 19 22 Rozaje 27 25 Savnik 26 27 Tivat 18 25 Ulcinj 22 20 Zabljak 28 24 > #GIVE NAMES TO TABLE DIMENSIONS# > names(dimnames(con_table)) <- c("Municipality", "Enumeration Area Type") > > #CREATE MOSAIC PLOT > windows(width=18, height=17) > mosaic(con_table,pop=FALSE, main="Visualisation of Contingency table usingn mosaic function from vcd package", + highlighting=1:2, highlighting_fill=c("lightblue", "gray"), + main_gp=gpar(fontsize=13, fontfamily="serif"), + sub_gp=gpar(fontsize=12, fontfamily="serif", col="red"), + labeling_args=list(rot_labels=c(bottom=0,top=0, left= 0),gp_labels=gpar(fontsize=9, fontfamily="serif"), + gp_varnames=gpar(fontsize=11,fontfamily="serif"), + offset_labels=c(0,0,0,1.45),
  • 2. + offset_varnames=c(0.3,0,0,2.5))) > > #ADD LABLES TO PLOT > labeling_cells(text=con_table , clip_cells=FALSE, gp_text=gpar(fontsize=8))(con_table) > > sessionInfo() R version 2.15.2 (2012-10-26) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] RODBC_1.3-6 vcd_1.2-13 colorspace_1.2-0 MASS_7.3-22 loaded via a namespace (and not attached): [1] tools_2.15.2