Module 2

Foo C.Y
April 2017

Module 2

ggplot alt text

ggmap alt text

Base R graphs

Load data

dta <- read.csv("F:/Dropbox/Conference.Seminar.Talks/NIH Monash R Course/04 Module 2/cvdata.csv")

Base R graphs - boxplot

boxplot(dta$heartrate)

plot of chunk unnamed-chunk-2

boxplot(dta$heartrate,dta$ptsex)

plot of chunk unnamed-chunk-3

Base R graphs - histogram

hist(dta$heartrate)

plot of chunk unnamed-chunk-4

Base R graphs - barplot

barplot(table(dta$ptsex))

plot of chunk unnamed-chunk-5

Base R graphs - scatterplot

plot(dta$heartrate,dta$weight)

plot of chunk unnamed-chunk-6

Explore a little more for Base R Graphics

?plot
plot(dta$heartrate,dta$weight, type = "l")

plot of chunk unnamed-chunk-8

plot(dta$heartrate,dta$weight, type = "l",col="red")

plot of chunk unnamed-chunk-9

ggplot

install.packages("ggplot2")
library(ggplot2)

Elements of Graphing in R - Overall

alt text

alt text

Elements of Graphing in R

  • Base + Aesthetics + Geometry
ggplot(data=dta,aes(x= , y=))

plot of chunk unnamed-chunk-12

Elements of Graphing in R

  • Histogram: Base + Aesthetics + Geometry
ggplot(data=dta,aes(x=heartrate))+
  geom_histogram()

plot of chunk unnamed-chunk-13

Elements of Graphing in R

  • Scatterplot: Base + Aesthetics + Geometry
ggplot(data=dta,aes(x=weight,y=heartrate))+
  geom_point()

plot of chunk unnamed-chunk-14

Elements of Graphing in R

  • Scatterplot: Base + Aesthetics + Geometry + facet
ggplot(data=dta,aes(x=weight,y=heartrate))+
  geom_point()+
  facet_grid(. ~ ptsex)

plot of chunk unnamed-chunk-15

Help - R Cookbook

Try to find out how you can plot this:

plot of chunk unnamed-chunk-16

Try to find out how you can plot this:

plot of chunk unnamed-chunk-17

Touching up...

alt text

  • Change axis labels
  • Add title
  • Colour

Touching up...

ggplot(data=dta,aes(x=heartrate))+
  geom_histogram()+
  facet_grid(.~acsstratum)+
  xlab("Heart Rate")+
  ylab("Frequency")+
  ggtitle("Figure 1. Heart Rate by ACS Type")

plot of chunk unnamed-chunk-18

Touching up...

  • changing the facet labels
  • many ways: change the data or use the “labeller”
acs <- c(`1` = "UA",`2` = "NSTEMI", `3` = "STEMI")

then...

ggplot(data=dta,aes(x=heartrate))+
  geom_histogram()+
  facet_grid(.~acsstratum, labeller = as_labeller(acs))+
  xlab("Heart Rate")+
  ylab("Frequency")

plot of chunk unnamed-chunk-20

Multigraph in one page

install.packages("gridExtra")
library(gridExtra)
grid.arrange(plot1,plot2, ncol=2, nrow =1)

plot of chunk unnamed-chunk-23

Last part ! Output the graphs

  • Saving/copy from the screen

OR

  • use script: pdf, png, svg

PDF is a vector file format. Vector files are generally preferred for print output because the resulting output can be scaled to any size without pixelation.

SVG is another vector format.

PNG are bitmap formats. If they are magnified, the pixels may be visible.

Output the graphs as pdf

library(ggplot2)
myplot<-ggplot(data=dta,aes(x=heartrate))+
        geom_histogram()+
        facet_grid(.~acsstratum, labeller = as_labeller(acs))+
        xlab("Heart Rate")+
        ylab("Frequency")
pdf("plot.pdf")
plot(myplot)
dev.off()
png 
  2 

Output the graphs as pdf

PDF's are 7x7 inches by default, and each new plot is on a new page. The size can be changed:

# 6x3 inches
pdf("plots.pdf", width=6, height=3)

# 10x6 cm
pdf("plots.pdf", width=10/2.54, height=6/2.54)

Output the graphs as SVG

svg("plot.svg")
plot(myplot)
dev.off()

Output the graphs as PNG

png("plot.png")
plot(myplot)
dev.off()

Objective checks

alt text

alt text

ggmap

alt text

alt text

geocoding - almost a must have tool for modern analytics

dta2 <- read.csv("./hospdata.csv")
  • find out which col has the address info
str(dta2)
'data.frame':   203 obs. of  10 variables:
 $ AddressText: Factor w/ 203 levels "1 & 3, Jalan 6/23 E, Taman Danau Kota, Setapak  53300 Wilayah Persekutuan Kuala Lumpur",..: 50 196 17 137 176 132 199 27 14 92 ...
 $ State      : Factor w/ 13 levels "Johor","Kedah",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Type       : Factor w/ 5 levels "Maternity centre",..: 1 3 1 1 3 3 2 3 1 3 ...
 $ Bed        : int  15 82 5 10 6 50 30 10 6 47 ...
 $ Adm        : int  1099 NA NA 219 NA 4848 66 NA 50 NA ...
 $ ALOS       : num  1.76 NA NA 3 NA 2 2 NA 1.4 NA ...
 $ BOR        : num  35.6 NA NA 18 NA 66 1.24 NA 3 NA ...
 $ TOI        : num  3.23 NA NA 13.68 NA ...
 $ lat        : num  1.48 1.48 44.73 1.49 32.99 ...
 $ long       : num  103.76 103.64 5.23 103.78 48.19 ...

the ggmap package

install.packages("ggmap")
library(ggmap)
  • geocode function
?geocode

Geocode

geocode(as.character(dta2$AddressText[1]))
       lon     lat
1 103.7633 1.48042

Download a map - get_map() + ggmap()

Msia <- get_map("Malaysia", zoom=7, source="stamen", maptype="toner-lite")
myMap= ggmap(Msia)
myMap

plot of chunk unnamed-chunk-35

Ploting on a map

myMap+geom_point(data = dta2, aes(x=long,y=lat))

plot of chunk unnamed-chunk-36

Touching up

myMap+geom_point(data = dta2,aes(x=long,y=lat),
                 alpha = 0.2,color="orange")

plot of chunk unnamed-chunk-37

Can you do this:

EMmap+geom_point(aes(x=long,y=lat),data = dta2,alpha = 0.2,color="red")

plot of chunk unnamed-chunk-39