R Graphs Gallery

From Wiki1

Jump to: navigation, search

Things to add?

  • 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

Hist.png Histogram.png Panelhistogram.png

Boxplots

BoxplotGentleR.png BwplotGentleR.png BwplotpanelsGentleR.png

Density Plots & Histograms

Densityhist.png DensityplotGentleR.png Densityplotpanels.png
Densityplotoverlay.png

Scatterplot Matrix

PairsGentleR.png ScatterplotMatrixGentleR.png

Scatterplot

Plotscatter.png XyplotGentleR.png XyplotPanelsGentleR.png
Xyplotpanelslayout.png Xyplot with layers.png

Segment plots

Segplot1.png Segplot2.png

Diagrams

Diagram.png

Contents


Histogram (package 'graphics')

Hist.png

 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')

Histogram.png

 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')

Panelhistogram.png

 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')

BoxplotGentleR.png

 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')

BwplotGentleR.png

 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')

BwplotpanelsGentleR.png

 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')

Densityhist.png

 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
 # adds a sub-title
 mtext("With overlaid normal distribution") 
 # Overlays a normal distribution centered at 'mean' with standard deviation 'sd'
 curve(dnorm(x, mean=mean(Chile$age[Chile$region=="C"]), sd=sd(Chile$age[Chile$region=="C"])), add=TRUE, lty=2)


Density Plot (package 'lattice')

DensityplotGentleR.png

 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')

Densityplotpanels.png

 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')

Densityplotoverlay.png

 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')

PairsGentleR.png

 library(car)
 data(Baumann)
 # Package -- graphics
 # Functions for base R graphics
 pairs(Baumann, main="Baumann Reading Study Data")


Scatterplot Matrix (package 'car')

ScatterplotMatrixGentleR.png

 library(car)
 data(Baumann)
 # Package -- car
 scatterplotMatrix(Baumann, main="Baumann Reading Study Data")


Scatterplot (package 'graphics')

Plotscatter.png

 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')

XyplotGentleR.png

 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')

XyplotPanelsGentleR.png

 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')

Xyplotpanelslayout.png

 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')

Xyplot with layers.png

 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')

Segplot1.png

 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')

Segplot2.png

 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')

Diagram.png

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)
textellipse(elpos[1,],0.1,lab="start",box.col="green",shadow.col="darkgreen",shadow.size=0.005,cex=1.5)
textrect   (elpos[2,],0.15,0.05,lab="found term?",box.col="lightblue",shadow.col="blue",shadow.size=0.005,cex=1.5)
textrect   (elpos[4,],0.15,0.05,lab="related?",box.col="lightblue",shadow.col="blue",shadow.size=0.005,cex=1.5)
textellipse(elpos[3,],0.1,0.1,lab=c("other","term"),box.col="orange",shadow.col="red",shadow.size=0.005,cex=1.5)
textellipse(elpos[3,],0.1,0.1,lab=c("other","term"),box.col="orange",shadow.col="red",shadow.size=0.005,cex=1.5)
textellipse(elpos[7,],0.1,0.1,lab=c("make","a link"),box.col="orange",shadow.col="red",shadow.size=0.005,cex=1.5)
textellipse(elpos[8,],0.1,0.1,lab=c("new","article"),box.col="orange",shadow.col="red",shadow.size=0.005,cex=1.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")

Other galleries

Personal tools