Introduction to Plotting Data in R Chris Robertson
Introduction to Plotting Data in R Chris Robertson Strathclyde University, Glasgow, Scotland Health Protection Scotland, Glasgow, Scotland International Prevention Research Institute, Lyon, France Feb 2016 PHI Graphs and Plots 1
Data Visualisation • • • First Steps – Look at the data Tabulations, bar charts for categorical data Histograms, density plots for measurements Scatter plots Association plots Feb 2016 PHI Graphs and Plots 2
Graphical Investigation • Use the HAI Prevalence Data • Age, Total Stay are numeric • Other variables are categorical factors Feb 2016 PHI Graphs and Plots 3
Types of Data • Categorical – Yes/No (binary) • [Factor in R or 0/1 indicator variable] – Labelled (type or car) • [Factor in R] – Ordered (severity of disease) • [Ordered Factor in R] • Measured – Continuous measurement (costs/value/time) – Discrete count (number of transactions, prescriptions) • [Both numeric variables in R] Feb 2016 PHI Graphs and Plots 4
Visualising Data in R • Extensive graphical plots • Libraries – lattice – graphics – ggplot – maptools • Extensive low level plotting functions to control the plotting window • Export in a variety of formats Feb 2016 PHI Graphs and Plots 5
barchart(table(HAI$Specialty), horizontal=FALSE, xlab="Specialty", ylab="Frequency", col="black") Feb 2016 PHI Graphs and Plots 6
barchart(table(HAI$Specialty), horizontal=TRUE, ylab="Specialty", xlab="Frequency", col="black") Feb 2016 PHI Graphs and Plots 7
histogram(~Age, data=HAI) Feb 2016 PHI Graphs and Plots 8
histogram(~Age|Sex, data=HAI) Feb 2016 PHI Graphs and Plots 9
histogram(~Age|Sex, data=HAI, subset=Sex != "Unknown") Feb 2016 PHI Graphs and Plots 10
histogram(~Age|Sex, data=HAI, subset=Sex != "Unknown", layout=c(1, 2)) Feb 2016 PHI Graphs and Plots 11
densityplot(~Age|Sex, data=HAI, subset=Sex != "Unknown", layout=c(1, 2), plot. points=TRUE, col="black") Feb 2016 PHI Graphs and Plots 12
densityplot(~Age|HAI, data=HAI, layout=c(1, 2), plot. points=TRUE, col="black", main="HAI Status") Feb 2016 PHI Graphs and Plots 13
bwplot(~Age|HAI, data=HAI, layout=c(1, 2), main="HAI Status") Feb 2016 PHI Graphs and Plots 14
bwplot(~Age|HAI+Sex, data=HAI, subset=Sex != "Unknown", layout=c(1, 4), main="HAI Status and Gender") Feb 2016 PHI Graphs and Plots 15
mosaicplot(urinarycatheter~HAI, data=HAI, xlab="Urinary Catheter", ylab="HAI", main="") Feb 2016 PHI Graphs and Plots 16
mosaicplot(Specialty~HAI, data=HAI, xlab="Specialty", ylab="HAI", main="") Feb 2016 PHI Graphs and Plots 17
xyplot(log 10(Total. Stay+1) ~ Age, data=HAI, xlab="Age", ylab="log 10 Total Stay") Feb 2016 PHI Graphs and Plots 18
library(graphics) smooth. Scatter(HAI$Age, log 10(HAI$Total. Stay+1), xlab="Age", ylab="log 10 Total Stay") Feb 2016 PHI Graphs and Plots 19
xyplot(log 10(Total. Stay+1) ~ Age|Specialty, data=HAI, xlab="Age", ylab="log 10 Total Stay") Feb 2016 PHI Graphs and Plots 20
levels(HAI$Specialty) <- c("Geriatric", "IC", "Medicine", "Unknown", "Obs/Gyn", "Other", "Psychiatry", "Surgery") xyplot(log 10(Total. Stay+1) ~ Age|Specialty, data=HAI, xlab="Age", ylab="log 10 Total Stay") Feb 2016 PHI Graphs and Plots 21
R Libraries • Foreign – reading in data from other statistical programs – Spss, Stata • RODBC – accessing data from databases • XLConnect – Reading Writing Excel Files Feb 2016 PHI Graphs and Plots 22
- Slides: 22