Foo C.Y
April 2017
ggplot
ggmap
Load data
dta <- read.csv("F:/Dropbox/Conference.Seminar.Talks/NIH Monash R Course/04 Module 2/cvdata.csv")
boxplot(dta$heartrate)
boxplot(dta$heartrate,dta$ptsex)
hist(dta$heartrate)
barplot(table(dta$ptsex))
plot(dta$heartrate,dta$weight)
?plot
plot(dta$heartrate,dta$weight, type = "l")
plot(dta$heartrate,dta$weight, type = "l",col="red")
install.packages("ggplot2")
library(ggplot2)
ggplot(data=dta,aes(x= , y=))
ggplot(data=dta,aes(x=heartrate))+
geom_histogram()
ggplot(data=dta,aes(x=weight,y=heartrate))+
geom_point()
ggplot(data=dta,aes(x=weight,y=heartrate))+
geom_point()+
facet_grid(. ~ ptsex)
ggplot(data=dta,aes(x=heartrate))+
geom_histogram()+
facet_grid(.~acsstratum)+
xlab("Heart Rate")+
ylab("Frequency")+
ggtitle("Figure 1. Heart Rate by ACS Type")
acs <- c(`1` = "UA",`2` = "NSTEMI", `3` = "STEMI")
ggplot(data=dta,aes(x=heartrate))+
geom_histogram()+
facet_grid(.~acsstratum, labeller = as_labeller(acs))+
xlab("Heart Rate")+
ylab("Frequency")
install.packages("gridExtra")
library(gridExtra)
grid.arrange(plot1,plot2, ncol=2, nrow =1)
OR
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.
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
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)
svg("plot.svg")
plot(myplot)
dev.off()
png("plot.png")
plot(myplot)
dev.off()
dta2 <- read.csv("./hospdata.csv")
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 ...
install.packages("ggmap")
library(ggmap)
?geocode
geocode(as.character(dta2$AddressText[1]))
lon lat
1 103.7633 1.48042
Msia <- get_map("Malaysia", zoom=7, source="stamen", maptype="toner-lite")
myMap= ggmap(Msia)
myMap
myMap+geom_point(data = dta2, aes(x=long,y=lat))
myMap+geom_point(data = dta2,aes(x=long,y=lat),
alpha = 0.2,color="orange")
EMmap+geom_point(aes(x=long,y=lat),data = dta2,alpha = 0.2,color="red")