# R Graphs Gallery

• Some examples from latticeExtra
• cd_plot from vcd
• Possible structure:
• Y ~ X | Z1,Z2,...,G1,G2,... where Zs are 'panelling' variables and Gs are within panel grouping variable, where each of Y, X, Z, G can be continuous numerical, binary or multinomial which can in turn be ordinal or not. A good example for Y multinomial and X continuous is 'cd_plot' in the 'vcd' package.

## Gallery

Histograms

Boxplots

Density Plots & Histograms

Scatterplot Matrix

Scatterplot

Segment plots

Diagrams

## Histogram (package 'graphics')

``` library(car)
data(Chile)
# Package -- graphics
# Functions for base R graphics
hist(Chile\$age[Chile\$region=="C"],
breaks=seq(0,80,2),   # sets break points at 0, 2, 4, ..., 78, 80
xlim=c(0,80),		   # Limits on the X axis
ylim=c(0,50), 		   # Limits on the Y axis
main="1988 Chilean Voter Survey - Histogram of Voter Ages in Central Region",   # Changes the main title
xlab="Age")       # Changes the x-label
```

## Histogram (package 'lattice')

``` library(lattice)
library(car)
data(Chile)
# Package -- Lattice
# Histogram
histogram(~ age, data=Chile[Chile\$region=="C",], type="count",
breaks=seq(0,80,2),   # sets break points at 0, 2, 4, ..., 78, 80
main="1988 Chilean Voter Survey - Histogram of Voter Ages in Central Region", # Changes title
xlab="Age",
col="red")       # Sets colour of bars
```

## Panel Histogram (package 'lattice')

``` library(lattice)
library(car)
data(Chile)
# Package -- Lattice
# Creates a panel histogram
histogram(~ age|region, data=Chile, type="count",
breaks=seq(0,80,2),   # sets break points at 0, 2, 4, ..., 78, 80
xlim=c(0,80),		  # Limits on the X axis
main="1988 Chilean Voter Survey - Histogram of Voter Ages in Central Region",   # Main title
xlab="Age",
layout = c(2,3))       # panel layout 2 columns 3 rows
```

## Boxplot (package 'graphics')

``` library(car)
data(Chile)
# Package -- graphics
# Functions for base R graphics
boxplot(age ~ region, data=Chile,
main="1988 Chilean Voter Survey - Age by Geographic Region",
ylab="Age")
mtext("C-Central, M-Metropolitan Santiago, N-North, S-South, SA-City of Santiago")  # adds a sub-title
```

## Boxplot (package 'lattice')

``` library(car)
data(Chile)
library(lattice)
# Package -- lattice
# Lattice boxplot
bwplot(age ~ region, data=Chile,
main="1988 Chilean Voter Survey - Age by Geographic Region",
sub="C-Central, M-Metropolitan Santiago, N-North, S-South, SA-City of Santiago",
ylab="Age",
col="red")
```

## Panel Boxplot (package 'lattice')

``` library(car)
data(Chile)
library(lattice)
# Package -- lattice
# Lattice boxplot with panels
bwplot(age ~ region | education, data=Chile,
main="1988 Chilean Voter Survey - Age by Geographic Region",
sub="C-Central, M-Metrop Santiago, N-North, S-South, SA-Santiago, P-Primary Ed, PS-Post Second, S-Secondary",
ylab="Age",
col="red")
```

## Density Histogram (package 'graphics')

``` library(car)
data(Chile)
# Density Histogram
# Package -- graphics
# Functions for base R graphics
hist(Chile\$age[Chile\$region=="C"],
freq=FALSE,
breaks=seq(0,80,5),
xlim=c(0,80),
main="1988 Chilean Voter Survey - Density Plot of Voter Ages in Central Region",	  # Changes the main title
xlab="Age")       # Changes the x-label
mtext("With overlaid normal distribution")
# Overlays a normal distribution centered at 'mean' with standard deviation 'sd'
```

## Density Plot (package 'lattice')

``` library(lattice)
library(car)
data(Chile)
# Package -- Lattice
# Panel Density Plot
densityplot(~ age, data = Chile,
main="1988 Chilean Voter Survey - Age",
sub="Including all geographic regions",
plot.points = FALSE, ref = TRUE,
col="purple")
```

## Panel Density Plot (package 'lattice')

``` library(lattice)
library(car)
data(Chile)
# Package -- Lattice
# Panel Density Plot
densityplot(~ age | region, data = Chile,
main="1988 Chilean Voter Survey - Age by Region",
sub="C-Central, M-Metrop Santiago, N-North, S-South, SA-Santiago",
plot.points = FALSE, ref = TRUE)
```

## Overlay Density Plot (package 'lattice')

``` library(lattice)
library(car)
data(Chile)
# Package -- Lattice
# Overlay density plots
densityplot(~ age,
groups = region,  # Separate lines for each group
data = Chile,
plot.points = FALSE,
ref = TRUE,
auto.key = list(columns = 3),
main="1988 Chilean Voter Survey - Age by Region",
sub="C-Central, M-Metrop Santiago, N-North, S-South, SA-Santiago")
```

## Scatterplot Matrix (package 'graphics')

``` library(car)
data(Baumann)
# Package -- graphics
# Functions for base R graphics
```

## Scatterplot Matrix (package 'car')

``` library(car)
data(Baumann)
# Package -- car
```

## Scatterplot (package 'graphics')

``` data(Davis, package="car")
# Package -- graphics
plot(weight ~ height, data=Davis, subset=-12,
main="Davis Data - Weight by Height",
sub="Data collected by Davis, data set available in package car"
)
```

## Scatterplot (package 'lattice')

``` library(lattice)
data(Davis, package="car")
# Package -- lattice
xyplot(weight ~ height, data=Davis, subset=-12,
main="Davis Data - Weight by Height",
sub="Data collected by Davis, data set available in package car",
panel = function(x, y) {
panel.grid(h = -1, v = 2) # -1 align horiz grid to ticks, 2 indicates 2 vertical lines
panel.xyplot(x, y, col='purple')
panel.loess(x, y, span=0.5, col='red')  # Span is the loess smoothness parameter, default 2/3
panel.lmline(x, y)  # Adds a least squares line
} )
```

## Panel Scatterplot (package 'lattice')

``` library(lattice)
data(Davis, package="car")
# Package -- lattice
# Panel scatter plot
xyplot(weight ~ height|sex, data=Davis, subset=-12, auto.key=TRUE,
panel = function(x, y) {
panel.grid(h = -1, v = 2) # -1 align horiz grid to ticks, 2 indicates 2 vertical lines
panel.xyplot(x, y)
panel.loess(x, y, span=0.5, col='red')  # Span is the loess smoothness parameter, default 2/3
panel.lmline(x, y)  # Adds a least squares line
})
```

## Panel Scatterplot 2 (package 'lattice')

``` library(lattice)
data(Davis, package="car")
# Package -- lattice
# Panel scatter plot
xyplot(weight ~ height|sex, data=Davis, subset=-12, auto.key=TRUE,
layout = c(1,2),
panel = function(x, y) {
panel.grid(h = -1, v = 2) # -1 align horiz grid to ticks, 2 indicates 2 vertical lines
panel.xyplot(x, y)
panel.loess(x, y, span=0.5, col='red')  # Span is the loess smoothness parameter, default 2/3
panel.lmline(x, y)  # Adds a least squares line
})
```

## Panel Scatterplot with layer (package 'latticeExtra')

``` library(lattice)
library(latticeExtra)
data(Davis, package = 'car' )
xyplot(weight ~ height|sex, data=Davis, subset=-12, auto.key=TRUE,
layout = c(1,2)) +
layer( panel.grid(h = -1, v = 2) ) +
layer( panel.loess(x, y, span=0.5, col='red') ) +
layer( panel.lmline(x, y) )
```

## Segment plot (package 'latticeExtra')

``` library(lattice)
library(latticeExtra)
data(USCancerRates)
segplot(reorder(factor(county), rate.male) ~ LCL95.male + UCL95.male,
data = subset(USCancerRates, state == "Washington"),
level = rate.female, col.regions = terrain.colors)
```

## Segment plot 2 (package 'latticeExtra')

``` library(lattice)
library(latticeExtra)
data(USCancerRates)
segplot(reorder(factor(county), rate.male) ~ LCL95.male + UCL95.male,
data = subset(USCancerRates, state == "Washington"),
draw.bands = FALSE, centers = rate.male,
segments.fun = panel.arrows, ends = "both",
angle = 90, length = 1, unit = "mm")
```

## Diagram (package 'diagram')

```library(diagram)
par(mar=c(1,1,1,1))
par(family="sans")
openplotmat()
elpos<-coordinates (c(1,1,2,4))
fromto <- matrix(ncol=2,byrow=TRUE,data=c(1,2,2,3,2,4,4,7,4,8))
nr     <-nrow(fromto)
arrpos <- matrix(ncol=2,nrow=nr)
for (i in 1:nr)
arrpos[i,]<- straightarrow (to=elpos[fromto[i,2],],from=elpos[fromto[i,1],]
,lwd=2,arr.pos=0.6,arr.length=0.5)

dd <- c(0.0,0.025)
text(arrpos[2,1]+0.05,arrpos[2,2],"yes")
text(arrpos[3,1]-0.05,arrpos[3,2],"no")
text(arrpos[4,1]+0.05,arrpos[4,2]+0.05,"yes")
text(arrpos[5,1]-0.05,arrpos[5,2]+0.05,"no")
```