# NOT RUN {
# the main function also calls plotca with the default options
bd <- cabootcrs(DreamDataNames, datasetname="Maxwell's dream data",
varnames=c("Age groups","Severity of disturbance"),showresults=FALSE)
plotca(bd)
# }
# NOT RUN {
### Plot options for SCA:
# Note that Rstudio changes plots depending on the size of your plot window,
# so the picsize parameter (used for xlim, ylim in the plot command) is partially
# overridden, so warnings that a point is outside the plot limits may not be correct
# Plot with specified size to fit the whole of the arrows in without cropping
plotca(bd, picsize=c(-2.5,2.5))
# or smaller, note the warning
plotca(bd, picsize=c(-0.5,0.5))
# Replacing the plot titles with your own
plotca(bd, mytitles=c("Plot 1 Title line 1\nline 2\nline 3","Plot 2 Title line 1\nline 2\nline 3" ))
# All points in colour
plotca(bd,othersmonochrome=NULL)
# 90% regions in reds and blue
plotca(bd, plotsymbolscolours=c(3,"differentreds","*","blue"), crpercent=90)
# Many different colour schemes and ways of specifying colours and symbols
# Specify colour/colour scheme and symbols
plotca(bd, plotsymbolscolours=c(3,"differentreds","*","blue") )
plotca(bd, plotsymbolscolours=c(3,"viridis") )
plotca(bd, plotsymbolscolours="inferno" )
plotca(bd, plotsymbolscolours=colours()[641] )
# Just give a list of colours, one for each category point
plotca(bd,
plotsymbolscolours=c("green","blue","yellow","red","orange","red","blue","tan1","orchid4") )
plotca(bd, plotsymbolscolours=colours()[161:170] )
plotca(bd, plotsymbolscolours=colours()[c(111:115,561:564)] )
# This time using colo(u)rspace package colour palettes
library(colorspace)
plotca(bd, plotsymbolscolours=hcl.colors(9,palette="Peach") )
plotca(bd, plotsymbolscolours=hcl.colors(50,palette="Mint")[c(11:15,31:34)] )
plotca(bd,
plotsymbolscolours=c(sequential_hcl(bd@rows,"Blues 3"),sequential_hcl(bd@columns,"Reds 3")) )
# suppress labels for column points, to de-clutter row points picture,
# this is mostly useful for larger data sets than this one
plotca(bd, showcolumnlabels=FALSE)
# only show ellipses for rows 1, 1-2 and 1-3 respectively
plotca(bd, showrowcrs=1)
plotca(bd, showrowcrs=c(1,2))
plotca(bd, showrowcrs=1:3)
# plot axes 1 v 2, 1 v 3 and 2 v 3
plotca(bd, firstaxis=1, lastaxis=3, plotallpairs="all")
# If the cell values were all 10 times larger
bdx10 <- cabootcrs(10*DreamData)
plotca(bdx10,plottype = "french",picsize=c(-0.4,0.4))
# Various plots for a larger data set, note that the default colour scheme picks out
# males, females and ages because of the ordering of the rows
bs <- cabootcrs(SuicideData)
plotca(bs, picsize=c(-0.7,0.8))
plotca(bs, plottype="french", picsize=c(-0.7,0.8))
plotca(bs, plottype="french", picsize=c(-0.7,0.8),
plotsymbolscolours=c(".","inferno","+","black"))
# Note that the ellipses follow the horseshoe
bas <- cabootcrs(AsbestosData)
# more complicated plotting, define group structure in data frames
groupingsframe <- cbind( c(1:5,1:4), c(1,1,2,2,3,1,1,2,2) )
grouplabframe <- cbind( c(1,2,3,1,2), c("AB","CD","E","ab","cd"), c(19,20,21,"+","*"),
c("green","blue","yellow","red","orange"), "T" )
plotca(bd, groupings=groupingsframe, grouplabels=grouplabframe)
plotca(bd, groupings=groupingsframe, grouplabels=grouplabframe, plottype="french")
# This can also be used for custom colour schemes other than "differentreds" etc as
# defined in the plotsymbolscolours option, though note that R colours are not ordered in the
# way you might expect, so the colour scheme below is purely illustrative and not very sensible
customframe <- cbind( c(1:5,1:4), c(1:5,1:4) )
customlabframe <- cbind( c(1:5,1:4), rep("",9), c(rep(18,5),rep(19,4)),
colours()[c(seq(10,130,30),seq(440,590,50))], "T" )
plotca(bd, groupings=customframe, grouplabels=customlabframe)
### Plot options for MCA:
# Use one of the below, labelling row A as R:A or just A (etc) as preferred
bd3 <- cabootcrs(DreamData223by3, catype="mca",
datasetname="Dream data with extra random column")
bd3 <- cabootcrs(DreamData223by3, catype="mca", varandcat=FALSE,
datasetname="Dream data with extra random column")
# just variable 2
plotca(bd3,mcaoneploteach=2)
# just variables 1 and 3
plotca(bd3,mcaoneploteach=c(1,3))
# one plot showing CRs for all variable categories (busy)
plotca(bd3,mcaoneploteach=FALSE)
# each variable has its own colour
plotca(bd3,mcavariablecolours=TRUE)
# each category number has its own colour
plotca(bd3,mcacategorycolours=TRUE)
# draw arrows between successive ordered categories
plotca(bd3,likertarrows=TRUE)
# secondary points black rather than grey
plotca(bd3,othersmonochrome="black")
# 99% CRs
plotca(bd3,crpercent=99)
# Plot together CRs for the first category of each variable
plotca(bd3,showcolumncrs=c(1,6,10),mcaoneploteach=FALSE)
# Plot together CRs for the second category of each variable
plotca(bd3,showcolumncrs=c(2,7,11),mcaoneploteach=FALSE)
# One plot with CRs only for variable 3
plotca(bd3,showcolumncrs=10:12,mcaoneploteach=FALSE)
# Three plots, various colour schemes
plotca(bd3,othersmonochrome="black")
plotca(bd3,othersmonochrome="black",mcacategorycolours=TRUE)
plotca(bd3,mcavariablecolours=TRUE,likertarrows=TRUE)
# All on one plot, various colour schemes, very busy
plotca(bd3,mcaoneploteach=FALSE,showcolumncrs=1:5,othersmonochrome="black")
plotca(bd3,mcaoneploteach=FALSE,showcolumncrs=1:5,likertarrows=TRUE)
plotca(bd3,mcaoneploteach=FALSE,likertarrows=TRUE,mcacategorycolours=TRUE)
plotca(bd3,mcaoneploteach=FALSE,likertarrows=TRUE,mcavariablecolours=TRUE)
# Plots with more complicated colour and grouping structure, as above but now in MCA case.
# Note the need to duplicate both data frames as groupings must be specified for both rows
# and columns, though only columns are used.
# Note also that symbol types need to be defined either all as numbers or all as symbols
groupingsframe <- cbind(1:12,c(1,1,2,2,3,4,4,5,5,6,7,7))
groupingsframe <- rbind(groupingsframe,groupingsframe)
grouplabframe <- cbind( 1:7, c("AB","CD","E","ab","cd","v1","v23"), 19:25,
c("cyan","deepskyblue","blue","red","tomato","chartreuse","green"),
"T" )
grouplabframe <- rbind(grouplabframe,grouplabframe)
plotca(bd3, groupings=groupingsframe, grouplabels=grouplabframe,
mcaoneploteach=FALSE)
### Adding confidence ellipses to plots from ca package ca() and mjca() functions
## Simple CA
# Adding confidence ellipses for row points to plots from ca() using ellipse()
# Note: reflectaxes() is needed if cabootcrs() and ca() axes are reflected wrt each other
library(ca)
library(ellipse)
cad <- ca(DreamData)
plot(cad)
for (i in 1:dim(DreamData)[1]) {
lines( ellipse(x=covmat(bd,i,"row",1,2,FALSE), centre=bd@Rowprinccoord[i,cbind(1,2)],
npoints=1000),
cex=1, pch=".", col="blue")
}
# These plots can also be produced almost identically here
bd <- cabootcrs(DreamData, showresults=FALSE)
# both plots almost the same as the default plot from ca()
plotca(bd, plottype="french", showrowcrs=FALSE, showcolumncrs=FALSE, othersmonochrome=NULL,
plotsymbolscolours=c(19,"blue",17,"red"), picsize=c(-0.5,0.6) )
# plot almost the same as the ca() plot, but with ellipses added
plotca(bd, plottype="french", othersmonochrome=NULL, plotsymbolscolours=c(19,"blue",17,"red"),
picsize=c(-0.5,0.6))
## Multiple CA
# Adding confidence ellipses for category points to plots from mjca() using ellipse()
# Note that ca also uses standardised inertias and coordinates by default
library(ca)
library(ellipse)
cad3 <- mjca(DreamData223by3)
# Obtain covariance matrices, using same scalings and standardisations
bd3 <- cabootcrs(DreamData223by3, catype="mca", showresults=FALSE)
# Reflect axis 1 for consistency (may differ on other machines)
bd3 <- reflectaxes(bd3,1)
# Plot and then add ellipses for categories of variable 1 only
plot(cad3)
for (i in 1:bd3@Jk[1]) {
lines( ellipse(x=covmat(bd3,i,"column",1,2,FALSE), centre=bd3@Colprinccoord[i,cbind(1,2)],
npoints=1000),
cex=1, pch=".", col="red")
}
# These plots can also be produced almost identically here
plotca(bd3,picsize=c(-0.35,0.35), mcaoneploteach=FALSE, mcavariablecolours=TRUE,
showcolumncrs=FALSE)
plotca(bd3,picsize=c(-0.35,0.35), mcaoneploteach=FALSE, plotsymbolscolours=c(17,"red"),
showcolumncrs=FALSE)
plotca(bd3,picsize=c(-0.35,0.35), mcaoneploteach=FALSE, mcacategorycolours=TRUE,
showcolumncrs=1:5 )
# Three separate plots with ellipses for one variable on each
for (j in 1:length(bd3@Jk)) {
plot(cad3)
if (j==1) { firstcol <- 1 } else { firstcol <- cumsum(bd3@Jk)[j-1]+1 }
for (i in firstcol:cumsum(bd3@Jk)[j]) {
lines( ellipse(x=covmat(bd3,i,"column",1,2,FALSE), centre=bd3@Colprinccoord[i,cbind(1,2)],
npoints=1000),
cex=1, pch=".", col="red")
}
}
# For comparison, default plot with one plot each showing the ellipses for each variable
plotca(bd3,picsize=c(-0.35,0.35), mcacategorycolours=TRUE )
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab