R Module 10 - Data_Visualization
R Module 10 - Data_Visualization
Andrew Jaffe
January 7, 2016
Basic Plots
We see that the column names were years, and R doesn’t necessarily
like to read in a column name that starts with a number and puts
an X there.
We’ll just take off that X and get the years.
library(stringr)
year = names(death) %>% str_replace("X","") %>% as.integer
head(year)
as.numeric(death["Sweden", ])
3.0
2.0
1.0
0.0
Sweden
3.0
of deaths per family
2.0
1.0
Basic Plots
Let’s drop any of the projections and keep it to year 2012, and
change the points to blue.
plot(as.numeric(death["Sweden",])~year,
ylab = "# of deaths per family", main = "Sweden",
xlim = c(1760,2012), pch = 19, cex=1.2,col="blue")
Sweden
3.0
deaths per family
2.0
1.0
Basic Plots
You can also use the subset argument in the plot() function, only
when using formula notation:
plot(as.numeric(death["Sweden",])~year,
ylab = "# of deaths per family", main = "Sweden",
subset = year < 2015, pch = 19, cex=1.2,col="blue")
Sweden
3.0
deaths per family
2.0
1.0
Basic Plots
Using scatter.smooth plots the points and runs a loess smoother
through the data.
> scatter.smooth(as.numeric(death["Sweden",])~year,span=0.2
+ ylab="# of deaths per family", main = "Sweden",lwd=
+ subset = year < 2015, pch = 19, cex=0.9,col="grey")
Sweden
3.0
deaths per family
2.0
1.0
Basic Plots
par(mfrow=c(1,2)) tells R that we want to set a parameter (par
function) named mfrow (number of plots - 1 row, 2 columns) so we
can have 2 plots side by side (Sweden and the UK)
> par(mfrow=c(1,2))
> scatter.smooth(as.numeric(death["Sweden",])~year,span=0.2
+ ylab="# of deaths per family", main = "Sweden",lwd=
+ xlim = c(1760,2012), pch = 19, cex=0.9,col="grey")
> scatter.smooth(as.numeric(death["United Kingdom",])~year,
+ ylab="# of deaths per family", main = "United Kingd
+ xlim = c(1760,2012), pch = 19, cex=0.9,col="grey")
par(mfrow=c(1,2))
yl = range(death[c("Sweden","United Kingdom"),])
scatter.smooth(as.numeric(death["Sweden",])~year,span=0.2,y
ylab="# of deaths per family", main = "Sweden",lwd=3,
xlim = c(1760,2012), pch = 19, cex=0.9,col="grey")
scatter.smooth(as.numeric(death["United Kingdom",])~year,sp
ylab="", main = "United Kingdom",lwd=3,ylim=yl,
xlim = c(1760,2012), pch = 19, cex=0.9,col="grey")
3.0
of deaths per family
2.0
2.0
1.0
1.0
Bar Plots
I Stacked Bar Charts are sometimes wanted to show distributions
of data
## Stacked Bar Charts
cars = read.csv("http://biostat.jhsph.edu/~ajaffe/files/kag
counts <- table(cars$IsBadBuy, cars$VehicleAge)
barplot(counts, main="Car Distribution by Age and Bad Buy S
xlab="Vehicle Age", col=c("darkblue","red"),
legend = rownames(counts))
1
0
Bar Plots
prop.table allows you to convert a table to proportions (depends
on margin - either row percent or column percent)
1
0
0.4
Bar Plots
Using the beside argument in barplot, you can get side-by-side
barplots.
# Stacked Bar Plot with Colors and Legend
barplot(counts, main="Car Distribution by Age and Bad Buy S
xlab="Vehicle Age", col=c("darkblue","red"),
legend = rownames(counts), beside=TRUE)
0
10000
1
0
Graphics parameters
Basically, you are creating a pdf file, and telling R to write any
subsequent plots to that file. Once you are done, you turn the
device off. Note that failing to turn the device off will create a pdf
file that is corrupt, that you cannot open.
Boxplots, revisited
These are one of my favorite plots. They are way more informative
than the barchart + antenna. . .
palette("default")
plot(1:8, 1:8, type="n")
text(1:8, 1:8, lab = palette(), col = 1:8)
gray
yellow
7
magenta
cyan
5
1:8
blue
green3
3
red
Colors
The default color palette is pretty bad, so you can try to make your
own.
palette(c("darkred","orange","blue"))
plot(1:3,1:3,col=1:3,pch =19,cex=2)
1.5 2.0 2.5 3.0
1:3
Colors
It’s actually pretty hard to make a good color palette. Luckily, smart
and artistic people have spent a lot more time thinking about this.
The result is the ‘RColorBrewer’ package
RColorBrewer::display.brewer.all() will show you all of the palettes
available. You can even print it out and keep it next to your monitor
for reference.
The help file for brewer.pal() gives you an idea how to use the
package.
You can also get a “sneak peek” of these palettes at:
www.colorbrewer2.com . You would provide the number of levels or
classes of your data, and then the type of data: sequential,
diverging, or qualitative. The names of the RColorBrewer palettes
are the string after ‘pick a color scheme:’
Colors
palette("default")
plot(weight ~ Time, data= ChickWeight, pch = 19, col = Diet
350
250
weight
150
50
Colors
library(RColorBrewer)
palette(brewer.pal(5,"Dark2"))
plot(weight ~ Time, data=ChickWeight, pch = 19, col = Diet
350
250
weight
150
50
Colors
library(RColorBrewer)
palette(brewer.pal(5,"Dark2"))
plot(weight ~ jitter(Time,amount=0.2),data=ChickWeight,
pch = 19, col = Diet,xlab="Time")
350
250
weight
150
0
Adding legends
The legend() command adds a legend to your plot. There are tons
of arguments to pass it.
x, y=NULL: this just means you can give (x,y) coordinates, or more
commonly just give x, as a character string:
“top”,“bottom”,“topleft”,“bottomleft”,“topright”,“bottomright”.
legend: unique character vector, the levels of a factor
pch, lwd: if you want points in the legend, give a pch value. if you
want lines, give a lwd value.
col: give the color for each legend level
Adding legends
palette(brewer.pal(5,"Dark2"))
plot(weight ~ jitter(Time,amount=0.2),data=ChickWeight,
pch = 19, col = Diet,xlab="Time")
legend("topleft", paste("Diet",levels(ChickWeight$Diet)),
col = 1:length(levels(ChickWeight$Diet)),
lwd = 3, ncol = 2)
350
Diet 1 Diet 3
Diet 2 Diet 4
250
weight
150
Coloring by variable
> circ = read.csv("http://www.aejaffe.com/winterR_2016/data
+ header=TRUE,as.is=TRUE)
> palette(brewer.pal(7,"Dark2"))
> dd = factor(circ$day)
> plot(orangeAverage ~ greenAverage, data=circ,
+ pch=19, col = as.numeric(dd))
> legend("bottomright", levels(dd), col=1:length(dd), pch =
Friday
eAverage
5000
Monday
Saturday
Sunday
Coloring by variable
> dd = factor(circ$day, levels=c("Monday","Tuesday","Wednes
+ "Thursday","Friday","Saturday","Sunday"))
> plot(orangeAverage ~ greenAverage, data=circ,
+ pch=19, col = as.numeric(dd))
> legend("bottomright", levels(dd), col=1:length(dd), pch =
Monday
orangeAverage
5000
Tuesday
Wednesday
Thursday
2000
Friday
Saturday
ggplot2
ggplot2 is a package of plotting that is very popular and powerful.
qplot is a short hand for “quick plot”. We can simply do a boxplot:
> library(ggplot2)
> qplot(factor(Diet), y = weight,
+ data = ChickWeight, geom = "boxplot")
300
weight
200
ggplot2
The generic plotting function is ggplot:
300
weight
200
100
Boxplots revisited again
We can do the same plot, by just saying we want a boxplot and
points (and jitter the points)
300
weight
200
ggplot2: Adding 2 geoms together
To have multiple geometrics, just “add” them
300
weight
200
100
ggplot2: Adding 2 geoms together
To have multiple geometrics, just “add” them
g + geom_boxplot() + geom_jitter()
300
weight
200
100
Histograms again
We can do histograms again using hist. Let’s do histograms of
weight at all time points for the chick’s weights. We reiterate how
useful these are to show your data.
Histogram of ChickWeight$weight
Frequency
80
40
Multiple Histograms
> qplot(x = weight,
+ fill = factor(Diet),
+ data = ChickWeight,
+ geom = c("histogram"))
60
40 factor(Diet)
1
count
2
3
20 4
Multiple Histograms
Alpha refers tot he opacity of the color, less is
60
40 Diet
1
count
2
3
20 4
Multiple Densities
We cold also do densities
0.0075
Diet
1
density
0.0050
2
3
4
0.0025
Multiple Densities
> qplot(x= weight, colour = Diet, data = ChickWeight,
+ geom = c("density"), alpha=I(.7))
0.0075
Diet
1
density
0.0050
2
3
4
0.0025
Multiple Densities
> ggplot(aes(x= weight, colour = Diet),
+ data = ChickWeight) + geom_density(alpha=I(.7))
0.0075
Diet
1
density
0.0050
2
3
4
0.0025
Multiple Densities
You can take off the lines of the bottom like this
0.0075
Diet
1
density
0.0050
2
3
4
0.0025
Spaghetti plot
We can make a spaghetti plot by telling ggplot we want a “line”,
and each line is colored by Chick.
8 27 45
200 17 28 43
19 26 41
4 25 47
Spaghetti plot: Facets
In ggplot2, if you want separate plots for something, these are
referred to as facets.
16 14 38
1 2
15 7 32
300 13 24 40
9 30 34
200
20 22 35
100 10 23 44
weight
8 27 45
3 4 17 28 43
19 26 41
300
4 25 47
Spaghetti plot: Facets
We can turn off the legend (referred to a “guide” in ggplot2). (Note
- there is different syntax with the +)
1 2
300
200
100
weight
3 4
Spaghetti plot: Facets
> ggplot(aes(x = Time, y = weight, colour = Chick),
+ data = ChickWeight) + geom_line() +
+ facet_wrap(facets = ~Diet) + guides(colour = FALSE)
1 2
300
200
100
weight
3 4
300
200
ggplot2
Let’s try this out on the childhood mortality data used above.
However, let’s do some manipulation first, by using gather on the
data to convert to long.
library(tidyr)
long = death
long$state = rownames(long)
long = long %>% gather(year, deaths, -state)
head(long, 2)
library(stringr)
library(dplyr)
long$year = long$year %>% str_replace("^X", "") %>% as.nume
long = long %>% filter(!is.na(deaths))
ggplot2
> qplot(x = year, y = deaths, colour = state,
+ data = long, geom = "line") + guides(colour = FALSE)
4
deaths
2
ggplot2
Let’s try to make it different like base R, a bit. We use tile for the
geometric unit:
Madagascar
Macedonia,
Macao, FYR
China
Luxembourg
Lithuania
Libya
Liberia
Lesotho
Lebanon
Latvia
Korea,Korea,
Dem. Laos
Kyrgyzstan
Kuwait
Rep.
Kenya
Kazakhstan
Jordan
Japan
JamaicaItaly
Israel
Ireland
Iraq
Iran
Hong Indonesia
Kong, India
Iceland
Hungary
China
Honduras
Guyana
Guinea−Bissau
GuineaHaiti
Guatemala
Guam
Guadeloupe
Grenada
Greece
Ghana
Germany
Georgia
Gambia
Gabon
French
FrenchPolynesia
Guiana
France
Finland Fiji
Equatorial Ethiopia
Estonia
Eritrea
Guinea
El Salvador
Dominican Egypt
Ecuador
Rep.
ggplot2
Useful links:
I http://docs.ggplot2.org/0.9.3/index.html
I http://www.cookbook-r.com/Graphs/